1

I'm investigating the use of Redis in an asp.net mvc application using the ServiceStack.Redis client and a single Redis instance running on a remote machine.

Our cache is broken up into 3 major "areas" (Asp.net output cache, NHibernate second level cache, application cache) and I would like to have the ability able to "flush" all of the keys in these areas individually.

In Couchbase (which we currently utilize) this would be accomplished by using separate buckets with a client instance pointing to each one. We could then flush all values in these buckets using a single call from the appropriate client instance.

Is there a way to accomplish a setup like this using Redis? If so, how do I approach this from the client/server side?

4

1 回答 1

1

我可以想到三种方法来实现这一目标。

共享数据库 - 最糟糕的方法(强烈建议不要追求)

Redis 支持共享数据库,这些数据库基本上是由同一服务器管理的独立键空间。连接到 Redis 后,您可以使用SELECT语句在数据库之间切换,并且可以使用FLUSHDB命令单独刷新每个数据库。

临:

  • 这是获得所需内容的最简单方法。

缺点:

  • 共享数据库共享同一个 Redis 进程。由于 Redis(大部分)是单线程的,因此不建议使用共享数据库的做法,因为在一个数据库中的操作可能会干扰在同一服务器上运行的其他数据库。
  • 目前尚不清楚未来版本的 Redis 是否会继续支持共享数据库。

(有关共享与专用 Redis 数据库的更多信息,请查看我在http://redislabs.com/blog/benchmark-shared-vs-dedicated-redis-instances上的帖子)

一个数据库,不同的键前缀 - 稍微好一点,但仍然......

您可以使用单个 Redis 数据库并根据它们所属的“区域”为您的键添加前缀(例如,进入 ASP.Net 区域的键将以 'asp:' 等为前缀)。要删除一个区域,请使用SCAN命令使用相关的键名模式遍历键空间,然后DEL它返回的结果。

优点:想不出任何缺点:

  • 与上面相同 + 更多代码来实现 SCAN/DEL 例程。

专用数据库 - 推荐方法

为每个区域使用单独的 Redis 实例,简单明了。设置您的远程计算机以运行 3 个 Redis 服务器,每个服务器管理自己的密钥空间。要刷新,请连接到相关数据库并执行 FLUSHDB 或FLUSHALL

优点:

  • 使用单个命令刷新区域
  • 在远程机器上利用多个核心

缺点:

  • 可能意味着需要更多的管理工作来设置 3 个 Redis 服务器。

最后,如果您正在寻找一种轻松使用 Redis 的方法,我强烈建议您考虑将Redis Cloud作为在云中托管 Redis 服务的一个选项。我们是唯一一家允许您在同一订阅中设置多个专用Redis 数据库且无需额外费用的服务提供商。

于 2014-06-11T15:12:11.197 回答