Reputation: 53603
In redis (2.8.x) I have three entity types (I define the entities).
For simplicity, let's call them organizations, departments, users.
When I run scan, it iterates on all keys in the memory, but brings only what matches the match
clause.
Is there a way to restrict the scan
to only a subset of key? For example search
only the users group of keys?
Each group has a different (but consistent) key name pattern (org:[id] dep:[id] user:[id]
)
Upvotes: 1
Views: 774
Reputation: 10917
SCAN has an optional MATCH
argument that accepts a glob pattern.
You can do something like this to scan through your users objects:
SCAN 0 MATCH user:*
Edit as it is too long for a comment
Clearly scan is not a magic method, a complete cycle (until you receive "0" cursor) must go through all of your keyspace. The MATCH option does the filtering in the Redis side instead of doing it in your client.
As Itamar suggested in his comment, to be able to get the keys you need without going thorough the complete keyspace, you must index them in sets (You must also maintain it on removals).
For example, if your users keys are:
user:31 user31data
user:45 user45data
user:67 user67data
user:13 user13data
You should have a SET
of user IDs:
users:IDs {31, 45, 67, 13}
To retrieve all users data, and you probably wants both the ID and the data:
sort users:IDs by nosort get # get user:*
Upvotes: 2