Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis: set maximum number of elements of a set

Tags:

redis

In redis, is it possible to set a maximum number of elements to a set so when one use the sadd, the redis server prevents the set from having more elements that a maximum amount? e.g. something like :

127.0.0.1:6379> SETSIZE KEY 100

Thanks in advance.

like image 913
Geoffrey R. Avatar asked Jan 12 '16 08:01

Geoffrey R.


1 Answers

No, it`s not possible with usual commands but possible with LUA scripting:

local size = redis.call('SCARD', KEYS[1]);
if size < tonumber(ARGV[1], 10) then 
    return redis.call('SADD', KEYS[1], ARGV[2]);
end  
return -1;
like image 161
Nick Bondarenko Avatar answered Sep 28 '22 17:09

Nick Bondarenko