Skip to content

Commit

Permalink
Go implement object freq object idle object ref count command (valkey…
Browse files Browse the repository at this point in the history
…-io#2958)

* Implement Object Freq, Idle and RefCount

Signed-off-by: EdricCua <[email protected]>
  • Loading branch information
EdricCua authored and Maayanshani25 committed Jan 22, 2025
1 parent 27c69e0 commit 42f3dab
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 0 deletions.
83 changes: 83 additions & 0 deletions go/api/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2665,3 +2665,86 @@ func (client *baseClient) ZRemRangeByScore(key string, rangeQuery options.RangeB
}
return handleIntResponse(result)
}

// Returns the logarithmic access frequency counter of a Valkey object stored at key.
//
// Parameters:
//
// key - The key of the object to get the logarithmic access frequency counter of.
//
// Return value:
//
// If key exists, returns the logarithmic access frequency counter of the
// object stored at key as a long. Otherwise, returns `nil`.
//
// Example:
//
// result, err := client.ObjectFreq(key)
// if err != nil {
// // handle error
// }
// fmt.Println(result.Value()) // Output: 1
//
// [valkey.io]: https://valkey.io/commands/object-freq/
func (client *baseClient) ObjectFreq(key string) (Result[int64], error) {
result, err := client.executeCommand(C.ObjectFreq, []string{key})
if err != nil {
return CreateNilInt64Result(), err
}
return handleIntOrNilResponse(result)
}

// Returns the logarithmic access frequency counter of a Valkey object stored at key.
//
// Parameters:
//
// key - The key of the object to get the logarithmic access frequency counter of.
//
// Return value:
//
// If key exists, returns the idle time in seconds. Otherwise, returns `nil`.
//
// Example:
//
// result, err := client.ObjectIdleTime(key)
// if err != nil {
// // handle error
// }
// fmt.Println(result.Value()) // Output: 1
//
// [valkey.io]: https://valkey.io/commands/object-idletime/
func (client *baseClient) ObjectIdleTime(key string) (Result[int64], error) {
result, err := client.executeCommand(C.ObjectIdleTime, []string{key})
if err != nil {
return CreateNilInt64Result(), err
}
return handleIntOrNilResponse(result)
}

// Returns the reference count of the object stored at key.
//
// Parameters:
//
// key - The key of the object to get the reference count of.
//
// Return value:
//
// If key exists, returns the reference count of the object stored at key.
// Otherwise, returns `nil`.
//
// Example:
//
// result, err := client.ObjectRefCount(key)
// if err != nil {
// // handle error
// }
// fmt.Println(result.Value()) // Output: 1
//
// [valkey.io]: https://valkey.io/commands/object-refcount/
func (client *baseClient) ObjectRefCount(key string) (Result[int64], error) {
result, err := client.executeCommand(C.ObjectRefCount, []string{key})
if err != nil {
return CreateNilInt64Result(), err
}
return handleIntOrNilResponse(result)
}
6 changes: 6 additions & 0 deletions go/api/generic_base_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,4 +527,10 @@ type GenericBaseCommands interface {
//
// [valkey.io]: https://valkey.io/commands/dump/
Dump(key string) (Result[string], error)

ObjectFreq(key string) (Result[int64], error)

ObjectIdleTime(key string) (Result[int64], error)

ObjectRefCount(key string) (Result[int64], error)
}
74 changes: 74 additions & 0 deletions go/integTest/shared_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6231,3 +6231,77 @@ func (suite *GlideTestSuite) TestZRemRangeByScore() {
assert.IsType(suite.T(), &api.RequestError{}, err)
})
}

func (suite *GlideTestSuite) TestObjectIdleTime() {
suite.runWithDefaultClients(func(client api.BaseClient) {
defaultClient := suite.defaultClient()
key := "testKey1_" + uuid.New().String()
value := "hello"
sleepSec := int64(5)
t := suite.T()
suite.verifyOK(defaultClient.Set(key, value))
keyValueMap := map[string]string{
"maxmemory-policy": "noeviction",
}
suite.verifyOK(defaultClient.ConfigSet(keyValueMap))
key1 := api.CreateStringResult("maxmemory-policy")
value1 := api.CreateStringResult("noeviction")
resultConfigMap := map[api.Result[string]]api.Result[string]{key1: value1}
resultConfig, err := defaultClient.ConfigGet([]string{"maxmemory-policy"})
assert.Nil(t, err, "Failed to get configuration")
assert.Equal(t, resultConfigMap, resultConfig, "Configuration mismatch for maxmemory-policy")
resultGet, err := defaultClient.Get(key)
assert.Nil(t, err)
assert.Equal(t, value, resultGet.Value())
time.Sleep(time.Duration(sleepSec) * time.Second)
resultIdleTime, err := defaultClient.ObjectIdleTime(key)
assert.Nil(t, err)
assert.GreaterOrEqual(t, resultIdleTime.Value(), sleepSec)
})
}

func (suite *GlideTestSuite) TestObjectRefCount() {
suite.runWithDefaultClients(func(client api.BaseClient) {
key := "testKey1_" + uuid.New().String()
value := "hello"
t := suite.T()
suite.verifyOK(client.Set(key, value))
resultGetRestoreKey, err := client.Get(key)
assert.Nil(t, err)
assert.Equal(t, value, resultGetRestoreKey.Value())
resultObjectRefCount, err := client.ObjectRefCount(key)
assert.Nil(t, err)
assert.GreaterOrEqual(t, resultObjectRefCount.Value(), int64(1))
})
}

func (suite *GlideTestSuite) TestObjectFreq() {
suite.runWithDefaultClients(func(client api.BaseClient) {
defaultClient := suite.defaultClient()
key := "testKey1_" + uuid.New().String()
value := "hello"
t := suite.T()
suite.verifyOK(defaultClient.Set(key, value))
keyValueMap := map[string]string{
"maxmemory-policy": "volatile-lfu",
}
suite.verifyOK(defaultClient.ConfigSet(keyValueMap))
key1 := api.CreateStringResult("maxmemory-policy")
value1 := api.CreateStringResult("volatile-lfu")
resultConfigMap := map[api.Result[string]]api.Result[string]{key1: value1}
resultConfig, err := defaultClient.ConfigGet([]string{"maxmemory-policy"})
assert.Nil(t, err, "Failed to get configuration")
assert.Equal(t, resultConfigMap, resultConfig, "Configuration mismatch for maxmemory-policy")
sleepSec := int64(5)
time.Sleep(time.Duration(sleepSec) * time.Second)
resultGet, err := defaultClient.Get(key)
assert.Nil(t, err)
assert.Equal(t, value, resultGet.Value())
resultGet2, err := defaultClient.Get(key)
assert.Nil(t, err)
assert.Equal(t, value, resultGet2.Value())
resultObjFreq, err := defaultClient.ObjectFreq(key)
assert.Nil(t, err)
assert.GreaterOrEqual(t, resultObjFreq.Value(), int64(2))
})
}

0 comments on commit 42f3dab

Please sign in to comment.