mirror of
https://github.com/EchoVault/SugarDB.git
synced 2025-09-26 20:11:15 +08:00
Iss 100: Implement interfaces and constants for generic API methods (#127)
SETOptions struct added. Adjusted Set method calls for using new struct. - @osteensco
This commit is contained in:
File diff suppressed because it is too large
Load Diff
1
docs/.gitignore
vendored
1
docs/.gitignore
vendored
@@ -8,6 +8,7 @@
|
||||
.docusaurus
|
||||
.cache-loader
|
||||
.next
|
||||
package-lock.json
|
||||
|
||||
# Misc
|
||||
.DS_Store
|
||||
|
@@ -25,6 +25,8 @@ Expire the key in the specified number of seconds. This commands turns a key int
|
||||
- `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
|
||||
|
||||
@@ -36,13 +38,21 @@ Expire the key in the specified number of seconds. This commands turns a key int
|
||||
]}
|
||||
>
|
||||
<TabItem value="go">
|
||||
Add an expiration to a key:
|
||||
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
|
||||
vault, err := echovault.NewEchoVault()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
ok, err := vault.Expire("key", 10, echovault.ExpireOptions{})
|
||||
ok, err := vault.Expire("key", 10, nil)
|
||||
```
|
||||
|
||||
Add an expiration to a key only if it does not have one already:
|
||||
@@ -51,7 +61,7 @@ Expire the key in the specified number of seconds. This commands turns a key int
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
ok, err := vault.Expire("key", 10, echovault.ExpireOptions{NX: true})
|
||||
ok, err := vault.Expire("key", 10, echovault.NX)
|
||||
```
|
||||
|
||||
Add an expiration to a key only if it has one already:
|
||||
@@ -60,7 +70,7 @@ Expire the key in the specified number of seconds. This commands turns a key int
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
ok, err := vault.Expire("key", 10, echovault.ExpireOptions{XX: true})
|
||||
ok, err := vault.Expire("key", 10, echovault.XX)
|
||||
```
|
||||
|
||||
Add an expiration to a key only if it already has one that is less than the current expiry:
|
||||
@@ -69,7 +79,7 @@ Expire the key in the specified number of seconds. This commands turns a key int
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
ok, err := vault.Expire("key", 10, echovault.ExpireOptions{XX: true, LT: true})
|
||||
ok, err := vault.Expire("key", 10, echovault.XX, echovault.LT)
|
||||
```
|
||||
</TabItem>
|
||||
<TabItem value="cli">
|
||||
|
106
docs/docs/commands/generic/expireat.mdx
Normal file
106
docs/docs/commands/generic/expireat.mdx
Normal file
@@ -0,0 +1,106 @@
|
||||
import Tabs from '@theme/Tabs';
|
||||
import TabItem from '@theme/TabItem';
|
||||
|
||||
# EXPIREAT
|
||||
|
||||
### Syntax
|
||||
```
|
||||
EXPIREAT key unix-time-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 at the provided unix-time. 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
|
||||
vault, err := echovault.NewEchoVault()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
ok, err := vault.Expire("key", 1767160800, nil)
|
||||
```
|
||||
|
||||
Add an expiration to a key only if it does not have one already:
|
||||
```go
|
||||
vault, err := echovault.NewEchoVault()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
ok, err := vault.Expire("key", 1767160800, echovault.NX)
|
||||
```
|
||||
|
||||
Add an expiration to a key only if it has one already:
|
||||
```go
|
||||
vault, err := echovault.NewEchoVault()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
ok, err := vault.Expire("key", 1767160800, echovault.XX)
|
||||
```
|
||||
|
||||
Add an expiration to a key only if it already has one that is less than the current expiry:
|
||||
```go
|
||||
vault, err := echovault.NewEchoVault()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
ok, err := vault.Expire("key", 1767160800, echovault.XX, echovault.LT)
|
||||
```
|
||||
</TabItem>
|
||||
<TabItem value="cli">
|
||||
Add an expiration to a key:
|
||||
```
|
||||
> EXPIRE key 1767160800
|
||||
```
|
||||
|
||||
Add an expiration to a key only if it does not have one already:
|
||||
```
|
||||
> EXPIRE key 1767160800 NX
|
||||
```
|
||||
|
||||
Add an expiration to a key only if it has one already:
|
||||
```
|
||||
> EXPIRE key 1767160800 XX
|
||||
```
|
||||
|
||||
Add an expiration to a key only if it already has one that is less than the current expiry:
|
||||
```
|
||||
> EXPIRE key 1767160800 XX LT
|
||||
```
|
||||
</TabItem>
|
||||
</Tabs>
|
@@ -35,13 +35,21 @@ Expire the key in the specified number of milliseconds. This commands turns a ke
|
||||
]}
|
||||
>
|
||||
<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
|
||||
vault, err := echovault.NewEchoVault()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
updated, err := vault.PExpire("key", 10000, echovault.PExpireOptions{})
|
||||
updated, err := vault.PExpire("key", 10000, nil)
|
||||
```
|
||||
|
||||
Add an expiration to a key only if it does not have one already:
|
||||
@@ -50,7 +58,7 @@ Expire the key in the specified number of milliseconds. This commands turns a ke
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
updated, err := vault.PExpire("key", 10000, echovault.PExpireOptions{NX: true})
|
||||
updated, err := vault.PExpire("key", 10000, echovault.NX)
|
||||
```
|
||||
|
||||
Add an expiration to a key only if it has one already:
|
||||
@@ -59,7 +67,7 @@ Expire the key in the specified number of milliseconds. This commands turns a ke
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
updated, err := vault.PExpire("key", 10000, echovault.PExpireOptions{XX: true})
|
||||
updated, err := vault.PExpire("key", 10000, echovault.XX)
|
||||
```
|
||||
|
||||
Add an expiration to a key only if it already has one that is less than the current expiry:
|
||||
@@ -68,7 +76,7 @@ Expire the key in the specified number of milliseconds. This commands turns a ke
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
updated, err := vault.PExpire("key", 10000, echovault.PExpireOptions{XX: true, LT: true})
|
||||
updated, err := vault.PExpire("key", 10000, echovault.XX, echovault.LT)
|
||||
```
|
||||
</TabItem>
|
||||
<TabItem value="cli">
|
||||
|
106
docs/docs/commands/generic/pexpireat.mdx
Normal file
106
docs/docs/commands/generic/pexpireat.mdx
Normal file
@@ -0,0 +1,106 @@
|
||||
import Tabs from '@theme/Tabs';
|
||||
import TabItem from '@theme/TabItem';
|
||||
|
||||
# PEXPIREAT
|
||||
|
||||
### Syntax
|
||||
```
|
||||
PEXPIREAT key unix-time-milliseconds [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 at the provided unix-time. 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
|
||||
vault, err := echovault.NewEchoVault()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
ok, err := vault.Expire("key", 1767160800000, nil)
|
||||
```
|
||||
|
||||
Add an expiration to a key only if it does not have one already:
|
||||
```go
|
||||
vault, err := echovault.NewEchoVault()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
ok, err := vault.Expire("key", 1767160800000, echovault.NX)
|
||||
```
|
||||
|
||||
Add an expiration to a key only if it has one already:
|
||||
```go
|
||||
vault, err := echovault.NewEchoVault()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
ok, err := vault.Expire("key", 1767160800000, echovault.XX)
|
||||
```
|
||||
|
||||
Add an expiration to a key only if it already has one that is less than the current expiry:
|
||||
```go
|
||||
vault, err := echovault.NewEchoVault()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
ok, err := vault.Expire("key", 1767160800000, echovault.XX, echovault.LT)
|
||||
```
|
||||
</TabItem>
|
||||
<TabItem value="cli">
|
||||
Add an expiration to a key:
|
||||
```
|
||||
> EXPIRE key 1767160800000
|
||||
```
|
||||
|
||||
Add an expiration to a key only if it does not have one already:
|
||||
```
|
||||
> EXPIRE key 1767160800000 NX
|
||||
```
|
||||
|
||||
Add an expiration to a key only if it has one already:
|
||||
```
|
||||
> EXPIRE key 1767160800000 XX
|
||||
```
|
||||
|
||||
Add an expiration to a key only if it already has one that is less than the current expiry:
|
||||
```
|
||||
> EXPIRE key 1767160800000 XX LT
|
||||
```
|
||||
</TabItem>
|
||||
</Tabs>
|
@@ -28,6 +28,7 @@ Set the value of a key, considering the value's type. If the key already exists,
|
||||
- `PXAT` - Expire at the exat time in unix milliseconds (positive integer).
|
||||
|
||||
|
||||
|
||||
### Examples
|
||||
|
||||
<Tabs
|
||||
@@ -38,13 +39,35 @@ Set the value of a key, considering the value's type. If the key already exists,
|
||||
]}
|
||||
>
|
||||
<TabItem value="go">
|
||||
The embedded API organizes the SET command options into constants wrapped in interfaces.
|
||||
<br></br>
|
||||
SetWriteOption
|
||||
- `SETNX` - Only set if the key does not exist.
|
||||
- `SETXX` - Only set if the key exists.
|
||||
<br></br>
|
||||
SetExOption
|
||||
- `SETEX` - Expire the key after the specified number of seconds.
|
||||
- `SETPX` - Expire the key after the specified number of milliseconds.
|
||||
- `SETEXAT` - Expire at the exact time in unix seconds.
|
||||
- `SETPXAT` - Expire at the exact time in unix milliseconds.
|
||||
<br></br>
|
||||
The API provides a struct called SETOptions that wraps these options in a convenient object.
|
||||
```go
|
||||
type SETOptions struct {
|
||||
WriteOpt SetWriteOption
|
||||
ExpireOpt SetExOption
|
||||
ExpireTime int
|
||||
Get bool
|
||||
}
|
||||
```
|
||||
<br></br>
|
||||
Set a value at a key:
|
||||
```go
|
||||
vault, err := echovault.NewEchoVault()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
ok, err := vault.Set("name", "EchoVault", echovault.SetOptions{})
|
||||
ok, err := vault.Set("name", "EchoVault", echovault.SETOptions{})
|
||||
```
|
||||
|
||||
Set a value only if the key does not exist:
|
||||
@@ -53,7 +76,7 @@ Set the value of a key, considering the value's type. If the key already exists,
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
ok, err := vault.Set("name", "EchoVault", echovault.SetOptions{NX: true})
|
||||
ok, err := vault.Set("name", "EchoVault", echovault.SETOptions{WriteOpt: echovault.SETNX})
|
||||
```
|
||||
|
||||
Set a value if key already exists and get the previous value:
|
||||
@@ -62,7 +85,7 @@ Set the value of a key, considering the value's type. If the key already exists,
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
previousValue, err := vault.Set("name", "EchoVault", echovault.SetOptions{XX: true, GET: true})
|
||||
previousValue, err := vault.Set("name", "EchoVault", echovault.SetOptions{WriteOpt: echovault.SETXX, Get: true})
|
||||
```
|
||||
|
||||
Set a value if the key already exists, return the previous value, and expire after 10 seconds:
|
||||
@@ -71,7 +94,7 @@ Set the value of a key, considering the value's type. If the key already exists,
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
previousValue, err := vault.Set("name", "EchoVault", echovault.SetOptions{XX: true, GET: true, EX: 10})
|
||||
previousValue, err := vault.Set("name", "EchoVault", echovault.SetOptions{WriteOpt: echovault.SETXX, ExpireOpt: echovault.SETEX, ExpireTime 10, Get: true})
|
||||
```
|
||||
</TabItem>
|
||||
<TabItem value="cli">
|
||||
|
@@ -29,6 +29,23 @@ const config: Config = {
|
||||
locales: ["en"],
|
||||
},
|
||||
|
||||
// Custom plugin for hot reloading
|
||||
plugins: [
|
||||
function hotReload() {
|
||||
return {
|
||||
name: 'hot-reload',
|
||||
configureWebpack() {
|
||||
return {
|
||||
watchOptions: {
|
||||
poll: 1000, // Check for changes every second
|
||||
aggregateTimeout: 300, // Delay before rebuilding
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
},
|
||||
],
|
||||
|
||||
presets: [
|
||||
[
|
||||
"classic",
|
||||
|
@@ -4168,11 +4168,6 @@ fs.realpath@^1.0.0:
|
||||
resolved "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz"
|
||||
integrity sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==
|
||||
|
||||
fsevents@~2.3.2:
|
||||
version "2.3.3"
|
||||
resolved "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz"
|
||||
integrity sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==
|
||||
|
||||
function-bind@^1.1.2:
|
||||
version "1.1.2"
|
||||
resolved "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz"
|
||||
|
@@ -15,42 +15,84 @@
|
||||
package echovault
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/echovault/echovault/internal"
|
||||
)
|
||||
|
||||
// SetOptions modifies the behaviour for the Set command
|
||||
// SetWriteOption constants
|
||||
type SetWriteOpt string
|
||||
|
||||
const (
|
||||
SETNX SetWriteOpt = "NX"
|
||||
SETXX SetWriteOpt = "XX"
|
||||
)
|
||||
|
||||
// SetWriteOption modifies the behavior of Set.
|
||||
//
|
||||
// NX - Only set if the key does not exist. NX is higher priority than XX.
|
||||
// SETNX - Only set if the key does not exist.
|
||||
//
|
||||
// XX - Only set if the key exists.
|
||||
//
|
||||
// GET - Return the old value stored at key, or nil if the value does not exist.
|
||||
//
|
||||
// EX - Expire the key after the specified number of seconds (positive integer).
|
||||
// EX has the highest priority
|
||||
//
|
||||
// PX - Expire the key after the specified number of milliseconds (positive integer).
|
||||
// PX has the second-highest priority.
|
||||
//
|
||||
// EXAT - Expire at the exact time in unix seconds (positive integer).
|
||||
// EXAT has the third-highest priority.
|
||||
//
|
||||
// PXAT - Expire at the exat time in unix milliseconds (positive integer).
|
||||
// PXAT has the least priority.
|
||||
type SetOptions struct {
|
||||
NX bool
|
||||
XX bool
|
||||
GET bool
|
||||
EX int
|
||||
PX int
|
||||
EXAT int
|
||||
PXAT int
|
||||
// SETXX - Only set if the key exists.
|
||||
type SetWriteOption interface {
|
||||
IsSetWriteOpt() SetWriteOpt
|
||||
}
|
||||
|
||||
// ExpireOptions modifies the behaviour of the Expire, PExpire, ExpireAt, PExpireAt.
|
||||
func (w SetWriteOpt) IsSetWriteOpt() SetWriteOpt { return w }
|
||||
|
||||
// SetExOption constants
|
||||
type SetExOpt string
|
||||
|
||||
const (
|
||||
SETEX SetExOpt = "EX"
|
||||
SETPX SetExOpt = "PX"
|
||||
SETEXAT SetExOpt = "EXAT"
|
||||
SETPXAT SetExOpt = "PXAT"
|
||||
)
|
||||
|
||||
// SetExOption modifies the behavior of Set.
|
||||
//
|
||||
// SETEX - Expire the key after the specified number of seconds (positive integer).
|
||||
//
|
||||
// SETPX - Expire the key after the specified number of milliseconds (positive integer).
|
||||
//
|
||||
// SETEXAT - Expire at the exact time in unix seconds (positive integer).
|
||||
//
|
||||
// SETPXAT - Expire at the exact time in unix milliseconds (positive integer).
|
||||
type SetExOption interface {
|
||||
IsSetExOpt() SetExOpt
|
||||
}
|
||||
|
||||
func (x SetExOpt) IsSetExOpt() SetExOpt { return x }
|
||||
|
||||
// SETOptions is a struct wrapper for all optional parameters of the Set command.
|
||||
//
|
||||
// `WriteOpt` - SetWriteOption - One of SETNX or SETXX.
|
||||
//
|
||||
// `ExpireOpt` - SetExOption - One of SETEX, SETPX, SETEXAT, or SETPXAT.
|
||||
//
|
||||
// `ExpireTime` - int - Time in seconds or milliseconds depending on what ExpireOpt was provided.
|
||||
//
|
||||
// `GET` - bool - Whether or not to return previous value if there was one.
|
||||
type SETOptions struct {
|
||||
WriteOpt SetWriteOption
|
||||
ExpireOpt SetExOption
|
||||
ExpireTime int
|
||||
Get bool
|
||||
}
|
||||
|
||||
// ExpireOptions constants
|
||||
type ExOpt string
|
||||
|
||||
const (
|
||||
NX ExOpt = "NX"
|
||||
XX ExOpt = "XX"
|
||||
LT ExOpt = "LT"
|
||||
GT ExOpt = "GT"
|
||||
)
|
||||
|
||||
// ExpireOptions modifies the behavior of Expire, PExpire, ExpireAt, PExpireAt.
|
||||
//
|
||||
// NX - Only set the expiry time if the key has no associated expiry.
|
||||
//
|
||||
@@ -59,17 +101,26 @@ type SetOptions struct {
|
||||
// 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.
|
||||
type ExpireOptions struct {
|
||||
NX bool
|
||||
XX bool
|
||||
LT bool
|
||||
GT bool
|
||||
//
|
||||
// NX, GT, and LT are mutually exclusive. XX can additionally be passed in with either GT or LT.
|
||||
type ExpireOptions interface {
|
||||
IsExOpt() ExOpt
|
||||
}
|
||||
type PExpireOptions ExpireOptions
|
||||
type ExpireAtOptions ExpireOptions
|
||||
type PExpireAtOptions ExpireOptions
|
||||
|
||||
// GetExOptions modifies the behaviour of
|
||||
func (x ExOpt) IsExOpt() ExOpt { return x }
|
||||
|
||||
// GetExOption constants
|
||||
type GetExOpt string
|
||||
|
||||
const (
|
||||
EX GetExOpt = "EX"
|
||||
PX GetExOpt = "PX"
|
||||
EXAT GetExOpt = "EXAT"
|
||||
PXAT GetExOpt = "PXAT"
|
||||
PERSIST GetExOpt = "PERSIST"
|
||||
)
|
||||
|
||||
// GetExOption modifies the behavior of GetEx.
|
||||
//
|
||||
// EX - Set the specified expire time, in seconds.
|
||||
//
|
||||
@@ -80,17 +131,12 @@ type PExpireAtOptions ExpireOptions
|
||||
// PXAT - Set the specified Unix time at which the key will expire, in milliseconds.
|
||||
//
|
||||
// PERSIST - Remove the time to live associated with the key.
|
||||
//
|
||||
// UNIXTIME - Number of seconds or miliseconds from now
|
||||
type GetExOptions struct {
|
||||
EX bool
|
||||
PX bool
|
||||
EXAT bool
|
||||
PXAT bool
|
||||
PERSIST bool
|
||||
UNIXTIME int
|
||||
type GetExOption interface {
|
||||
isGetExOpt() GetExOpt
|
||||
}
|
||||
|
||||
func (x GetExOpt) isGetExOpt() GetExOpt { return x }
|
||||
|
||||
// Set creates or modifies the value at the given key.
|
||||
//
|
||||
// Parameters:
|
||||
@@ -99,8 +145,6 @@ type GetExOptions struct {
|
||||
//
|
||||
// `value` - string - the value to place at the key.
|
||||
//
|
||||
// `options` - SetOptions.
|
||||
//
|
||||
// Returns: true if the set is successful, If the "Get" flag in SetOptions is set to true, the previous value is returned.
|
||||
//
|
||||
// Errors:
|
||||
@@ -108,28 +152,18 @@ type GetExOptions struct {
|
||||
// "key <key> does not exist"" - when the XX flag is set to true and the key does not exist.
|
||||
//
|
||||
// "key <key> does already exists" - when the NX flag is set to true and the key already exists.
|
||||
func (server *EchoVault) Set(key, value string, options SetOptions) (string, bool, error) {
|
||||
func (server *EchoVault) Set(key, value string, options SETOptions) (string, bool, error) {
|
||||
cmd := []string{"SET", key, value}
|
||||
|
||||
switch {
|
||||
case options.NX:
|
||||
cmd = append(cmd, "NX")
|
||||
case options.XX:
|
||||
cmd = append(cmd, "XX")
|
||||
if options.WriteOpt != nil {
|
||||
cmd = append(cmd, fmt.Sprint(options.WriteOpt))
|
||||
}
|
||||
|
||||
switch {
|
||||
case options.EX != 0:
|
||||
cmd = append(cmd, []string{"EX", strconv.Itoa(options.EX)}...)
|
||||
case options.PX != 0:
|
||||
cmd = append(cmd, []string{"PX", strconv.Itoa(options.PX)}...)
|
||||
case options.EXAT != 0:
|
||||
cmd = append(cmd, []string{"EXAT", strconv.Itoa(options.EXAT)}...)
|
||||
case options.PXAT != 0:
|
||||
cmd = append(cmd, []string{"PXAT", strconv.Itoa(options.PXAT)}...)
|
||||
if options.ExpireOpt != nil {
|
||||
cmd = append(cmd, []string{fmt.Sprint(options.ExpireOpt), strconv.Itoa(options.ExpireTime)}...)
|
||||
}
|
||||
|
||||
if options.GET {
|
||||
if options.Get {
|
||||
cmd = append(cmd, "GET")
|
||||
}
|
||||
|
||||
@@ -142,7 +176,7 @@ func (server *EchoVault) Set(key, value string, options SetOptions) (string, boo
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
if !options.GET {
|
||||
if !options.Get {
|
||||
previousValue = ""
|
||||
}
|
||||
|
||||
@@ -313,21 +347,16 @@ func (server *EchoVault) PTTL(key string) (int, error) {
|
||||
//
|
||||
// `seconds` - int - number of seconds from now.
|
||||
//
|
||||
// `options` - ExpireOptions
|
||||
// `options` - ExpireOptions - One of NX, GT, LT. XX can be passed with GT OR LT optionally.
|
||||
//
|
||||
// Returns: true if the key's expiry was successfully updated.
|
||||
func (server *EchoVault) Expire(key string, seconds int, options ExpireOptions) (bool, error) {
|
||||
func (server *EchoVault) Expire(key string, seconds int, options ...ExpireOptions) (bool, error) {
|
||||
cmd := []string{"EXPIRE", key, strconv.Itoa(seconds)}
|
||||
|
||||
switch {
|
||||
case options.NX:
|
||||
cmd = append(cmd, "NX")
|
||||
case options.XX:
|
||||
cmd = append(cmd, "XX")
|
||||
case options.LT:
|
||||
cmd = append(cmd, "LT")
|
||||
case options.GT:
|
||||
cmd = append(cmd, "GT")
|
||||
for _, opt := range options {
|
||||
if opt != nil {
|
||||
cmd = append(cmd, fmt.Sprint(opt))
|
||||
}
|
||||
}
|
||||
|
||||
b, err := server.handleCommand(server.context, internal.EncodeCommand(cmd), nil, false, true)
|
||||
@@ -350,18 +379,13 @@ func (server *EchoVault) Expire(key string, seconds int, options ExpireOptions)
|
||||
// `options` - PExpireOptions
|
||||
//
|
||||
// Returns: true if the key's expiry was successfully updated.
|
||||
func (server *EchoVault) PExpire(key string, milliseconds int, options PExpireOptions) (bool, error) {
|
||||
func (server *EchoVault) PExpire(key string, milliseconds int, options ...ExpireOptions) (bool, error) {
|
||||
cmd := []string{"PEXPIRE", key, strconv.Itoa(milliseconds)}
|
||||
|
||||
switch {
|
||||
case options.NX:
|
||||
cmd = append(cmd, "NX")
|
||||
case options.XX:
|
||||
cmd = append(cmd, "XX")
|
||||
case options.LT:
|
||||
cmd = append(cmd, "LT")
|
||||
case options.GT:
|
||||
cmd = append(cmd, "GT")
|
||||
for _, opt := range options {
|
||||
if opt != nil {
|
||||
cmd = append(cmd, fmt.Sprint(opt))
|
||||
}
|
||||
}
|
||||
|
||||
b, err := server.handleCommand(server.context, internal.EncodeCommand(cmd), nil, false, true)
|
||||
@@ -384,18 +408,13 @@ func (server *EchoVault) PExpire(key string, milliseconds int, options PExpireOp
|
||||
// `options` - ExpireAtOptions
|
||||
//
|
||||
// Returns: true if the key's expiry was successfully updated.
|
||||
func (server *EchoVault) ExpireAt(key string, unixSeconds int, options ExpireAtOptions) (int, error) {
|
||||
func (server *EchoVault) ExpireAt(key string, unixSeconds int, options ...ExpireOptions) (int, error) {
|
||||
cmd := []string{"EXPIREAT", key, strconv.Itoa(unixSeconds)}
|
||||
|
||||
switch {
|
||||
case options.NX:
|
||||
cmd = append(cmd, "NX")
|
||||
case options.XX:
|
||||
cmd = append(cmd, "XX")
|
||||
case options.LT:
|
||||
cmd = append(cmd, "LT")
|
||||
case options.GT:
|
||||
cmd = append(cmd, "GT")
|
||||
for _, opt := range options {
|
||||
if opt != nil {
|
||||
cmd = append(cmd, fmt.Sprint(opt))
|
||||
}
|
||||
}
|
||||
|
||||
b, err := server.handleCommand(server.context, internal.EncodeCommand(cmd), nil, false, true)
|
||||
@@ -418,18 +437,13 @@ func (server *EchoVault) ExpireAt(key string, unixSeconds int, options ExpireAtO
|
||||
// `options` - PExpireAtOptions
|
||||
//
|
||||
// Returns: true if the key's expiry was successfully updated.
|
||||
func (server *EchoVault) PExpireAt(key string, unixMilliseconds int, options PExpireAtOptions) (int, error) {
|
||||
func (server *EchoVault) PExpireAt(key string, unixMilliseconds int, options ...ExpireOptions) (int, error) {
|
||||
cmd := []string{"PEXPIREAT", key, strconv.Itoa(unixMilliseconds)}
|
||||
|
||||
switch {
|
||||
case options.NX:
|
||||
cmd = append(cmd, "NX")
|
||||
case options.XX:
|
||||
cmd = append(cmd, "XX")
|
||||
case options.LT:
|
||||
cmd = append(cmd, "LT")
|
||||
case options.GT:
|
||||
cmd = append(cmd, "GT")
|
||||
for _, opt := range options {
|
||||
if opt != nil {
|
||||
cmd = append(cmd, fmt.Sprint(opt))
|
||||
}
|
||||
}
|
||||
|
||||
b, err := server.handleCommand(server.context, internal.EncodeCommand(cmd), nil, false, true)
|
||||
@@ -607,37 +621,25 @@ func (server *EchoVault) GetDel(key string) (string, error) {
|
||||
//
|
||||
// `key` - string - the key whose value should be retrieved and expiry set.
|
||||
//
|
||||
// `opts` - GetExOptions.
|
||||
// `option` - GetExOption - one of EX, PX, EXAT, PXAT, PERSIST. Can be nil.
|
||||
//
|
||||
// `unixtime` - Number of seconds or miliseconds from now.
|
||||
//
|
||||
// Returns: A string representing the value at the specified key. If the value does not exist, an empty string is returned.
|
||||
func (server *EchoVault) GetEx(key string, opts GetExOptions) (string, error) {
|
||||
func (server *EchoVault) GetEx(key string, option GetExOption, unixtime int) (string, error) {
|
||||
|
||||
cmd := make([]string, 2)
|
||||
|
||||
cmd[0] = "GETEX"
|
||||
cmd[1] = key
|
||||
|
||||
var command string
|
||||
|
||||
switch {
|
||||
case opts.EX:
|
||||
command = "EX"
|
||||
case opts.PX:
|
||||
command = "PX"
|
||||
case opts.EXAT:
|
||||
command = "EXAT"
|
||||
case opts.PXAT:
|
||||
command = "PXAT"
|
||||
case opts.PERSIST:
|
||||
command = "PERSIST"
|
||||
default:
|
||||
if option != nil {
|
||||
opt := fmt.Sprint(option)
|
||||
cmd = append(cmd, opt)
|
||||
}
|
||||
|
||||
if command != "" {
|
||||
cmd = append(cmd, command)
|
||||
}
|
||||
if opts.UNIXTIME != 0 {
|
||||
cmd = append(cmd, strconv.Itoa(opts.UNIXTIME))
|
||||
if unixtime != 0 {
|
||||
cmd = append(cmd, strconv.Itoa(unixtime))
|
||||
}
|
||||
|
||||
b, err := server.handleCommand(server.context, internal.EncodeCommand(cmd), nil, false, true)
|
||||
|
@@ -82,7 +82,6 @@ func TestEchoVault_EXPIRE(t *testing.T) {
|
||||
key string
|
||||
time int
|
||||
expireOpts ExpireOptions
|
||||
pexpireOpts PExpireOptions
|
||||
want bool
|
||||
wantErr bool
|
||||
}{
|
||||
@@ -91,7 +90,7 @@ func TestEchoVault_EXPIRE(t *testing.T) {
|
||||
cmd: "EXPIRE",
|
||||
key: "key1",
|
||||
time: 100,
|
||||
expireOpts: ExpireOptions{},
|
||||
expireOpts: nil,
|
||||
presetValues: map[string]internal.KeyData{
|
||||
"key1": {Value: "value1", ExpireAt: time.Time{}},
|
||||
},
|
||||
@@ -99,11 +98,11 @@ func TestEchoVault_EXPIRE(t *testing.T) {
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "Set new expire by milliseconds",
|
||||
cmd: "PEXPIRE",
|
||||
key: "key2",
|
||||
time: 1000,
|
||||
pexpireOpts: PExpireOptions{},
|
||||
name: "Set new expire by milliseconds",
|
||||
cmd: "PEXPIRE",
|
||||
key: "key2",
|
||||
time: 1000,
|
||||
expireOpts: nil,
|
||||
presetValues: map[string]internal.KeyData{
|
||||
"key2": {Value: "value2", ExpireAt: time.Time{}},
|
||||
},
|
||||
@@ -115,7 +114,7 @@ func TestEchoVault_EXPIRE(t *testing.T) {
|
||||
cmd: "EXPIRE",
|
||||
key: "key3",
|
||||
time: 1000,
|
||||
expireOpts: ExpireOptions{NX: true},
|
||||
expireOpts: NX,
|
||||
presetValues: map[string]internal.KeyData{
|
||||
"key3": {Value: "value3", ExpireAt: time.Time{}},
|
||||
},
|
||||
@@ -127,7 +126,7 @@ func TestEchoVault_EXPIRE(t *testing.T) {
|
||||
cmd: "EXPIRE",
|
||||
key: "key4",
|
||||
time: 1000,
|
||||
expireOpts: ExpireOptions{NX: true},
|
||||
expireOpts: NX,
|
||||
presetValues: map[string]internal.KeyData{
|
||||
"key4": {Value: "value4", ExpireAt: mockClock.Now().Add(1000 * time.Second)},
|
||||
},
|
||||
@@ -139,7 +138,7 @@ func TestEchoVault_EXPIRE(t *testing.T) {
|
||||
cmd: "EXPIRE",
|
||||
key: "key5",
|
||||
time: 1000,
|
||||
expireOpts: ExpireOptions{XX: true},
|
||||
expireOpts: XX,
|
||||
presetValues: map[string]internal.KeyData{
|
||||
"key5": {Value: "value5", ExpireAt: mockClock.Now().Add(30 * time.Second)},
|
||||
},
|
||||
@@ -150,7 +149,7 @@ func TestEchoVault_EXPIRE(t *testing.T) {
|
||||
name: "Return false when key does not have an expiry and the XX flag is provided",
|
||||
cmd: "EXPIRE",
|
||||
time: 1000,
|
||||
expireOpts: ExpireOptions{XX: true},
|
||||
expireOpts: XX,
|
||||
key: "key6",
|
||||
presetValues: map[string]internal.KeyData{
|
||||
"key6": {Value: "value6", ExpireAt: time.Time{}},
|
||||
@@ -163,7 +162,7 @@ func TestEchoVault_EXPIRE(t *testing.T) {
|
||||
cmd: "EXPIRE",
|
||||
key: "key7",
|
||||
time: 100000,
|
||||
expireOpts: ExpireOptions{GT: true},
|
||||
expireOpts: GT,
|
||||
presetValues: map[string]internal.KeyData{
|
||||
"key7": {Value: "value7", ExpireAt: mockClock.Now().Add(30 * time.Second)},
|
||||
},
|
||||
@@ -175,7 +174,7 @@ func TestEchoVault_EXPIRE(t *testing.T) {
|
||||
cmd: "EXPIRE",
|
||||
key: "key8",
|
||||
time: 1000,
|
||||
expireOpts: ExpireOptions{GT: true},
|
||||
expireOpts: GT,
|
||||
presetValues: map[string]internal.KeyData{
|
||||
"key8": {Value: "value8", ExpireAt: mockClock.Now().Add(3000 * time.Second)},
|
||||
},
|
||||
@@ -187,7 +186,7 @@ func TestEchoVault_EXPIRE(t *testing.T) {
|
||||
cmd: "EXPIRE",
|
||||
key: "key9",
|
||||
time: 1000,
|
||||
expireOpts: ExpireOptions{GT: true},
|
||||
expireOpts: GT,
|
||||
presetValues: map[string]internal.KeyData{
|
||||
"key9": {Value: "value9", ExpireAt: time.Time{}},
|
||||
},
|
||||
@@ -199,7 +198,7 @@ func TestEchoVault_EXPIRE(t *testing.T) {
|
||||
cmd: "EXPIRE",
|
||||
key: "key10",
|
||||
time: 1000,
|
||||
expireOpts: ExpireOptions{LT: true},
|
||||
expireOpts: LT,
|
||||
presetValues: map[string]internal.KeyData{
|
||||
"key10": {Value: "value10", ExpireAt: mockClock.Now().Add(3000 * time.Second)},
|
||||
},
|
||||
@@ -211,7 +210,7 @@ func TestEchoVault_EXPIRE(t *testing.T) {
|
||||
cmd: "EXPIRE",
|
||||
key: "key11",
|
||||
time: 50000,
|
||||
expireOpts: ExpireOptions{LT: true},
|
||||
expireOpts: LT,
|
||||
presetValues: map[string]internal.KeyData{
|
||||
"key11": {Value: "value11", ExpireAt: mockClock.Now().Add(30 * time.Second)},
|
||||
},
|
||||
@@ -229,7 +228,7 @@ func TestEchoVault_EXPIRE(t *testing.T) {
|
||||
var got bool
|
||||
var err error
|
||||
if strings.EqualFold(tt.cmd, "PEXPIRE") {
|
||||
got, err = server.PExpire(tt.key, tt.time, tt.pexpireOpts)
|
||||
got, err = server.PExpire(tt.key, tt.time, tt.expireOpts)
|
||||
} else {
|
||||
got, err = server.Expire(tt.key, tt.time, tt.expireOpts)
|
||||
}
|
||||
@@ -250,21 +249,20 @@ func TestEchoVault_EXPIREAT(t *testing.T) {
|
||||
server := createEchoVault()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
presetValues map[string]internal.KeyData
|
||||
cmd string
|
||||
key string
|
||||
time int
|
||||
expireAtOpts ExpireAtOptions
|
||||
pexpireAtOpts PExpireAtOptions
|
||||
want int
|
||||
wantErr bool
|
||||
name string
|
||||
presetValues map[string]internal.KeyData
|
||||
cmd string
|
||||
key string
|
||||
time int
|
||||
expireAtOpts ExpireOptions
|
||||
want int
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "Set new expire by unix seconds",
|
||||
cmd: "EXPIREAT",
|
||||
key: "key1",
|
||||
expireAtOpts: ExpireAtOptions{},
|
||||
expireAtOpts: nil,
|
||||
time: int(mockClock.Now().Add(1000 * time.Second).Unix()),
|
||||
presetValues: map[string]internal.KeyData{
|
||||
"key1": {Value: "value1", ExpireAt: time.Time{}},
|
||||
@@ -273,11 +271,11 @@ func TestEchoVault_EXPIREAT(t *testing.T) {
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "Set new expire by milliseconds",
|
||||
cmd: "PEXPIREAT",
|
||||
key: "key2",
|
||||
pexpireAtOpts: PExpireAtOptions{},
|
||||
time: int(mockClock.Now().Add(1000 * time.Second).UnixMilli()),
|
||||
name: "Set new expire by milliseconds",
|
||||
cmd: "PEXPIREAT",
|
||||
key: "key2",
|
||||
expireAtOpts: nil,
|
||||
time: int(mockClock.Now().Add(1000 * time.Second).UnixMilli()),
|
||||
presetValues: map[string]internal.KeyData{
|
||||
"key2": {Value: "value2", ExpireAt: time.Time{}},
|
||||
},
|
||||
@@ -289,7 +287,7 @@ func TestEchoVault_EXPIREAT(t *testing.T) {
|
||||
cmd: "EXPIREAT",
|
||||
key: "key3",
|
||||
time: int(mockClock.Now().Add(1000 * time.Second).Unix()),
|
||||
expireAtOpts: ExpireAtOptions{NX: true},
|
||||
expireAtOpts: NX,
|
||||
presetValues: map[string]internal.KeyData{
|
||||
"key3": {Value: "value3", ExpireAt: time.Time{}},
|
||||
},
|
||||
@@ -300,7 +298,7 @@ func TestEchoVault_EXPIREAT(t *testing.T) {
|
||||
name: "Return 0, when NX flag is provided and key already has an expiry time",
|
||||
cmd: "EXPIREAT",
|
||||
time: int(mockClock.Now().Add(1000 * time.Second).Unix()),
|
||||
expireAtOpts: ExpireAtOptions{NX: true},
|
||||
expireAtOpts: NX,
|
||||
key: "key4",
|
||||
presetValues: map[string]internal.KeyData{
|
||||
"key4": {Value: "value4", ExpireAt: mockClock.Now().Add(1000 * time.Second)},
|
||||
@@ -313,7 +311,7 @@ func TestEchoVault_EXPIREAT(t *testing.T) {
|
||||
cmd: "EXPIREAT",
|
||||
time: int(mockClock.Now().Add(1000 * time.Second).Unix()),
|
||||
key: "key5",
|
||||
expireAtOpts: ExpireAtOptions{XX: true},
|
||||
expireAtOpts: XX,
|
||||
presetValues: map[string]internal.KeyData{
|
||||
"key5": {Value: "value5", ExpireAt: mockClock.Now().Add(30 * time.Second)},
|
||||
},
|
||||
@@ -325,7 +323,7 @@ func TestEchoVault_EXPIREAT(t *testing.T) {
|
||||
cmd: "EXPIREAT",
|
||||
key: "key6",
|
||||
time: int(mockClock.Now().Add(1000 * time.Second).Unix()),
|
||||
expireAtOpts: ExpireAtOptions{XX: true},
|
||||
expireAtOpts: XX,
|
||||
presetValues: map[string]internal.KeyData{
|
||||
"key6": {Value: "value6", ExpireAt: time.Time{}},
|
||||
},
|
||||
@@ -337,7 +335,7 @@ func TestEchoVault_EXPIREAT(t *testing.T) {
|
||||
cmd: "EXPIREAT",
|
||||
key: "key7",
|
||||
time: int(mockClock.Now().Add(1000 * time.Second).Unix()),
|
||||
expireAtOpts: ExpireAtOptions{GT: true},
|
||||
expireAtOpts: GT,
|
||||
presetValues: map[string]internal.KeyData{
|
||||
"key7": {Value: "value7", ExpireAt: mockClock.Now().Add(30 * time.Second)},
|
||||
},
|
||||
@@ -349,7 +347,7 @@ func TestEchoVault_EXPIREAT(t *testing.T) {
|
||||
cmd: "EXPIREAT",
|
||||
key: "key8",
|
||||
time: int(mockClock.Now().Add(1000 * time.Second).Unix()),
|
||||
expireAtOpts: ExpireAtOptions{GT: true},
|
||||
expireAtOpts: GT,
|
||||
presetValues: map[string]internal.KeyData{
|
||||
"key8": {Value: "value8", ExpireAt: mockClock.Now().Add(3000 * time.Second)},
|
||||
},
|
||||
@@ -361,7 +359,7 @@ func TestEchoVault_EXPIREAT(t *testing.T) {
|
||||
cmd: "EXPIREAT",
|
||||
key: "key9",
|
||||
time: int(mockClock.Now().Add(1000 * time.Second).Unix()),
|
||||
expireAtOpts: ExpireAtOptions{GT: true},
|
||||
expireAtOpts: GT,
|
||||
presetValues: map[string]internal.KeyData{
|
||||
"key9": {Value: "value9", ExpireAt: time.Time{}},
|
||||
},
|
||||
@@ -372,7 +370,7 @@ func TestEchoVault_EXPIREAT(t *testing.T) {
|
||||
cmd: "EXPIREAT",
|
||||
key: "key10",
|
||||
time: int(mockClock.Now().Add(1000 * time.Second).Unix()),
|
||||
expireAtOpts: ExpireAtOptions{LT: true},
|
||||
expireAtOpts: LT,
|
||||
presetValues: map[string]internal.KeyData{
|
||||
"key10": {Value: "value10", ExpireAt: mockClock.Now().Add(3000 * time.Second)},
|
||||
},
|
||||
@@ -384,7 +382,7 @@ func TestEchoVault_EXPIREAT(t *testing.T) {
|
||||
cmd: "EXPIREAT",
|
||||
key: "key11",
|
||||
time: int(mockClock.Now().Add(3000 * time.Second).Unix()),
|
||||
expireAtOpts: ExpireAtOptions{LT: true},
|
||||
expireAtOpts: LT,
|
||||
presetValues: map[string]internal.KeyData{
|
||||
"key11": {Value: "value11", ExpireAt: mockClock.Now().Add(1000 * time.Second)},
|
||||
},
|
||||
@@ -396,7 +394,7 @@ func TestEchoVault_EXPIREAT(t *testing.T) {
|
||||
cmd: "EXPIREAT",
|
||||
key: "key12",
|
||||
time: int(mockClock.Now().Add(1000 * time.Second).Unix()),
|
||||
expireAtOpts: ExpireAtOptions{LT: true},
|
||||
expireAtOpts: LT,
|
||||
presetValues: map[string]internal.KeyData{
|
||||
"key12": {Value: "value12", ExpireAt: time.Time{}},
|
||||
},
|
||||
@@ -414,7 +412,7 @@ func TestEchoVault_EXPIREAT(t *testing.T) {
|
||||
var got int
|
||||
var err error
|
||||
if strings.EqualFold(tt.cmd, "PEXPIREAT") {
|
||||
got, err = server.PExpireAt(tt.key, tt.time, tt.pexpireAtOpts)
|
||||
got, err = server.PExpireAt(tt.key, tt.time, tt.expireAtOpts)
|
||||
} else {
|
||||
got, err = server.ExpireAt(tt.key, tt.time, tt.expireAtOpts)
|
||||
}
|
||||
@@ -606,12 +604,21 @@ func TestEchoVault_SET(t *testing.T) {
|
||||
|
||||
server := createEchoVault()
|
||||
|
||||
SetOptions := func(W SetWriteOption, EX SetExOption, EXTIME int, GET bool) SETOptions {
|
||||
return SETOptions{
|
||||
WriteOpt: W,
|
||||
ExpireOpt: EX,
|
||||
ExpireTime: EXTIME,
|
||||
Get: GET,
|
||||
}
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
presetValues map[string]internal.KeyData
|
||||
key string
|
||||
value string
|
||||
options SetOptions
|
||||
options SETOptions
|
||||
wantOk bool
|
||||
wantPrev string
|
||||
wantErr bool
|
||||
@@ -621,7 +628,7 @@ func TestEchoVault_SET(t *testing.T) {
|
||||
presetValues: nil,
|
||||
key: "key1",
|
||||
value: "value1",
|
||||
options: SetOptions{},
|
||||
options: SetOptions(nil, nil, 0, false),
|
||||
wantOk: true,
|
||||
wantPrev: "",
|
||||
wantErr: false,
|
||||
@@ -631,7 +638,7 @@ func TestEchoVault_SET(t *testing.T) {
|
||||
presetValues: nil,
|
||||
key: "key2",
|
||||
value: "value2",
|
||||
options: SetOptions{NX: true},
|
||||
options: SetOptions(SETNX, nil, 0, false),
|
||||
wantOk: true,
|
||||
wantPrev: "",
|
||||
wantErr: false,
|
||||
@@ -646,7 +653,7 @@ func TestEchoVault_SET(t *testing.T) {
|
||||
},
|
||||
key: "key3",
|
||||
value: "value3",
|
||||
options: SetOptions{NX: true},
|
||||
options: SetOptions(SETNX, nil, 0, false),
|
||||
wantOk: false,
|
||||
wantPrev: "",
|
||||
wantErr: true,
|
||||
@@ -661,7 +668,7 @@ func TestEchoVault_SET(t *testing.T) {
|
||||
},
|
||||
key: "key4",
|
||||
value: "value4",
|
||||
options: SetOptions{XX: true},
|
||||
options: SetOptions(SETXX, nil, 0, false),
|
||||
wantOk: true,
|
||||
wantPrev: "",
|
||||
wantErr: false,
|
||||
@@ -671,7 +678,7 @@ func TestEchoVault_SET(t *testing.T) {
|
||||
presetValues: nil,
|
||||
key: "key5",
|
||||
value: "value5",
|
||||
options: SetOptions{XX: true},
|
||||
options: SetOptions(SETXX, nil, 0, false),
|
||||
wantOk: false,
|
||||
wantPrev: "",
|
||||
wantErr: true,
|
||||
@@ -681,7 +688,7 @@ func TestEchoVault_SET(t *testing.T) {
|
||||
presetValues: nil,
|
||||
key: "key6",
|
||||
value: "value6",
|
||||
options: SetOptions{EX: 100},
|
||||
options: SetOptions(nil, SETEX, 100, false),
|
||||
wantOk: true,
|
||||
wantPrev: "",
|
||||
wantErr: false,
|
||||
@@ -691,7 +698,7 @@ func TestEchoVault_SET(t *testing.T) {
|
||||
presetValues: nil,
|
||||
key: "key7",
|
||||
value: "value7",
|
||||
options: SetOptions{PX: 4096},
|
||||
options: SetOptions(nil, SETPX, 4096, false),
|
||||
wantOk: true,
|
||||
wantPrev: "",
|
||||
wantErr: false,
|
||||
@@ -701,7 +708,7 @@ func TestEchoVault_SET(t *testing.T) {
|
||||
presetValues: nil,
|
||||
key: "key8",
|
||||
value: "value8",
|
||||
options: SetOptions{EXAT: int(mockClock.Now().Add(200 * time.Second).Unix())},
|
||||
options: SetOptions(nil, SETEXAT, int(mockClock.Now().Add(200*time.Second).Unix()), false),
|
||||
wantOk: true,
|
||||
wantPrev: "",
|
||||
wantErr: false,
|
||||
@@ -710,7 +717,7 @@ func TestEchoVault_SET(t *testing.T) {
|
||||
name: "Set exact expiry time in milliseconds from unix epoch",
|
||||
key: "key9",
|
||||
value: "value9",
|
||||
options: SetOptions{PXAT: int(mockClock.Now().Add(4096 * time.Millisecond).UnixMilli())},
|
||||
options: SetOptions(nil, SETPXAT, int(mockClock.Now().Add(4096*time.Millisecond).UnixMilli()), false),
|
||||
presetValues: nil,
|
||||
wantOk: true,
|
||||
wantPrev: "",
|
||||
@@ -726,7 +733,7 @@ func TestEchoVault_SET(t *testing.T) {
|
||||
},
|
||||
key: "key10",
|
||||
value: "value10",
|
||||
options: SetOptions{GET: true, EX: 1000},
|
||||
options: SetOptions(nil, SETEX, 1000, true),
|
||||
wantOk: true,
|
||||
wantPrev: "previous-value",
|
||||
wantErr: false,
|
||||
@@ -736,7 +743,7 @@ func TestEchoVault_SET(t *testing.T) {
|
||||
presetValues: nil,
|
||||
key: "key11",
|
||||
value: "value11",
|
||||
options: SetOptions{GET: true, EX: 1000},
|
||||
options: SetOptions(nil, SETEX, 1000, true),
|
||||
wantOk: true,
|
||||
wantPrev: "",
|
||||
wantErr: false,
|
||||
@@ -749,7 +756,11 @@ func TestEchoVault_SET(t *testing.T) {
|
||||
presetKeyData(server, context.Background(), k, d)
|
||||
}
|
||||
}
|
||||
previousValue, ok, err := server.Set(tt.key, tt.value, tt.options)
|
||||
previousValue, ok, err := server.Set(
|
||||
tt.key,
|
||||
tt.value,
|
||||
tt.options,
|
||||
)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("SET() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
@@ -1408,90 +1419,97 @@ func TestEchoVault_GETEX(t *testing.T) {
|
||||
server := createEchoVault()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
presetValue interface{}
|
||||
getExOpts GetExOptions
|
||||
key string
|
||||
want string
|
||||
wantEx int
|
||||
wantErr bool
|
||||
name string
|
||||
presetValue interface{}
|
||||
getExOpt GetExOption
|
||||
getExOptTime int
|
||||
key string
|
||||
want string
|
||||
wantEx int
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "1. Return string from existing key, no expire options",
|
||||
presetValue: "value1",
|
||||
getExOpts: GetExOptions{},
|
||||
getExOpt: nil,
|
||||
key: "key1",
|
||||
want: "value1",
|
||||
wantEx: -1,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "2. Return empty string if the key does not exist",
|
||||
presetValue: nil,
|
||||
getExOpts: GetExOptions{EX: true, UNIXTIME: int(mockClock.Now().Add(100 * time.Second).Unix())},
|
||||
key: "key2",
|
||||
want: "",
|
||||
wantEx: 0,
|
||||
wantErr: false,
|
||||
name: "2. Return empty string if the key does not exist",
|
||||
presetValue: nil,
|
||||
getExOpt: EX,
|
||||
getExOptTime: int(mockClock.Now().Add(100 * time.Second).Unix()),
|
||||
key: "key2",
|
||||
want: "",
|
||||
wantEx: 0,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "3. Return key set expiry with EX",
|
||||
presetValue: "value3",
|
||||
getExOpts: GetExOptions{EX: true, UNIXTIME: 100},
|
||||
key: "key3",
|
||||
want: "value3",
|
||||
wantEx: 100,
|
||||
wantErr: false,
|
||||
name: "3. Return key set expiry with EX",
|
||||
presetValue: "value3",
|
||||
getExOpt: EX,
|
||||
getExOptTime: 100,
|
||||
key: "key3",
|
||||
want: "value3",
|
||||
wantEx: 100,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "4. Return key set expiry with PX",
|
||||
presetValue: "value4",
|
||||
getExOpts: GetExOptions{PX: true, UNIXTIME: 100000},
|
||||
key: "key4",
|
||||
want: "value4",
|
||||
wantEx: 100,
|
||||
wantErr: false,
|
||||
name: "4. Return key set expiry with PX",
|
||||
presetValue: "value4",
|
||||
getExOpt: PX,
|
||||
getExOptTime: 100000,
|
||||
key: "key4",
|
||||
want: "value4",
|
||||
wantEx: 100,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "5. Return key set expiry with EXAT",
|
||||
presetValue: "value5",
|
||||
getExOpts: GetExOptions{EXAT: true, UNIXTIME: int(mockClock.Now().Add(100 * time.Second).Unix())},
|
||||
key: "key5",
|
||||
want: "value5",
|
||||
wantEx: 100,
|
||||
wantErr: false,
|
||||
name: "5. Return key set expiry with EXAT",
|
||||
presetValue: "value5",
|
||||
getExOpt: EXAT,
|
||||
getExOptTime: int(mockClock.Now().Add(100 * time.Second).Unix()),
|
||||
key: "key5",
|
||||
want: "value5",
|
||||
wantEx: 100,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "6. Return key set expiry with PXAT",
|
||||
presetValue: "value6",
|
||||
getExOpts: GetExOptions{PXAT: true, UNIXTIME: int(mockClock.Now().Add(100 * time.Second).UnixMilli())},
|
||||
key: "key6",
|
||||
want: "value6",
|
||||
wantEx: 100,
|
||||
wantErr: false,
|
||||
name: "6. Return key set expiry with PXAT",
|
||||
presetValue: "value6",
|
||||
getExOpt: PXAT,
|
||||
getExOptTime: int(mockClock.Now().Add(100 * time.Second).UnixMilli()),
|
||||
key: "key6",
|
||||
want: "value6",
|
||||
wantEx: 100,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "7. Return key passing PERSIST",
|
||||
presetValue: "value7",
|
||||
getExOpts: GetExOptions{PERSIST: true},
|
||||
getExOpt: PERSIST,
|
||||
key: "key7",
|
||||
want: "value7",
|
||||
wantEx: -1,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "8. Return key passing PERSIST, and include a UNIXTIME",
|
||||
presetValue: "value8",
|
||||
getExOpts: GetExOptions{PERSIST: true, UNIXTIME: int(mockClock.Now().Add(100 * time.Second).Unix())},
|
||||
key: "key8",
|
||||
want: "value8",
|
||||
wantEx: -1,
|
||||
wantErr: false,
|
||||
name: "8. Return key passing PERSIST, and include a UNIXTIME",
|
||||
presetValue: "value8",
|
||||
getExOpt: PERSIST,
|
||||
getExOptTime: int(mockClock.Now().Add(100 * time.Second).Unix()),
|
||||
key: "key8",
|
||||
want: "value8",
|
||||
wantEx: -1,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "9. Return key and attempt to set expiry with EX without providing UNIXTIME",
|
||||
presetValue: "value9",
|
||||
getExOpts: GetExOptions{EX: true},
|
||||
getExOpt: EX,
|
||||
key: "key9",
|
||||
want: "value9",
|
||||
wantEx: -1,
|
||||
@@ -1500,7 +1518,7 @@ func TestEchoVault_GETEX(t *testing.T) {
|
||||
{
|
||||
name: "10. Return key and attempt to set expiry with PXAT without providing UNIXTIME",
|
||||
presetValue: "value10",
|
||||
getExOpts: GetExOptions{PXAT: true},
|
||||
getExOpt: PXAT,
|
||||
key: "key10",
|
||||
want: "value10",
|
||||
wantEx: -1,
|
||||
@@ -1516,8 +1534,8 @@ func TestEchoVault_GETEX(t *testing.T) {
|
||||
return
|
||||
}
|
||||
}
|
||||
// Check value received
|
||||
got, err := server.GetEx(tt.key, tt.getExOpts)
|
||||
//Check value received
|
||||
got, err := server.GetEx(tt.key, tt.getExOpt, tt.getExOptTime)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("GETEX() GET error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
|
@@ -311,7 +311,7 @@ func Test_Cluster(t *testing.T) {
|
||||
// Write all the data to the cluster leader.
|
||||
for i, test := range tests {
|
||||
node := nodes[0]
|
||||
_, ok, err := node.server.Set(test.key, test.value, SetOptions{})
|
||||
_, ok, err := node.server.Set(test.key, test.value, SETOptions{})
|
||||
if err != nil {
|
||||
t.Errorf("could not write command to leader node (test %d): %v", i, err)
|
||||
}
|
||||
@@ -411,7 +411,7 @@ func Test_Cluster(t *testing.T) {
|
||||
// Write all the data to the cluster leader.
|
||||
for i, test := range tests {
|
||||
node := nodes[0]
|
||||
_, ok, err := node.server.Set(test.key, test.value, SetOptions{})
|
||||
_, ok, err := node.server.Set(test.key, test.value, SETOptions{})
|
||||
if err != nil {
|
||||
t.Errorf("could not write command to leader node (test %d): %v", i, err)
|
||||
}
|
||||
@@ -999,7 +999,7 @@ func Test_Standalone(t *testing.T) {
|
||||
for database, data := range test.values {
|
||||
_ = mockServer.SelectDB(database)
|
||||
for key, value := range data {
|
||||
if _, _, err = mockServer.Set(key, value, SetOptions{}); err != nil {
|
||||
if _, _, err = mockServer.Set(key, value, SETOptions{}); err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
@@ -1101,7 +1101,7 @@ func Test_Standalone(t *testing.T) {
|
||||
|
||||
// Perform write commands from "before-rewrite"
|
||||
for key, value := range data["before-rewrite"] {
|
||||
if _, _, err := mockServer.Set(key, value, SetOptions{}); err != nil {
|
||||
if _, _, err := mockServer.Set(key, value, SETOptions{}); err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
@@ -1118,7 +1118,7 @@ func Test_Standalone(t *testing.T) {
|
||||
|
||||
// Perform write commands from "after-rewrite"
|
||||
for key, value := range data["after-rewrite"] {
|
||||
if _, _, err := mockServer.Set(key, value, SetOptions{}); err != nil {
|
||||
if _, _, err := mockServer.Set(key, value, SETOptions{}); err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
@@ -776,7 +776,7 @@ func Test_AdminCommands(t *testing.T) {
|
||||
|
||||
// Trigger some write commands
|
||||
for key, value := range test.values {
|
||||
if _, _, err = mockServer.Set(key, value, echovault.SetOptions{}); err != nil {
|
||||
if _, _, err = mockServer.Set(key, value, echovault.SETOptions{}); err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
@@ -2662,7 +2662,7 @@ func Test_Generic(t *testing.T) {
|
||||
_, _, _ = mockServer.Set(
|
||||
fmt.Sprintf("key%d", k),
|
||||
fmt.Sprintf("value%d", k),
|
||||
echovault.SetOptions{},
|
||||
echovault.SETOptions{},
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -2768,7 +2768,7 @@ func Test_Generic(t *testing.T) {
|
||||
_, _, err := mockServer.Set(
|
||||
fmt.Sprintf("RandomKey%d", i),
|
||||
fmt.Sprintf("Value%d", i),
|
||||
echovault.SetOptions{},
|
||||
echovault.SETOptions{},
|
||||
)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
|
Reference in New Issue
Block a user