Files
SugarDB/docs/docs/commands/generic/expire.mdx
T
Kelvin Mwinuka 703ad2a802 Rename the project to SugarDB. (#130)
Renames project to "SugarDB" - @kelvinmwinuka
2024-09-22 21:31:12 +08:00

107 lines
2.9 KiB
Plaintext

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
# EXPIRE
### Syntax
```
EXPIRE key seconds [NX | XX | GT | LT]
```
### Module
<span className="acl-category">generic</span>
### Categories
<span className="acl-category">fast</span>
<span className="acl-category">keyspace</span>
<span className="acl-category">write</span>
### Description
Expire the key in the specified number of seconds. This commands turns a key into a volatile one.
### Options
- `NX` - Only set the expiry time if the key has no associated expiry.
- `XX` - Only set the expiry time if the key already has an expiry time.
- `GT` - Only set the expiry time if the new expiry time is greater than the current one.
- `LT` - Only set the expiry time if the new expiry time is less than the current one.
<br></br>
NX, GT, and LT are mutually exclusive. XX can additionally be passed in with either GT or LT.
### Examples
<Tabs
defaultValue="go"
values={[
{ label: 'Go (Embedded)', value: 'go', },
{ label: 'CLI', value: 'cli', },
]}
>
<TabItem value="go">
The embedded API utilizes the ExpireOptions interface, which acts as a wrapper for the various expiry options.
<br></br>
ExpireOptions include the following constants:
- `NX` - Only set the expiry time if the key has no associated expiry.
- `XX` - Only set the expiry time if the key already has an expiry time.
- `GT` - Only set the expiry time if the new expiry time is greater than the current one.
- `LT` - Only set the expiry time if the new expiry time is less than the current one.
<br></br>
Add an expiration to a key:
```go
db, err := sugardb.NewSugarDB()
if err != nil {
log.Fatal(err)
}
ok, err := db.Expire("key", 10, nil)
```
Add an expiration to a key only if it does not have one already:
```go
db, err := sugardb.NewSugarDB()
if err != nil {
log.Fatal(err)
}
ok, err := db.Expire("key", 10, sugardb.NX)
```
Add an expiration to a key only if it has one already:
```go
db, err := sugardb.NewSugarDB()
if err != nil {
log.Fatal(err)
}
ok, err := db.Expire("key", 10, sugardb.XX)
```
Add an expiration to a key only if it already has one that is less than the current expiry:
```go
db, err := sugardb.NewSugarDB()
if err != nil {
log.Fatal(err)
}
ok, err := db.Expire("key", 10, sugardb.XX, sugardb.LT)
```
</TabItem>
<TabItem value="cli">
Add an expiration to a key:
```
> EXPIRE key 10
```
Add an expiration to a key only if it does not have one already:
```
> EXPIRE key 10 NX
```
Add an expiration to a key only if it has one already:
```
> EXPIRE key 10 XX
```
Add an expiration to a key only if it already has one that is less than the current expiry:
```
> EXPIRE key 10 XX LT
```
</TabItem>
</Tabs>