我有一个redis服务器,我想实现一个原子(或伪原子)方法,它将执行以下操作(注意:我有一个与redis服务器有多个会话的系统):
我不想用函数F预先生成一个值(在检查值是否存在之前),并在键不存在时使用它的原因是:
我创建的python伪代码是:
import redis
r = redis.StrictRedis(host='localhost', port=6379, db=0)
''' gets the value of key K if exists (r.get(K) if r.exists(K)),
otherwise gets the value of key K if calling SETNX function returned TRUE
(else r.get(K) if r.setnx(K,F())), meaning this the sent value is really the value,
otherwise, get the value of key K, that was generated by another session a
short moment ago (else r.get(K))
The last one is redundant and I can write "**r.setnx(K,F()) or True**" to get the
latest value instead, but the syntax requires an "else" clause at the end '''
r.get(K) if r.exists(K) else r.get(K) if r.setnx(K,F()) else r.get(K)
还有别的解决办法吗?在
是的,您可以使用WATCH。下面是一个用redis-py修改的示例:
相关问题 更多 >
编程相关推荐