Azure Cache/DataCache style Regions in Redis

前端 未结 2 502
感动是毒
感动是毒 2021-01-15 02:12

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

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-15 02:53

    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();
        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.

提交回复
热议问题