count of keys matching a pattern

shanks picture shanks · Dec 6, 2013 · Viewed 64.6k times · Source

How can I find the count of all the keys that has a matching pattern.

For example, there are two keys abc:random-text-1 and abc:random-text-2 . The common pattern here isabc: . So, here the count is 2.

How can I do this in redis?

Answer

warvariuc picture warvariuc · Nov 11, 2014

From here:

eval "return #redis.pcall('keys', 'abc:*')" 0

It's not O(1), but at least the count is done on the server side.