pub struct ScanKeyCursor { /* private fields */ }Expand description
A cursor to scan field/value pairs of a (hash) key.
It provides access via a closure given to ScanKeyCursor::for_each or if you need more control, you can use ScanKeyCursor::scan
and implement your own loop, e.g. to allow an early stop.
§Example usage
Here we show how to extract values to communicate them back to the Redis client. We assume that the following hash key is setup before:
HSET user:123 name Alice age 29 location AustinThe following example command implementation scans all fields and values in the hash key and returns them as an array of RedisString.
fn example_scan_key_for_each(ctx: &Context) -> RedisResult {
let key = ctx.open_key_with_flags("user:123", KeyFlags::NOEFFECTS | KeyFlags::NOEXPIRE | KeyFlags::ACCESS_EXPIRED );
let cursor = ScanKeyCursor::new(key);
let res = RefCell::new(Vec::new());
cursor.for_each(|_key, field, value| {
let mut res = res.borrow_mut();
res.push(RedisValue::BulkRedisString(field.clone()));
res.push(RedisValue::BulkRedisString(value.clone()));
});
Ok(RedisValue::Array(res.take()))
}The method will produce the following output:
1) "name"
2) "Alice"
3) "age"
4) "29"
5) "location"
6) "Austin"Implementations§
Source§impl ScanKeyCursor
impl ScanKeyCursor
Sourcepub fn scan<F: FnMut(&RedisKey, &RedisString, &RedisString)>(
&self,
f: F,
) -> bool
pub fn scan<F: FnMut(&RedisKey, &RedisString, &RedisString)>( &self, f: F, ) -> bool
Implements a call to RedisModule_ScanKey and calls the given closure for each callback invocation by ScanKey.
Returns true if there are more fields to scan, false otherwise.
The callback may be called multiple times per RedisModule_ScanKey invocation.
§Example
while cursor.scan(|_key, field, value| {
// do something with field and value
}) {
// do something between scans if needed, like an early stop
}Sourcepub fn for_each<F: FnMut(&RedisKey, &RedisString, &RedisString)>(&self, f: F)
pub fn for_each<F: FnMut(&RedisKey, &RedisString, &RedisString)>(&self, f: F)
Implements a callback based for_each loop over all fields and values in the hash key.
If you need more control, e.g. stopping after a scan invocation, then use ScanKeyCursor::scan directly.