阻止访问Redis的密钥(ServiceStack) [英] Block access to Redis key (ServiceStack)

查看:755
本文介绍了阻止访问Redis的密钥(ServiceStack)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现什么,我会觉得是使用Redis的ServiceStack客户一种很常见的缓存方案,但我很难找到一个很好的例子。

I'm trying to implement what I would think is a very common caching scenario using the ServiceStack Redis client, yet I'm having difficulty finding a good example of this.

在一个ASP.NET MVC应用,我们做一个相对长时间运行(和计量)调用外部Web服务并缓存用于在一定的时间段的结果。在高速缓存中执行,期望以阻止该键附加请求,直到web服务调用完成,以避免额外的(昂贵的)呼叫。

In an ASP.NET MVC app, we make a relatively long-running (and metered) call to an external web service and cache the results for a certain period of time. In the cache implementation, it is desired to block additional requests for that key until the web service call has completed, to avoid additional (expensive) calls.

那么,什么是要实现一键级锁的最好方法? Redis的是否支持这一开箱?将ServiceStack的 IRedisClient.AcquireLock 很适合这一点,或者是如果矫枉过正我们不处理分布式锁?或者,我会是最好的关闭只是实现自己锁,像描述这里

So, what is the best way to implement a key-level lock? Does Redis support this out of the box? Would ServiceStack's IRedisClient.AcquireLock be a good fit for this, or is it overkill if we're not dealing with distributed locks? Or would I be best off just implementing the lock myself, something like described here?

在此先感谢!

推荐答案

Redis是一个非阻塞异步服务器,没有内置的语义Redis的一个客户端连接,直到键上阻止是免费的。

Redis is a non-blocking async server, there are no semantics built-in to redis to block on a client connection until a key is free.

请注意:Redis是一个远程的NoSQL数据存储,因此任何锁定您实现涉及Redis是由设计分发。 ServiceStack的AcquireLock使用Redis的的原始的 SETNX 锁定语义,以确保只有1客户端连接有锁,所有其他客户端/连接保持阻塞,直到锁已被使用指数重试回退乘数轮询释放

Note: Redis is a remote NoSQL data store, therefore any lock you implement involving redis is 'distributed' by design. ServiceStack's AcquireLock uses redis's primitive SETNX locking semantics to ensure only 1 client connection has the lock, all other clients/connections are remain blocking until the lock has been freed by using an exponential retry back-off multiplier to poll.

为了实现一个分布式锁不轮询你需要创建一个使用的SETNX + Redis的的发布/订阅的支持相结合,以通知等待客户端的锁已经被释放的解决方案。

In order to implement a distributed lock without polling you'd need to create a solution that uses a combination of SETNX + redis's Pub/Sub support to notify waiting clients that the lock has been freed.

这篇关于阻止访问Redis的密钥(ServiceStack)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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