REDIS - How can I query keys and get their values in one query? -
using keys can query keys can see below:
redis> set popo "pepe" ok redis> set coco "kansas" ok redis> set cool "rock" ok redis> set cool2 "punk" ok redis> keys *co* 1) "cool2" 2) "coco" 3) "cool" redis> keys *ol* 1) "cool2" 2) "cool"
is there way values instead of keys? like: mget (keys *ol*)
notice: others have mentioned, along myself in comments on original question, in production environments keys should avoided. if you're running queries on own box , hacking together, go it. otherwise, question if redis makes sense particular application, , if need - if so, impose limits , avoid large blocking calls, such keys
. (for this, see 2015 edit, below.)
my laptop isn't readily available right test this, can tell there isn't native commands allow use pattern in way. if want within redis, might have use eval
chain commands:
eval "return redis.call('mget', unpack(redis.call('keys', keys[1])))" 1 "*co*"
(replacing *co*
@ end whatever pattern you're searching for.)
note: runs string lua script - haven't dove it, don't know if sanitizes input in way. before use (especially if intend user input) test injecting further redis.call functions in , see if evaluates too. if does, careful it.
edit: actually, should safe because neither redis nor it's lua evaluation allows escaping containing string: http://redis.io/topics/security
2015 edit: since original post, redis has released 2.8, includes scan
command, better fit type of functionality. it not work exact question, requests one-liner command, it's better reasonable constraints / environments.
details scan
can read @ http://redis.io/commands/scan .
to use this, iterate on data set using scan ${cursor} match ${query} count ${maxpagesize}
(e.g. scan 0 match *co* count 500
). here, cursor
should initialized 0.
this returns 2 things: first new cursor
value can use next set of elements, , second collection of elements matching query
. keep updating cursor
, calling query until cursor
0 again (meaning you've iterated on everything), , push found elements collection.
i know scan
sounds lot more work, implore you, please use solution instead of keys
important.
Comments
Post a Comment