I am setting up Redis for my application, which will serve hundreds of companies. I am considering a key structure like this:
companyname1:xxx:yyy:zzz
companyname1:yyy
companyname1:qqq:zz:xxx:ppp
companyname1:tttt:zz:qqq
companyName1:iiii:rrrr
companyName1:ppp:ddd:xxx
[... thousands more]
Each company would follow a similar pattern. I need to be able to remove all keys that contain “xxx” at any position, but only for companyname1.
How can I organize it so that Redis does not have to search through the entire database? Is there a way to group keys for each company and then operate only on that specific group?
I initially did something like this, but I do not know if it has the right to work, and most importantly, if it is efficient. I’m using StackExchange.
var keysToDelete = new List<RedisKey>();
var cursor = 0L;
var pageSize = 100;
// pattern = "companyName1::xxx:";
do
{
var redisResult = await _database.ExecuteAsync("SCAN", cursor.ToString(), "MATCH", pattern, "COUNT", pageSize).ConfigureAwait(false);
var result = (RedisResult[])redisResult;
cursor = Convert.ToInt64((string)result[0]);
keysToDelete.AddRange((RedisKey[])result[1]);
}
while (cursor != 0);
I would appreciate any guidance.
Remove keys for single companay, pattern to remove can be anywhere in the key
Tomasz Cioch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.