Use set or just create keys in redis to check existence? -
i can think of 2 ways of checking existence using redis:
- use whole database 'set', ,
set
key , checking existenceget
ing (or usingexists
mentioned in comment @sergio tulentsev) - use
sadd
add members key , check existencesismember
which 1 better? problem, compared same amount of keys in single set, if choose first method , number of keys in database gets larger?
in fact, besides these 2 methods, can use hash
data structure hexists
command (i'll call method third solution).
all these solutions fast enough, , it's not problem if have large set
, hash
, or keyspace.
so, 1 should use? it depends on lots of things...
does key has value?
keys of both first , third solution can have value, while second solution cannot.
so if there's no value each key, i'd prefer second solution, i.e. set
solution. otherwise, have use first or third solution.
does value has structure?
if value not raw string, data structure, e.g. list
, set
. have use first solution, since hash
's value can raw string.
do need set operations?
if need intersection, union or diff operations on multiple data sets, should use second solution. redis has built-in commands these operations, although might slow commands.
memory efficiency consideration
redis takes more memory-efficient encoding small set
, hash
. when have lots of small data sets, take second , third solution can save lots of memory. see this details.
update
do need set ttl these keys?
as @dizzyf points out in comment, if need set ttl
these keys, have use first solution. because items of hash
, set
not have expiration property. can set ttl
entire hash
or set
, not elements.
Comments
Post a Comment