How to use counters in a redis key? -
is there way in redis ?
set counter 0 incr counter set key:{counter} "content of line 1" incr counter set key:{counter} "different content of line 2"
my example code should substituted (i.e., transformed @ runtime redis-cli) into:
set counter 0 incr counter set key:1 "content of line 1" incr counter set key:2 "different content of line 2" etc.
my problem not how auto-increment counter.
problem syntax: how include generic {wildcard} like:
set keyname:{currentcounter} "value" ...
any appreciated. lot !
bernie
if using redis 2.6+ can use lua scripting along eval command following:
eval "local c = redis.call('incr', keys[1]); return redis.call('set', keys[2] .. ':' .. c, argv[1])" 2 counter key "content of line 1"
i broke onto multiple lines make easier read.
edit
sorry, away on business few days. here sample showing works.
redis 127.0.0.1:6379> flushdb ok redis 127.0.0.1:6379> eval "local c = redis.call('incr', keys[1]); return redis.call('set', keys[2] .. ':' .. c, argv[1])" 2 counter key "content of line 1" ok redis 127.0.0.1:6379> keys * 1) "key:1" 2) "counter" redis 127.0.0.1:6379> counter "1" redis 127.0.0.1:6379> key:1 "content of line 1" redis 127.0.0.1:6379> eval "local c = redis.call('incr', keys[1]); return redis.call('set', keys[2] .. ':' .. c, argv[1])" 2 counter key "content of next thing" ok redis 127.0.0.1:6379> keys * 1) "key:1" 2) "key:2" 3) "counter" redis 127.0.0.1:6379> counter "2" redis 127.0.0.1:6379> key:2 "content of next thing"
Comments
Post a Comment