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

add object commands #610

Merged
merged 1 commit into from Jun 8, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/commands.rs
Expand Up @@ -1008,6 +1008,28 @@ implement_commands! {
cmd("PUBLISH").arg(channel).arg(message)
}

// Object commands

/// Returns the encoding of a key.
fn object_encoding<K: ToRedisArgs>(key: K) {
cmd("OBJECT").arg("ENCODING").arg(key)
}

/// Returns the time in seconds since the last access of a key.
fn object_idletime<K: ToRedisArgs>(key: K) {
cmd("OBJECT").arg("IDLETIME").arg(key)
}

/// Returns the logarithmic access frequency counter of a key.
fn object_freq<K: ToRedisArgs>(key: K) {
cmd("OBJECT").arg("FREQ").arg(key)
}

/// Returns the reference count of a key.
fn object_refcount<K: ToRedisArgs>(key: K) {
cmd("OBJECT").arg("REFCOUNT").arg(key)
}

// ACL commands

/// When Redis is configured to use an ACL file (with the aclfile
Expand Down
35 changes: 35 additions & 0 deletions tests/test_basic.rs
Expand Up @@ -927,3 +927,38 @@ fn test_zrandmember() {
let results: Vec<String> = con.zrandmember_withscores(setname, -5).unwrap();
assert_eq!(results.len(), 10);
}

#[test]
fn test_object_commands() {
let ctx = TestContext::new();
let mut con = ctx.connection();

let _: () = con.set("object_key_str", "object_value_str").unwrap();
let _: () = con.set("object_key_int", 42).unwrap();

assert_eq!(
con.object_encoding::<_, String>("object_key_str").unwrap(),
"embstr"
);

assert_eq!(
con.object_encoding::<_, String>("object_key_int").unwrap(),
"int"
);

assert_eq!(con.object_idletime::<_, i32>("object_key_str").unwrap(), 0);
assert_eq!(con.object_refcount::<_, i32>("object_key_str").unwrap(), 1);

// Needed for OBJECT FREQ and can't be set before object_idletime
// since that will break getting the idletime before idletime adjuts
redis::cmd("CONFIG")
.arg("SET")
.arg(b"maxmemory-policy")
.arg("allkeys-lfu")
.execute(&mut con);

let _: () = con.get("object_key_str").unwrap();
// since maxmemory-policy changed, freq should reset to 1 since we only called
// get after that
assert_eq!(con.object_freq::<_, i32>("object_key_str").unwrap(), 1);
}