mirror of
https://github.com/EchoVault/SugarDB.git
synced 2026-04-22 23:47:09 +08:00
703ad2a802
Renames project to "SugarDB" - @kelvinmwinuka
107 lines
2.9 KiB
Plaintext
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>
|