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:
osteensco
2024-09-20 10:53:39 -05:00
committed by GitHub
parent cc086a9504
commit 016bd2d2bd
14 changed files with 7077 additions and 1881 deletions

File diff suppressed because it is too large Load Diff

1
docs/.gitignore vendored
View File

@@ -8,6 +8,7 @@
.docusaurus .docusaurus
.cache-loader .cache-loader
.next .next
package-lock.json
# Misc # Misc
.DS_Store .DS_Store

View File

@@ -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. - `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. - `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. - `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 ### Examples
@@ -36,13 +38,21 @@ Expire the key in the specified number of seconds. This commands turns a key int
]} ]}
> >
<TabItem value="go"> <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 ```go
vault, err := echovault.NewEchoVault() vault, err := echovault.NewEchoVault()
if err != nil { if err != nil {
log.Fatal(err) 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: 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 { if err != nil {
log.Fatal(err) 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: 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 { if err != nil {
log.Fatal(err) 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: 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 { if err != nil {
log.Fatal(err) 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>
<TabItem value="cli"> <TabItem value="cli">

View 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>

View File

@@ -35,13 +35,21 @@ Expire the key in the specified number of milliseconds. This commands turns a ke
]} ]}
> >
<TabItem value="go"> <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: Add an expiration to a key:
```go ```go
vault, err := echovault.NewEchoVault() vault, err := echovault.NewEchoVault()
if err != nil { if err != nil {
log.Fatal(err) 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: 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 { if err != nil {
log.Fatal(err) 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: 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 { if err != nil {
log.Fatal(err) 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: 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 { if err != nil {
log.Fatal(err) 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>
<TabItem value="cli"> <TabItem value="cli">

View 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>

View File

@@ -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). - `PXAT` - Expire at the exat time in unix milliseconds (positive integer).
### Examples ### Examples
<Tabs <Tabs
@@ -38,13 +39,35 @@ Set the value of a key, considering the value's type. If the key already exists,
]} ]}
> >
<TabItem value="go"> <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: Set a value at a key:
```go ```go
vault, err := echovault.NewEchoVault() vault, err := echovault.NewEchoVault()
if err != nil { if err != nil {
log.Fatal(err) 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: 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 { if err != nil {
log.Fatal(err) 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: 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 { if err != nil {
log.Fatal(err) 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: 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 { if err != nil {
log.Fatal(err) 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>
<TabItem value="cli"> <TabItem value="cli">

View File

@@ -29,6 +29,23 @@ const config: Config = {
locales: ["en"], 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: [ presets: [
[ [
"classic", "classic",

View File

@@ -4168,11 +4168,6 @@ fs.realpath@^1.0.0:
resolved "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz" resolved "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz"
integrity sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw== 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: function-bind@^1.1.2:
version "1.1.2" version "1.1.2"
resolved "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz" resolved "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz"

View File

@@ -15,42 +15,84 @@
package echovault package echovault
import ( import (
"fmt"
"strconv" "strconv"
"strings" "strings"
"github.com/echovault/echovault/internal" "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. // SETXX - Only set if the key exists.
// type SetWriteOption interface {
// GET - Return the old value stored at key, or nil if the value does not exist. IsSetWriteOpt() SetWriteOpt
//
// 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
} }
// 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. // 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. // 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. // LT - Only set the expiry time if the new expiry time is less than the current one.
type ExpireOptions struct { //
NX bool // NX, GT, and LT are mutually exclusive. XX can additionally be passed in with either GT or LT.
XX bool type ExpireOptions interface {
LT bool IsExOpt() ExOpt
GT bool
} }
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. // 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. // PXAT - Set the specified Unix time at which the key will expire, in milliseconds.
// //
// PERSIST - Remove the time to live associated with the key. // PERSIST - Remove the time to live associated with the key.
// type GetExOption interface {
// UNIXTIME - Number of seconds or miliseconds from now isGetExOpt() GetExOpt
type GetExOptions struct {
EX bool
PX bool
EXAT bool
PXAT bool
PERSIST bool
UNIXTIME int
} }
func (x GetExOpt) isGetExOpt() GetExOpt { return x }
// Set creates or modifies the value at the given key. // Set creates or modifies the value at the given key.
// //
// Parameters: // Parameters:
@@ -99,8 +145,6 @@ type GetExOptions struct {
// //
// `value` - string - the value to place at the key. // `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. // Returns: true if the set is successful, If the "Get" flag in SetOptions is set to true, the previous value is returned.
// //
// Errors: // 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 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. // "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} cmd := []string{"SET", key, value}
switch { if options.WriteOpt != nil {
case options.NX: cmd = append(cmd, fmt.Sprint(options.WriteOpt))
cmd = append(cmd, "NX")
case options.XX:
cmd = append(cmd, "XX")
} }
switch { if options.ExpireOpt != nil {
case options.EX != 0: cmd = append(cmd, []string{fmt.Sprint(options.ExpireOpt), strconv.Itoa(options.ExpireTime)}...)
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.GET { if options.Get {
cmd = append(cmd, "GET") cmd = append(cmd, "GET")
} }
@@ -142,7 +176,7 @@ func (server *EchoVault) Set(key, value string, options SetOptions) (string, boo
if err != nil { if err != nil {
return "", false, err return "", false, err
} }
if !options.GET { if !options.Get {
previousValue = "" previousValue = ""
} }
@@ -313,21 +347,16 @@ func (server *EchoVault) PTTL(key string) (int, error) {
// //
// `seconds` - int - number of seconds from now. // `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. // 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)} cmd := []string{"EXPIRE", key, strconv.Itoa(seconds)}
switch { for _, opt := range options {
case options.NX: if opt != nil {
cmd = append(cmd, "NX") cmd = append(cmd, fmt.Sprint(opt))
case options.XX: }
cmd = append(cmd, "XX")
case options.LT:
cmd = append(cmd, "LT")
case options.GT:
cmd = append(cmd, "GT")
} }
b, err := server.handleCommand(server.context, internal.EncodeCommand(cmd), nil, false, true) 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 // `options` - PExpireOptions
// //
// Returns: true if the key's expiry was successfully updated. // 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)} cmd := []string{"PEXPIRE", key, strconv.Itoa(milliseconds)}
switch { for _, opt := range options {
case options.NX: if opt != nil {
cmd = append(cmd, "NX") cmd = append(cmd, fmt.Sprint(opt))
case options.XX: }
cmd = append(cmd, "XX")
case options.LT:
cmd = append(cmd, "LT")
case options.GT:
cmd = append(cmd, "GT")
} }
b, err := server.handleCommand(server.context, internal.EncodeCommand(cmd), nil, false, true) 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 // `options` - ExpireAtOptions
// //
// Returns: true if the key's expiry was successfully updated. // 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)} cmd := []string{"EXPIREAT", key, strconv.Itoa(unixSeconds)}
switch { for _, opt := range options {
case options.NX: if opt != nil {
cmd = append(cmd, "NX") cmd = append(cmd, fmt.Sprint(opt))
case options.XX: }
cmd = append(cmd, "XX")
case options.LT:
cmd = append(cmd, "LT")
case options.GT:
cmd = append(cmd, "GT")
} }
b, err := server.handleCommand(server.context, internal.EncodeCommand(cmd), nil, false, true) 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 // `options` - PExpireAtOptions
// //
// Returns: true if the key's expiry was successfully updated. // 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)} cmd := []string{"PEXPIREAT", key, strconv.Itoa(unixMilliseconds)}
switch { for _, opt := range options {
case options.NX: if opt != nil {
cmd = append(cmd, "NX") cmd = append(cmd, fmt.Sprint(opt))
case options.XX: }
cmd = append(cmd, "XX")
case options.LT:
cmd = append(cmd, "LT")
case options.GT:
cmd = append(cmd, "GT")
} }
b, err := server.handleCommand(server.context, internal.EncodeCommand(cmd), nil, false, true) 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. // `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. // 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 := make([]string, 2)
cmd[0] = "GETEX" cmd[0] = "GETEX"
cmd[1] = key cmd[1] = key
var command string if option != nil {
opt := fmt.Sprint(option)
switch { cmd = append(cmd, opt)
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 command != "" { if unixtime != 0 {
cmd = append(cmd, command) cmd = append(cmd, strconv.Itoa(unixtime))
}
if opts.UNIXTIME != 0 {
cmd = append(cmd, strconv.Itoa(opts.UNIXTIME))
} }
b, err := server.handleCommand(server.context, internal.EncodeCommand(cmd), nil, false, true) b, err := server.handleCommand(server.context, internal.EncodeCommand(cmd), nil, false, true)

View File

@@ -82,7 +82,6 @@ func TestEchoVault_EXPIRE(t *testing.T) {
key string key string
time int time int
expireOpts ExpireOptions expireOpts ExpireOptions
pexpireOpts PExpireOptions
want bool want bool
wantErr bool wantErr bool
}{ }{
@@ -91,7 +90,7 @@ func TestEchoVault_EXPIRE(t *testing.T) {
cmd: "EXPIRE", cmd: "EXPIRE",
key: "key1", key: "key1",
time: 100, time: 100,
expireOpts: ExpireOptions{}, expireOpts: nil,
presetValues: map[string]internal.KeyData{ presetValues: map[string]internal.KeyData{
"key1": {Value: "value1", ExpireAt: time.Time{}}, "key1": {Value: "value1", ExpireAt: time.Time{}},
}, },
@@ -99,11 +98,11 @@ func TestEchoVault_EXPIRE(t *testing.T) {
wantErr: false, wantErr: false,
}, },
{ {
name: "Set new expire by milliseconds", name: "Set new expire by milliseconds",
cmd: "PEXPIRE", cmd: "PEXPIRE",
key: "key2", key: "key2",
time: 1000, time: 1000,
pexpireOpts: PExpireOptions{}, expireOpts: nil,
presetValues: map[string]internal.KeyData{ presetValues: map[string]internal.KeyData{
"key2": {Value: "value2", ExpireAt: time.Time{}}, "key2": {Value: "value2", ExpireAt: time.Time{}},
}, },
@@ -115,7 +114,7 @@ func TestEchoVault_EXPIRE(t *testing.T) {
cmd: "EXPIRE", cmd: "EXPIRE",
key: "key3", key: "key3",
time: 1000, time: 1000,
expireOpts: ExpireOptions{NX: true}, expireOpts: NX,
presetValues: map[string]internal.KeyData{ presetValues: map[string]internal.KeyData{
"key3": {Value: "value3", ExpireAt: time.Time{}}, "key3": {Value: "value3", ExpireAt: time.Time{}},
}, },
@@ -127,7 +126,7 @@ func TestEchoVault_EXPIRE(t *testing.T) {
cmd: "EXPIRE", cmd: "EXPIRE",
key: "key4", key: "key4",
time: 1000, time: 1000,
expireOpts: ExpireOptions{NX: true}, expireOpts: NX,
presetValues: map[string]internal.KeyData{ presetValues: map[string]internal.KeyData{
"key4": {Value: "value4", ExpireAt: mockClock.Now().Add(1000 * time.Second)}, "key4": {Value: "value4", ExpireAt: mockClock.Now().Add(1000 * time.Second)},
}, },
@@ -139,7 +138,7 @@ func TestEchoVault_EXPIRE(t *testing.T) {
cmd: "EXPIRE", cmd: "EXPIRE",
key: "key5", key: "key5",
time: 1000, time: 1000,
expireOpts: ExpireOptions{XX: true}, expireOpts: XX,
presetValues: map[string]internal.KeyData{ presetValues: map[string]internal.KeyData{
"key5": {Value: "value5", ExpireAt: mockClock.Now().Add(30 * time.Second)}, "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", name: "Return false when key does not have an expiry and the XX flag is provided",
cmd: "EXPIRE", cmd: "EXPIRE",
time: 1000, time: 1000,
expireOpts: ExpireOptions{XX: true}, expireOpts: XX,
key: "key6", key: "key6",
presetValues: map[string]internal.KeyData{ presetValues: map[string]internal.KeyData{
"key6": {Value: "value6", ExpireAt: time.Time{}}, "key6": {Value: "value6", ExpireAt: time.Time{}},
@@ -163,7 +162,7 @@ func TestEchoVault_EXPIRE(t *testing.T) {
cmd: "EXPIRE", cmd: "EXPIRE",
key: "key7", key: "key7",
time: 100000, time: 100000,
expireOpts: ExpireOptions{GT: true}, expireOpts: GT,
presetValues: map[string]internal.KeyData{ presetValues: map[string]internal.KeyData{
"key7": {Value: "value7", ExpireAt: mockClock.Now().Add(30 * time.Second)}, "key7": {Value: "value7", ExpireAt: mockClock.Now().Add(30 * time.Second)},
}, },
@@ -175,7 +174,7 @@ func TestEchoVault_EXPIRE(t *testing.T) {
cmd: "EXPIRE", cmd: "EXPIRE",
key: "key8", key: "key8",
time: 1000, time: 1000,
expireOpts: ExpireOptions{GT: true}, expireOpts: GT,
presetValues: map[string]internal.KeyData{ presetValues: map[string]internal.KeyData{
"key8": {Value: "value8", ExpireAt: mockClock.Now().Add(3000 * time.Second)}, "key8": {Value: "value8", ExpireAt: mockClock.Now().Add(3000 * time.Second)},
}, },
@@ -187,7 +186,7 @@ func TestEchoVault_EXPIRE(t *testing.T) {
cmd: "EXPIRE", cmd: "EXPIRE",
key: "key9", key: "key9",
time: 1000, time: 1000,
expireOpts: ExpireOptions{GT: true}, expireOpts: GT,
presetValues: map[string]internal.KeyData{ presetValues: map[string]internal.KeyData{
"key9": {Value: "value9", ExpireAt: time.Time{}}, "key9": {Value: "value9", ExpireAt: time.Time{}},
}, },
@@ -199,7 +198,7 @@ func TestEchoVault_EXPIRE(t *testing.T) {
cmd: "EXPIRE", cmd: "EXPIRE",
key: "key10", key: "key10",
time: 1000, time: 1000,
expireOpts: ExpireOptions{LT: true}, expireOpts: LT,
presetValues: map[string]internal.KeyData{ presetValues: map[string]internal.KeyData{
"key10": {Value: "value10", ExpireAt: mockClock.Now().Add(3000 * time.Second)}, "key10": {Value: "value10", ExpireAt: mockClock.Now().Add(3000 * time.Second)},
}, },
@@ -211,7 +210,7 @@ func TestEchoVault_EXPIRE(t *testing.T) {
cmd: "EXPIRE", cmd: "EXPIRE",
key: "key11", key: "key11",
time: 50000, time: 50000,
expireOpts: ExpireOptions{LT: true}, expireOpts: LT,
presetValues: map[string]internal.KeyData{ presetValues: map[string]internal.KeyData{
"key11": {Value: "value11", ExpireAt: mockClock.Now().Add(30 * time.Second)}, "key11": {Value: "value11", ExpireAt: mockClock.Now().Add(30 * time.Second)},
}, },
@@ -229,7 +228,7 @@ func TestEchoVault_EXPIRE(t *testing.T) {
var got bool var got bool
var err error var err error
if strings.EqualFold(tt.cmd, "PEXPIRE") { 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 { } else {
got, err = server.Expire(tt.key, tt.time, tt.expireOpts) got, err = server.Expire(tt.key, tt.time, tt.expireOpts)
} }
@@ -250,21 +249,20 @@ func TestEchoVault_EXPIREAT(t *testing.T) {
server := createEchoVault() server := createEchoVault()
tests := []struct { tests := []struct {
name string name string
presetValues map[string]internal.KeyData presetValues map[string]internal.KeyData
cmd string cmd string
key string key string
time int time int
expireAtOpts ExpireAtOptions expireAtOpts ExpireOptions
pexpireAtOpts PExpireAtOptions want int
want int wantErr bool
wantErr bool
}{ }{
{ {
name: "Set new expire by unix seconds", name: "Set new expire by unix seconds",
cmd: "EXPIREAT", cmd: "EXPIREAT",
key: "key1", key: "key1",
expireAtOpts: ExpireAtOptions{}, expireAtOpts: nil,
time: int(mockClock.Now().Add(1000 * time.Second).Unix()), time: int(mockClock.Now().Add(1000 * time.Second).Unix()),
presetValues: map[string]internal.KeyData{ presetValues: map[string]internal.KeyData{
"key1": {Value: "value1", ExpireAt: time.Time{}}, "key1": {Value: "value1", ExpireAt: time.Time{}},
@@ -273,11 +271,11 @@ func TestEchoVault_EXPIREAT(t *testing.T) {
wantErr: false, wantErr: false,
}, },
{ {
name: "Set new expire by milliseconds", name: "Set new expire by milliseconds",
cmd: "PEXPIREAT", cmd: "PEXPIREAT",
key: "key2", key: "key2",
pexpireAtOpts: PExpireAtOptions{}, expireAtOpts: nil,
time: int(mockClock.Now().Add(1000 * time.Second).UnixMilli()), time: int(mockClock.Now().Add(1000 * time.Second).UnixMilli()),
presetValues: map[string]internal.KeyData{ presetValues: map[string]internal.KeyData{
"key2": {Value: "value2", ExpireAt: time.Time{}}, "key2": {Value: "value2", ExpireAt: time.Time{}},
}, },
@@ -289,7 +287,7 @@ func TestEchoVault_EXPIREAT(t *testing.T) {
cmd: "EXPIREAT", cmd: "EXPIREAT",
key: "key3", key: "key3",
time: int(mockClock.Now().Add(1000 * time.Second).Unix()), time: int(mockClock.Now().Add(1000 * time.Second).Unix()),
expireAtOpts: ExpireAtOptions{NX: true}, expireAtOpts: NX,
presetValues: map[string]internal.KeyData{ presetValues: map[string]internal.KeyData{
"key3": {Value: "value3", ExpireAt: time.Time{}}, "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", name: "Return 0, when NX flag is provided and key already has an expiry time",
cmd: "EXPIREAT", cmd: "EXPIREAT",
time: int(mockClock.Now().Add(1000 * time.Second).Unix()), time: int(mockClock.Now().Add(1000 * time.Second).Unix()),
expireAtOpts: ExpireAtOptions{NX: true}, expireAtOpts: NX,
key: "key4", key: "key4",
presetValues: map[string]internal.KeyData{ presetValues: map[string]internal.KeyData{
"key4": {Value: "value4", ExpireAt: mockClock.Now().Add(1000 * time.Second)}, "key4": {Value: "value4", ExpireAt: mockClock.Now().Add(1000 * time.Second)},
@@ -313,7 +311,7 @@ func TestEchoVault_EXPIREAT(t *testing.T) {
cmd: "EXPIREAT", cmd: "EXPIREAT",
time: int(mockClock.Now().Add(1000 * time.Second).Unix()), time: int(mockClock.Now().Add(1000 * time.Second).Unix()),
key: "key5", key: "key5",
expireAtOpts: ExpireAtOptions{XX: true}, expireAtOpts: XX,
presetValues: map[string]internal.KeyData{ presetValues: map[string]internal.KeyData{
"key5": {Value: "value5", ExpireAt: mockClock.Now().Add(30 * time.Second)}, "key5": {Value: "value5", ExpireAt: mockClock.Now().Add(30 * time.Second)},
}, },
@@ -325,7 +323,7 @@ func TestEchoVault_EXPIREAT(t *testing.T) {
cmd: "EXPIREAT", cmd: "EXPIREAT",
key: "key6", key: "key6",
time: int(mockClock.Now().Add(1000 * time.Second).Unix()), time: int(mockClock.Now().Add(1000 * time.Second).Unix()),
expireAtOpts: ExpireAtOptions{XX: true}, expireAtOpts: XX,
presetValues: map[string]internal.KeyData{ presetValues: map[string]internal.KeyData{
"key6": {Value: "value6", ExpireAt: time.Time{}}, "key6": {Value: "value6", ExpireAt: time.Time{}},
}, },
@@ -337,7 +335,7 @@ func TestEchoVault_EXPIREAT(t *testing.T) {
cmd: "EXPIREAT", cmd: "EXPIREAT",
key: "key7", key: "key7",
time: int(mockClock.Now().Add(1000 * time.Second).Unix()), time: int(mockClock.Now().Add(1000 * time.Second).Unix()),
expireAtOpts: ExpireAtOptions{GT: true}, expireAtOpts: GT,
presetValues: map[string]internal.KeyData{ presetValues: map[string]internal.KeyData{
"key7": {Value: "value7", ExpireAt: mockClock.Now().Add(30 * time.Second)}, "key7": {Value: "value7", ExpireAt: mockClock.Now().Add(30 * time.Second)},
}, },
@@ -349,7 +347,7 @@ func TestEchoVault_EXPIREAT(t *testing.T) {
cmd: "EXPIREAT", cmd: "EXPIREAT",
key: "key8", key: "key8",
time: int(mockClock.Now().Add(1000 * time.Second).Unix()), time: int(mockClock.Now().Add(1000 * time.Second).Unix()),
expireAtOpts: ExpireAtOptions{GT: true}, expireAtOpts: GT,
presetValues: map[string]internal.KeyData{ presetValues: map[string]internal.KeyData{
"key8": {Value: "value8", ExpireAt: mockClock.Now().Add(3000 * time.Second)}, "key8": {Value: "value8", ExpireAt: mockClock.Now().Add(3000 * time.Second)},
}, },
@@ -361,7 +359,7 @@ func TestEchoVault_EXPIREAT(t *testing.T) {
cmd: "EXPIREAT", cmd: "EXPIREAT",
key: "key9", key: "key9",
time: int(mockClock.Now().Add(1000 * time.Second).Unix()), time: int(mockClock.Now().Add(1000 * time.Second).Unix()),
expireAtOpts: ExpireAtOptions{GT: true}, expireAtOpts: GT,
presetValues: map[string]internal.KeyData{ presetValues: map[string]internal.KeyData{
"key9": {Value: "value9", ExpireAt: time.Time{}}, "key9": {Value: "value9", ExpireAt: time.Time{}},
}, },
@@ -372,7 +370,7 @@ func TestEchoVault_EXPIREAT(t *testing.T) {
cmd: "EXPIREAT", cmd: "EXPIREAT",
key: "key10", key: "key10",
time: int(mockClock.Now().Add(1000 * time.Second).Unix()), time: int(mockClock.Now().Add(1000 * time.Second).Unix()),
expireAtOpts: ExpireAtOptions{LT: true}, expireAtOpts: LT,
presetValues: map[string]internal.KeyData{ presetValues: map[string]internal.KeyData{
"key10": {Value: "value10", ExpireAt: mockClock.Now().Add(3000 * time.Second)}, "key10": {Value: "value10", ExpireAt: mockClock.Now().Add(3000 * time.Second)},
}, },
@@ -384,7 +382,7 @@ func TestEchoVault_EXPIREAT(t *testing.T) {
cmd: "EXPIREAT", cmd: "EXPIREAT",
key: "key11", key: "key11",
time: int(mockClock.Now().Add(3000 * time.Second).Unix()), time: int(mockClock.Now().Add(3000 * time.Second).Unix()),
expireAtOpts: ExpireAtOptions{LT: true}, expireAtOpts: LT,
presetValues: map[string]internal.KeyData{ presetValues: map[string]internal.KeyData{
"key11": {Value: "value11", ExpireAt: mockClock.Now().Add(1000 * time.Second)}, "key11": {Value: "value11", ExpireAt: mockClock.Now().Add(1000 * time.Second)},
}, },
@@ -396,7 +394,7 @@ func TestEchoVault_EXPIREAT(t *testing.T) {
cmd: "EXPIREAT", cmd: "EXPIREAT",
key: "key12", key: "key12",
time: int(mockClock.Now().Add(1000 * time.Second).Unix()), time: int(mockClock.Now().Add(1000 * time.Second).Unix()),
expireAtOpts: ExpireAtOptions{LT: true}, expireAtOpts: LT,
presetValues: map[string]internal.KeyData{ presetValues: map[string]internal.KeyData{
"key12": {Value: "value12", ExpireAt: time.Time{}}, "key12": {Value: "value12", ExpireAt: time.Time{}},
}, },
@@ -414,7 +412,7 @@ func TestEchoVault_EXPIREAT(t *testing.T) {
var got int var got int
var err error var err error
if strings.EqualFold(tt.cmd, "PEXPIREAT") { 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 { } else {
got, err = server.ExpireAt(tt.key, tt.time, tt.expireAtOpts) got, err = server.ExpireAt(tt.key, tt.time, tt.expireAtOpts)
} }
@@ -606,12 +604,21 @@ func TestEchoVault_SET(t *testing.T) {
server := createEchoVault() 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 { tests := []struct {
name string name string
presetValues map[string]internal.KeyData presetValues map[string]internal.KeyData
key string key string
value string value string
options SetOptions options SETOptions
wantOk bool wantOk bool
wantPrev string wantPrev string
wantErr bool wantErr bool
@@ -621,7 +628,7 @@ func TestEchoVault_SET(t *testing.T) {
presetValues: nil, presetValues: nil,
key: "key1", key: "key1",
value: "value1", value: "value1",
options: SetOptions{}, options: SetOptions(nil, nil, 0, false),
wantOk: true, wantOk: true,
wantPrev: "", wantPrev: "",
wantErr: false, wantErr: false,
@@ -631,7 +638,7 @@ func TestEchoVault_SET(t *testing.T) {
presetValues: nil, presetValues: nil,
key: "key2", key: "key2",
value: "value2", value: "value2",
options: SetOptions{NX: true}, options: SetOptions(SETNX, nil, 0, false),
wantOk: true, wantOk: true,
wantPrev: "", wantPrev: "",
wantErr: false, wantErr: false,
@@ -646,7 +653,7 @@ func TestEchoVault_SET(t *testing.T) {
}, },
key: "key3", key: "key3",
value: "value3", value: "value3",
options: SetOptions{NX: true}, options: SetOptions(SETNX, nil, 0, false),
wantOk: false, wantOk: false,
wantPrev: "", wantPrev: "",
wantErr: true, wantErr: true,
@@ -661,7 +668,7 @@ func TestEchoVault_SET(t *testing.T) {
}, },
key: "key4", key: "key4",
value: "value4", value: "value4",
options: SetOptions{XX: true}, options: SetOptions(SETXX, nil, 0, false),
wantOk: true, wantOk: true,
wantPrev: "", wantPrev: "",
wantErr: false, wantErr: false,
@@ -671,7 +678,7 @@ func TestEchoVault_SET(t *testing.T) {
presetValues: nil, presetValues: nil,
key: "key5", key: "key5",
value: "value5", value: "value5",
options: SetOptions{XX: true}, options: SetOptions(SETXX, nil, 0, false),
wantOk: false, wantOk: false,
wantPrev: "", wantPrev: "",
wantErr: true, wantErr: true,
@@ -681,7 +688,7 @@ func TestEchoVault_SET(t *testing.T) {
presetValues: nil, presetValues: nil,
key: "key6", key: "key6",
value: "value6", value: "value6",
options: SetOptions{EX: 100}, options: SetOptions(nil, SETEX, 100, false),
wantOk: true, wantOk: true,
wantPrev: "", wantPrev: "",
wantErr: false, wantErr: false,
@@ -691,7 +698,7 @@ func TestEchoVault_SET(t *testing.T) {
presetValues: nil, presetValues: nil,
key: "key7", key: "key7",
value: "value7", value: "value7",
options: SetOptions{PX: 4096}, options: SetOptions(nil, SETPX, 4096, false),
wantOk: true, wantOk: true,
wantPrev: "", wantPrev: "",
wantErr: false, wantErr: false,
@@ -701,7 +708,7 @@ func TestEchoVault_SET(t *testing.T) {
presetValues: nil, presetValues: nil,
key: "key8", key: "key8",
value: "value8", 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, wantOk: true,
wantPrev: "", wantPrev: "",
wantErr: false, wantErr: false,
@@ -710,7 +717,7 @@ func TestEchoVault_SET(t *testing.T) {
name: "Set exact expiry time in milliseconds from unix epoch", name: "Set exact expiry time in milliseconds from unix epoch",
key: "key9", key: "key9",
value: "value9", 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, presetValues: nil,
wantOk: true, wantOk: true,
wantPrev: "", wantPrev: "",
@@ -726,7 +733,7 @@ func TestEchoVault_SET(t *testing.T) {
}, },
key: "key10", key: "key10",
value: "value10", value: "value10",
options: SetOptions{GET: true, EX: 1000}, options: SetOptions(nil, SETEX, 1000, true),
wantOk: true, wantOk: true,
wantPrev: "previous-value", wantPrev: "previous-value",
wantErr: false, wantErr: false,
@@ -736,7 +743,7 @@ func TestEchoVault_SET(t *testing.T) {
presetValues: nil, presetValues: nil,
key: "key11", key: "key11",
value: "value11", value: "value11",
options: SetOptions{GET: true, EX: 1000}, options: SetOptions(nil, SETEX, 1000, true),
wantOk: true, wantOk: true,
wantPrev: "", wantPrev: "",
wantErr: false, wantErr: false,
@@ -749,7 +756,11 @@ func TestEchoVault_SET(t *testing.T) {
presetKeyData(server, context.Background(), k, d) 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 { if (err != nil) != tt.wantErr {
t.Errorf("SET() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("SET() error = %v, wantErr %v", err, tt.wantErr)
return return
@@ -1408,90 +1419,97 @@ func TestEchoVault_GETEX(t *testing.T) {
server := createEchoVault() server := createEchoVault()
tests := []struct { tests := []struct {
name string name string
presetValue interface{} presetValue interface{}
getExOpts GetExOptions getExOpt GetExOption
key string getExOptTime int
want string key string
wantEx int want string
wantErr bool wantEx int
wantErr bool
}{ }{
{ {
name: "1. Return string from existing key, no expire options", name: "1. Return string from existing key, no expire options",
presetValue: "value1", presetValue: "value1",
getExOpts: GetExOptions{}, getExOpt: nil,
key: "key1", key: "key1",
want: "value1", want: "value1",
wantEx: -1, wantEx: -1,
wantErr: false, wantErr: false,
}, },
{ {
name: "2. Return empty string if the key does not exist", name: "2. Return empty string if the key does not exist",
presetValue: nil, presetValue: nil,
getExOpts: GetExOptions{EX: true, UNIXTIME: int(mockClock.Now().Add(100 * time.Second).Unix())}, getExOpt: EX,
key: "key2", getExOptTime: int(mockClock.Now().Add(100 * time.Second).Unix()),
want: "", key: "key2",
wantEx: 0, want: "",
wantErr: false, wantEx: 0,
wantErr: false,
}, },
{ {
name: "3. Return key set expiry with EX", name: "3. Return key set expiry with EX",
presetValue: "value3", presetValue: "value3",
getExOpts: GetExOptions{EX: true, UNIXTIME: 100}, getExOpt: EX,
key: "key3", getExOptTime: 100,
want: "value3", key: "key3",
wantEx: 100, want: "value3",
wantErr: false, wantEx: 100,
wantErr: false,
}, },
{ {
name: "4. Return key set expiry with PX", name: "4. Return key set expiry with PX",
presetValue: "value4", presetValue: "value4",
getExOpts: GetExOptions{PX: true, UNIXTIME: 100000}, getExOpt: PX,
key: "key4", getExOptTime: 100000,
want: "value4", key: "key4",
wantEx: 100, want: "value4",
wantErr: false, wantEx: 100,
wantErr: false,
}, },
{ {
name: "5. Return key set expiry with EXAT", name: "5. Return key set expiry with EXAT",
presetValue: "value5", presetValue: "value5",
getExOpts: GetExOptions{EXAT: true, UNIXTIME: int(mockClock.Now().Add(100 * time.Second).Unix())}, getExOpt: EXAT,
key: "key5", getExOptTime: int(mockClock.Now().Add(100 * time.Second).Unix()),
want: "value5", key: "key5",
wantEx: 100, want: "value5",
wantErr: false, wantEx: 100,
wantErr: false,
}, },
{ {
name: "6. Return key set expiry with PXAT", name: "6. Return key set expiry with PXAT",
presetValue: "value6", presetValue: "value6",
getExOpts: GetExOptions{PXAT: true, UNIXTIME: int(mockClock.Now().Add(100 * time.Second).UnixMilli())}, getExOpt: PXAT,
key: "key6", getExOptTime: int(mockClock.Now().Add(100 * time.Second).UnixMilli()),
want: "value6", key: "key6",
wantEx: 100, want: "value6",
wantErr: false, wantEx: 100,
wantErr: false,
}, },
{ {
name: "7. Return key passing PERSIST", name: "7. Return key passing PERSIST",
presetValue: "value7", presetValue: "value7",
getExOpts: GetExOptions{PERSIST: true}, getExOpt: PERSIST,
key: "key7", key: "key7",
want: "value7", want: "value7",
wantEx: -1, wantEx: -1,
wantErr: false, wantErr: false,
}, },
{ {
name: "8. Return key passing PERSIST, and include a UNIXTIME", name: "8. Return key passing PERSIST, and include a UNIXTIME",
presetValue: "value8", presetValue: "value8",
getExOpts: GetExOptions{PERSIST: true, UNIXTIME: int(mockClock.Now().Add(100 * time.Second).Unix())}, getExOpt: PERSIST,
key: "key8", getExOptTime: int(mockClock.Now().Add(100 * time.Second).Unix()),
want: "value8", key: "key8",
wantEx: -1, want: "value8",
wantErr: false, wantEx: -1,
wantErr: false,
}, },
{ {
name: "9. Return key and attempt to set expiry with EX without providing UNIXTIME", name: "9. Return key and attempt to set expiry with EX without providing UNIXTIME",
presetValue: "value9", presetValue: "value9",
getExOpts: GetExOptions{EX: true}, getExOpt: EX,
key: "key9", key: "key9",
want: "value9", want: "value9",
wantEx: -1, 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", name: "10. Return key and attempt to set expiry with PXAT without providing UNIXTIME",
presetValue: "value10", presetValue: "value10",
getExOpts: GetExOptions{PXAT: true}, getExOpt: PXAT,
key: "key10", key: "key10",
want: "value10", want: "value10",
wantEx: -1, wantEx: -1,
@@ -1516,8 +1534,8 @@ func TestEchoVault_GETEX(t *testing.T) {
return return
} }
} }
// Check value received //Check value received
got, err := server.GetEx(tt.key, tt.getExOpts) got, err := server.GetEx(tt.key, tt.getExOpt, tt.getExOptTime)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("GETEX() GET error = %v, wantErr %v", err, tt.wantErr) t.Errorf("GETEX() GET error = %v, wantErr %v", err, tt.wantErr)
return return

View File

@@ -311,7 +311,7 @@ func Test_Cluster(t *testing.T) {
// Write all the data to the cluster leader. // Write all the data to the cluster leader.
for i, test := range tests { for i, test := range tests {
node := nodes[0] 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 { if err != nil {
t.Errorf("could not write command to leader node (test %d): %v", i, err) 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. // Write all the data to the cluster leader.
for i, test := range tests { for i, test := range tests {
node := nodes[0] 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 { if err != nil {
t.Errorf("could not write command to leader node (test %d): %v", i, err) 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 { for database, data := range test.values {
_ = mockServer.SelectDB(database) _ = mockServer.SelectDB(database)
for key, value := range data { 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) t.Error(err)
return return
} }
@@ -1101,7 +1101,7 @@ func Test_Standalone(t *testing.T) {
// Perform write commands from "before-rewrite" // Perform write commands from "before-rewrite"
for key, value := range data["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) t.Error(err)
return return
} }
@@ -1118,7 +1118,7 @@ func Test_Standalone(t *testing.T) {
// Perform write commands from "after-rewrite" // Perform write commands from "after-rewrite"
for key, value := range data["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) t.Error(err)
return return
} }

View File

@@ -776,7 +776,7 @@ func Test_AdminCommands(t *testing.T) {
// Trigger some write commands // Trigger some write commands
for key, value := range test.values { 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) t.Error(err)
return return
} }

View File

@@ -2662,7 +2662,7 @@ func Test_Generic(t *testing.T) {
_, _, _ = mockServer.Set( _, _, _ = mockServer.Set(
fmt.Sprintf("key%d", k), fmt.Sprintf("key%d", k),
fmt.Sprintf("value%d", k), fmt.Sprintf("value%d", k),
echovault.SetOptions{}, echovault.SETOptions{},
) )
} }
} }
@@ -2768,7 +2768,7 @@ func Test_Generic(t *testing.T) {
_, _, err := mockServer.Set( _, _, err := mockServer.Set(
fmt.Sprintf("RandomKey%d", i), fmt.Sprintf("RandomKey%d", i),
fmt.Sprintf("Value%d", i), fmt.Sprintf("Value%d", i),
echovault.SetOptions{}, echovault.SETOptions{},
) )
if err != nil { if err != nil {
t.Error(err) t.Error(err)