I am in the planning process of moving a C# ASP.Net web application over to Azure (currently hosted on a single dedicated server) and am looking at caching options. Currentl
For what I understand by your question you need to group your data according to some criteria (user in your case), so that whenever record related to that criteria is changed, all of data related to that record is also invalidated in cache using a single cache api call.
You can achieve this in Azure using NCache for Azure, a distributed caching solution for azure by Alachisoft which has a rich set of features along with multiple caching topologies.
NCache allows multiple ways to perform this type of operations. One suitable for your use case is data grouping feature that will allow you to group data in groups/subgroups on addition. Data can be later fetched/removed on the basis of groups/subgroups.
NCache also allows to add tags with items being added. These tags can then be used for removing/fetching all data containing one or more specified tags. Querying feature (Delete query) provided in NCache can also be used to remove data satisfying a particular criteria.
You can use this method which leverage the async/await features and redis pipelining to delete keys by pattern using stack exchange redis client
private static Task DeleteKeysByPatternAsync(string pattern)
{
IDatabase cache1 = Connection.GetDatabase();
var redisServer1 = Connection.GetServer(Connection.GetEndPoints().First());
var deleteTasks = new List<Task>();
var counter = 0;
foreach (var key in redisServer1.Keys(pattern: pattern, database: 0, pageSize: 5000))
{
deleteTasks.Add(cache1.KeyDeleteAsync(key));
counter++;
if (counter % 1000 == 0)
Console.WriteLine($"Delete key tasks created: {counter}");
}
return Task.WhenAll(deleteTasks);
}
Then you can use it like this:
DeleteKeysByPatternAsync("*user:*").Wait(); //If you are calling from main method for example where you cant use await.
or
await DeleteKeysByPatternAsync("*user:*"); //If you run from async method
You can tweak the pageSize or receive as method param.