12

I'm trying to use a pattern to retrieve all keys matching a pattern by Stackexchange.Redis.

Code

KEYS *o*
2
  • 2
    Are you aware that you shouldn't (in general) use the KEYS statement in live setups? Reason: O(n). See the docs, that's why SCAN is implemented. Commented Aug 16, 2014 at 19:56
  • @TwBert thanks.. Redis in Sort statement use SCAN? Commented Aug 17, 2014 at 6:45

1 Answer 1

10

On the project homepage is linked Where are KEYS, SCAN, FLUSHDB etc? which gives full details on how to access this, and why it isn't on IDatabase. I should point out that you should avoid KEYS on a production server. The library will automatically try to use SCAN instead if it is available - which is less harmful but should still be treated with some caution. It would be preferable to explicitly store related keys in a set or hash.

Sign up to request clarification or add additional context in comments.

4 Comments

@MarkGravell, would you please clarify why the server.Keys(pattern: "foo") shouldn't be used if we are using 2.8+ instance of Redis? I read the link/page you refer to but it seems to contradict the advice given at redis.io/commands/scan where it says: "Since these commands allow for incremental iteration, returning only a small number of elements per call, they can be used in production without the downside of commands like KEYS".
@Jeff I said it should still be "treated with some caution" - not that it shouldn't be used. Frankly, redis is just slow for this scenario (on a busy server) - it is O(N) for N=number of keys on server. I would advocate using a set that contains the active keys for your scenario, so you can use SMEMBERS / SSCAN
@MarcGravell If you do that, how do you deal with items that expire? If I store an item with a TTL of N minutes, after that time the key name will still be in my list but my saved item will be gone and my list of keys will be stale.
@howcheng indeed; if you go with key sets, your code would need to deal with missing records appropriately; you could also use the SORT instruction, which allows the GET modifier to be used to perform a "gather" operation (essentially doing an "inner join", in SQL terms)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.