Redis 中的 Azure 缓存/数据缓存样式区域 [英] Azure Cache/DataCache style Regions in Redis

查看:42
本文介绍了Redis 中的 Azure 缓存/数据缓存样式区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在计划将 C# ASP.Net Web 应用程序迁移到 Azure(目前托管在单个专用服务器上),并且正在研究缓存选项.目前,因为我们一次只有一个应用程序实例在运行,所以我们有一个进程中"内存缓存来减轻一些相同请求的 SQL DB.

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. Currently, because we only have one instance of the application running at one time, we have an 'in process' memory cache to relieve the SQL DB of some identical requests.

目前的流程是在管理器/服务对数据库的某些部分进行更改时清除缓存的某些部分,例如我们有一个用户表,我们将有诸如User.{0}"返回单个用户记录/对象和Users.ForeignKey.{0}"之类的键,返回与外键相关的所有用户.如果我们更新单个用户记录,那么我们将删除User.1"键(如果 userid = 1),并且为了方便起见,所有列表集合都可能已更改.我们通过按模式删除键来实现这一点,这意味着只有受影响的键会被删除,而所有其他键都会保留.

The process at the moment is to clear certain parts of the cache when the managers/services make a change to those parts of the database, e.g. we have a users table and we'll have keys like "User.{0}" returning a single User record/object and "Users.ForeignKey.{0}" returning all users related to the foreign key. If we update a single user record then we remove the "User.1" key (if the userid = 1) and for ease all of the list collections as they could have changed. We do this by removing keys by pattern, this means that only the affected keys are removed and all others persist.

我们计划迁移到 Azure 已经有一段时间了,当我们第一次开始查看 Azure Redis 缓存服务不可用(至少受支持)时,我们查看了基于 AppFabric 的 Azure 缓存服务.使用这个,我们决定我们将使用 DataCache 区域来分隔不同的对象类型,然后只刷新受影响的区域,不像我们当前的方法那么精确,但可以.现在,由于 Redis 已经出现,我们一直在研究它,如果可能的话,我们更愿意使用它.但是,似乎要实现相同的目标,我们必须为每个区域"/部分拥有单独的 Redis 缓存,根据我的理解,这意味着我们将为 Azure 的许多小型 Redis 缓存服务实例付费,考虑到我们需要 10 多个单独的可刷新部分到缓存,这将花费很多.

We've been planning this move to Azure for a while now and when we first started looking at everything the Azure Redis Cache service wasn't available, at least supported, so we looked at the Azure Cache service, based on AppFabric. Using this we decided that we would use DataCache regions to separate the different object types and then just flush the region that was affected, not quite as exact as our current method but OK. Now, since Redis has come on to the scene, we've been looking at that and would prefer to use it if possible. However, it seems that to achieve the same thing we would have to have separate Redis caches for each 'Region'/section, which from how I understand it would mean we would pay for lots of small instances of the Redis Cache service from Azure which would cost quite a lot given that we would need 10+ separately flushable sections to the cache.

任何人都知道如何使用 Redis 实现类似于 Azure DataCache Regions 的功能,或者您能否提出一些我可能遗漏的显而易见的东西.

Anyone know how to achieve something similar to Azure DataCache Regions with Redis or can you suggest something glaringly obvious that I'm probably missing.

抱歉问了这么长的问题/解释,但我发现在没有背景/上下文的情况下很难解释我想要实现的目标.

Sorry for such a long question/explanation but I found it difficult to explain what I'm trying to achieve without background/context.

谢谢,加雷斯

更新:

我发现了一些 bash 命令可以完成按模式删除键的工作,包括使用KEYS"命令here 和 lua 脚本 EVAL 命令这里.

I've found a few bash commands that can do the job of deleting keys by pattern, including using the 'KEYS' command here and the lua script EVAL command here.

我打算使用 StackExchange.Redis 客户端进行交互,有谁知道在使用 StackExchange.Redis 时如何使用这些类型的命令或替代方法(按模式删除键)?

I'm planning on using the StackExchange.Redis client to interact, does anyone know how to use these types of commands or alternatives to those (to delete keys by pattern) when using StackExchange.Redis?

感谢阅读,加雷斯

推荐答案

您可以使用这种方法来利用 async/await 功能和 redis pipelining 使用堆栈交换redis客户端按模式删除键

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.

await DeleteKeysByPatternAsync("*user:*"); //If you run from async method

您可以调整 pageSize 或作为方法参数接收.

You can tweak the pageSize or receive as method param.

这篇关于Redis 中的 Azure 缓存/数据缓存样式区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆