Skip to content

Commit

Permalink
add object commands
Browse files Browse the repository at this point in the history
ENCODING, IDLETIME, FREQ, REFCOUNT
closes: #599
  • Loading branch information
Roger committed May 27, 2022
1 parent 31d09ab commit d236732
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
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);
}

0 comments on commit d236732

Please sign in to comment.