Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iss 60: Implement Touch, with bonus commands ObjectFreq and ObjectIdleTime #126

Merged
merged 19 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,157 changes: 1,619 additions & 1,538 deletions coverage/coverage.out

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/docs/commands/connection/hello.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,4 @@ This command returns a contextual client report.
> HELLO 3 AUTH myuser mypass
```
</TabItem>
</Tabs>
</Tabs>
2 changes: 1 addition & 1 deletion docs/docs/commands/connection/select.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ When this command is executed in a RAFT cluster, the database will be created in
> SELECT 1
```
</TabItem>
</Tabs>
</Tabs>
2 changes: 1 addition & 1 deletion docs/docs/commands/connection/swapdb.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ and the other way around. If either one of the databases does not exist, it will
> SWAPDB 1 2
```
</TabItem>
</Tabs>
</Tabs>
49 changes: 49 additions & 0 deletions docs/docs/commands/generic/objectfreq.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

# OBJECTFREQ

### Syntax
```
OBJECTFREQ keys
```

### Module
<span className="acl-category">generic</span>

### Categories
<span className="acl-category">keyspace</span>
<span className="acl-category">read</span>
<span className="acl-category">slow</span>

### Description
Get the time in seconds since the last access to the value stored at the key.
The command is only available when the maxmemory-policy configuration directive is set to one of the LRU policies.
This command returns an integer representing the access frequency. If the key doesn't exist -1 and an error is returned.

### Examples

<Tabs
defaultValue="go"
values={[
{ label: 'Go (Embedded)', value: 'go', },
{ label: 'CLI', value: 'cli', },
]}
>
<TabItem value="go">
Get a key's access frequency:
```go
vault, err := echovault.NewEchoVault()
if err != nil {
log.Fatal(err)
}
freq, err := vault.ObjectFreq("key")
```
</TabItem>
<TabItem value="cli">
Get the access frequency of a key:
```
> OBJECTFREQ key
```
</TabItem>
</Tabs>
50 changes: 50 additions & 0 deletions docs/docs/commands/generic/objectidletime.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

# OBJECTIDLETIME

### Syntax
```
OBJECTIDLETIME key
```

### Module
<span className="acl-category">generic</span>

### Categories
<span className="acl-category">keyspace</span>
<span className="acl-category">read</span>
<span className="acl-category">slow</span>

### Description
Get the time in seconds since the last access to the value stored at the key.
The command is only available when the maxmemory-policy configuration directive is set to one of the LRU policies.
This commands returns a float representing the seconds since the key was last accessed. If the key doesn't exist -1
and an error is returned.

### Examples

<Tabs
defaultValue="go"
values={[
{ label: 'Go (Embedded)', value: 'go', },
{ label: 'CLI', value: 'cli', },
]}
>
<TabItem value="go">
Get a key's idle time:
```go
vault, err := echovault.NewEchoVault()
if err != nil {
log.Fatal(err)
}
idletime, err := vault.ObjectIdleTime("key")
```
</TabItem>
<TabItem value="cli">
Get the idle time of a key:
```
> OBJECTIDLETIME key
```
</TabItem>
</Tabs>
62 changes: 62 additions & 0 deletions docs/docs/commands/generic/touch.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

# TOUCH

### Syntax
```
TOUCH keys [key ...]
```

### Module
<span className="acl-category">generic</span>

### Categories
<span className="acl-category">keyspace</span>
<span className="acl-category">read</span>
<span className="acl-category">fast</span>

### Description
Alters the last access time or access count of the key(s) depending on whether LFU or LRU strategy was used.
A key is ignored if it does not exist. This commands returns the number of keys that were touched.

### Examples

<Tabs
defaultValue="go"
values={[
{ label: 'Go (Embedded)', value: 'go', },
{ label: 'CLI', value: 'cli', },
]}
>
<TabItem value="go">
Touch a key:
```go
vault, err := echovault.NewEchoVault()
if err != nil {
log.Fatal(err)
}
touched, err := vault.Touch("key1")
```

Touch multiple keys:
```go
vault, err := echovault.NewEchoVault()
if err != nil {
log.Fatal(err)
}
touched, err := vault.Touch("key1", "key2", "key3")
```
</TabItem>
<TabItem value="cli">
Touch a key:
```
> TOUCH key1
```

Touch multiple keys:
```
> TOUCH key1 key2 key3
```
</TabItem>
</Tabs>
54 changes: 54 additions & 0 deletions echovault/api_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,3 +646,57 @@ func (server *EchoVault) GetEx(key string, opts GetExOptions) (string, error) {
}
return internal.ParseStringResponse(b)
}

// Touch Alters the last access time or access count of the key(s) depending on whether LFU or LRU strategy was used.
// A key is ignored if it does not exist.
//
// Parameters:
//
// `keys` - ...string - the keys whose access time or access count should be incremented based on eviction policy.
//
// Returns: An integer representing the number of keys successfully touched. If a key doesn't exist it is simply ignored.
func (server *EchoVault) Touch(keys ...string) (int, error) {
cmd := make([]string, len(keys)+1)
cmd[0] = "TOUCH"
for i, k := range keys {
cmd[i+1] = k
}

b, err := server.handleCommand(server.context, internal.EncodeCommand(cmd), nil, false, true)
if err != nil {
return -1, err
}
return internal.ParseIntegerResponse(b)
}

// ObjectFreq retrieves the access frequency count of an object stored at <key>.
// The command is only available when the maxmemory-policy configuration directive is set to one of the LFU policies.
//
// Parameters:
//
// `key` - string - the key whose access frequency should be retrieved.
//
// Returns: An integer representing the access frequency. If the key doesn't exist -1 and an error is returned.
func (server *EchoVault) ObjectFreq(key string) (int, error) {
b, err := server.handleCommand(server.context, internal.EncodeCommand([]string{"OBJECTFREQ", key}), nil, false, true)
if err != nil {
return -1, err
}
return internal.ParseIntegerResponse(b)
}

// ObjectIdleTime retrieves the time in seconds since the last access to the value stored at <key>.
// The command is only available when the maxmemory-policy configuration directive is set to one of the LRU policies.
//
// Parameters:
//
// `key` - string - the key whose last access time should be retrieved.
//
// Returns: A float64 representing the seconds since the key was last accessed. If the key doesn't exist -1 and an error is returned.
func (server *EchoVault) ObjectIdleTime(key string) (float64, error) {
b, err := server.handleCommand(server.context, internal.EncodeCommand([]string{"OBJECTIDLETIME", key}), nil, false, true)
if err != nil {
return -1, err
}
return internal.ParseFloatResponse(b)
}
Loading
Loading