在Redis的Azure的缓存/数据高速缓存作风地区 [英] Azure Cache/DataCache style Regions in Redis

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

问题描述

我在移动上一个C#ASP.Net Web应用程序的Azure的规划过程(目前主持一个专用的服务器上),并期待在缓存选项。目前,因为我们只在同一时间都运行的应用程序的一个实例,我们有一个'过程'内存缓存,以减轻一些相同的请求的SQL数据库。

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.

在目前的过程是清除缓存的某些部分时,经理/服务进行更改数据库,例如那些部分我们有一个用户表,我们将有一个像钥匙的用户。{0}返回单个用户记录/对象和Users.ForeignKey。{0}回归与外键的所有用户。如果我们更新一个用户记录,然后我们去掉User.1键(如果用户ID = 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 Redis的缓存服务不可用,至少支持,因此我们看到了在Azure缓存服务的基础上的AppFabric 。使用此,我们决定,我们将使用数据高速缓存区分开不同的对象类型,然后只是刷新了受到影响的区域,不是很确切的对我们目前的方法,但确定。现在,由于Redis的已经到了现场,我们一直在寻找这一点,将preFER尽可能使用它。然而,似乎实现,我们就必须有独立的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.

任何人知道如何实现类似于Azure的数据高速缓存区一些与Redis的,或者你可以建议一些昭然若揭了我可能丢失。

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.

谢谢,
加雷思

Thanks, Gareth

更新:

我已经发现了一些的bash命令,可以做到通过模式删除键,包括在这里使用键的命令的工作和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?

感谢您的阅读,加雷思

推荐答案

您可以使用此方法,它利用异步/等待功能和<一个href=\"https://github.com/StackExchange/StackExchange.Redis/blob/master/Docs/PipelinesMultiplexers.md\">redis流水线以利用模式删除键堆叠交换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天全站免登陆