基于 Redis 实现 CAS 操作
基于 Redis 实现 CAS 操作
Intro
在 .NET 里并发情况下我们可以使用 Interlocked.CompareExchange 来实现 CAS (Compare And Swap) 操作,在分布式的情景下很多时候我们都会使用 Redis ,最近在改之前做的一个微信小游戏项目,之前是单机运行的,有些数据存储是基于内存的,直接基于对象操作的,最近要改成支持分布式的,于是引入了 redis,原本基于内存的数据就要迁移到 redis 中存储,原来的代码里有一些地方使用了 Interlocked.CompareExchange 来实现 CAS 操作,迁移到 redis 中之后也需要类似的功能,于是就想基于 redis 实现 CAS 操作。
CAS
CAS (Compare And Swap) 通常可以使用在并发操作中更新某一个对象的值,CAS 是无锁操作,CAS 相当于是一种乐观锁,而直接加锁相当于是悲观锁,所以相对来说 CAS 操作 是会比直接加锁更加高效的。
Redis Lua
redis 从 2.6.0 版本开始支持 Lua 脚本,Lua 脚本的执行是原子性的,所以我们在实现基于 redis 的分布式锁释放锁的时候或者下面要介绍的实现CAS 操作的,要执行多个操作但是希望操作是原子操作的时候就可以借助 Lua 脚本来实现(也可以使用事务来做)
基于 Redis Lua 实现 CAS
String CAS Lua Script:
KEYS[1] 对应要操作的String 类型的 redis 缓存的 key,ARGV[1]对应要比较的值,值相同则更新成 ARGV[2],并返回 1,否则返回 0
if redis.call(""get"", KEYS[1]) == ARGV[1] then
redis.call(""set"", KEYS[1], ARGV[2])
return 1
else
return 0
end
Hash CAS Lua Script:
KEYS[1] 对应要操作的 Hash 类型的 redis 缓存的 key,ARGV[1] 对应 Hash 的 field,ARGV[2]对应要比较的值,值相同则更新成 ARGV[3],并返回 1,否则返回 0
if redis.call(""hget"", KEYS[1], ARGV[1]) == ARGV[2] then
redis.call(""hset"", KEYS[1], ARGV[1], ARGV[3])
return 1
else
return 0
end
基于 StackExchange.Redis 的实现
为了方便使用,基于 IDatabase 提供了几个方便使用的扩展方法,实现如下:
public static bool StringCompareAndExchange(this IDatabase db, RedisKey key, RedisValue newValue, RedisValue originValue)
{
return (int)db.ScriptEvaluate(StringCasLuaScript, new[] { key }, new[] { originValue, newValue }) == 1;
}
public static async Task<bool> StringCompareAndExchangeAsync(this IDatabase db, RedisKey key, RedisValue newValue, RedisValue originValue)
{
return await db.ScriptEvaluateAsync(StringCasLuaScript, new[] { key }, new[] { originValue, newValue })
.ContinueWith(r => (int)r.Result == 1);
}
public static bool HashCompareAndExchange(this IDatabase db, RedisKey key, RedisValue field, RedisValue newValue, RedisValue originValue)
{
return (int)db.ScriptEvaluate(HashCasLuaScript, new[] { key }, new[] { field, originValue, newValue }) == 1;
}
public static async Task<bool> HashCompareAndExchangeAsync(this IDatabase db, RedisKey key, RedisValue field, RedisValue newValue, RedisValue originValue)
{
return await db.ScriptEvaluateAsync(HashCasLuaScript, new[] { key }, new[] { field, originValue, newValue })
.ContinueWith(r => (int)r.Result == 1);
}
实际使用
使用可以参考下面的测试代码:
[Fact]
public void StringCompareAndExchangeTest()
{
var key = "test:String:cas";
var redis = DependencyResolver.Current
.GetRequiredService<IConnectionMultiplexer>()
.GetDatabase();
redis.StringSet(key, 1);
// set to 3 if now is 2
Assert.False(redis.StringCompareAndExchange(key, 3, 2));
Assert.Equal(1, redis.StringGet(key));
// set to 4 if now is 1
Assert.True(redis.StringCompareAndExchange(key, 4, 1));
Assert.Equal(4, redis.StringGet(key));
redis.KeyDelete(key);
}
[Fact]
public void HashCompareAndExchangeTest()
{
var key = "test:Hash:cas";
var field = "testField";
var redis = DependencyResolver.Current
.GetRequiredService<IConnectionMultiplexer>()
.GetDatabase();
redis.HashSet(key, field, 1);
// set to 3 if now is 2
Assert.False(redis.HashCompareAndExchange(key, field, 3, 2));
Assert.Equal(1, redis.HashGet(key, field));
// set to 4 if now is 1
Assert.True(redis.HashCompareAndExchange(key, field, 4, 1));
Assert.Equal(4, redis.HashGet(key, field));
redis.KeyDelete(key);
}
References
- https://redis.io/commands/eval
- https://redisbook.readthedocs.io/en/latest/feature/scripting.html
- https://github.com/WeihanLi/WeihanLi.Redis/blob/dev/src/WeihanLi.Redis/RedisExtensions.cs
- https://github.com/WeihanLi/WeihanLi.Redis/blob/dev/test/WeihanLi.Redis.UnitTest/RedisExtensionsTest.cs
基于 Redis 实现 CAS 操作的更多相关文章
- 基于Redis的CAS服务端集群
为了保证生产环境CAS(Central Authentication Service)认证服务的高可用,防止出现单点故障,我们需要对CAS Server进行集群部署. CAS的Ticket默认是以Ma ...
- apscheduler(定时任务) 基于redis持久化配置操作
apscheduler(定时任务) 基于redis持久化配置操作 安装模块 pip install apscheduler 导入模块配置 ## 配置redis模块 from apscheduler.j ...
- 基于redis的cas实现
cas是我们常用的一种解决并发问题的手段,小到CPU指令集,大到分布式存储,都能看到cas的影子.本文假定你已经充分理解一般的cas方案,如果你还不知道cas是什么,请自行百度 我们在进行关系型数据库 ...
- 基于redis的cas集群配置(转)
1.cas ticket统一存储 做cas集群首先需要将ticket拿出来,做统一存储,以便每个节点访问到的数据一致.官方提供基于memcached的方案,由于项目需要,需要做计入redis,根据官方 ...
- 基于redis的cas集群配置
1.cas ticket统一存储 做cas集群首先需要将ticket拿出来,做统一存储,以便每个节点访问到的数据一致.官方提供基于memcached的方案,由于项目需要,需要做计入redis,根据官方 ...
- 基于Redis的CAS集群
单点登录(SSO)是复杂应用系统的基本需求,Yale CAS是目前常用的开源解决方案.CAS认证中心,基于其特殊作用,自然会成为整个应用系统的核心,所有应用系统的认证工作,都将请求到CAS来完成.因此 ...
- 基于CAS操作的非阻塞算法
非阻塞算法(non-blocking algorithms)定义 所谓非阻塞算法是相对于锁机制而言的,是指:一个线程的失败或挂起不应该引起另一个线程的失败或挂起的一种算法.一般是利用硬件 ...
- 【连载】redis库存操作,分布式锁的四种实现方式[三]--基于Redis watch机制实现分布式锁
一.redis的事务介绍 1. Redis保证一个事务中的所有命令要么都执行,要么都不执行.如果在发送EXEC命令前客户端断线了,则Redis会清空事务队列,事务中的所有命令都不会执行.而一旦客户端发 ...
- 基于redis分布式缓存实现(新浪微博案例)
第一:Redis 是什么? Redis是基于内存.可持久化的日志型.Key-Value数据库 高性能存储系统,并提供多种语言的API. 第二:出现背景 数据结构(Data Structure)需求越来 ...
随机推荐
- 吴裕雄--天生自然python学习笔记:python 创建和操作Firebase实时数据库
Fire base 是专为移动开发提供的后端服务平台, Firebase 数据库与传统数据库不同,它不是用数据表来存储数据, 而是用 Key . Value 的字典型结构来存储数据,所以它不仅 是轻量 ...
- Uber退出东南亚留下烂摊子,给“中国式并购”带来哪些启示
当下,从全球范围内来看很多互联网企业都采用了"复制+粘贴"的疯狂推进模式.它们往往在某一个国家或地区取得领先优势后,就快速将相同模式在全球推进去占领当地市场.无论结果是一家独大占据 ...
- 区别 new function(){} 和 function(){}()
只要 new 表达式之后的 constructor 返回(return)一个引用对象(数组,对象,函数等),都将覆盖new创建的匿名对象,如果返回(return)一个原始类型(无 return 时其实 ...
- SSH免密码登陆详解
为了更好的理解SSH免密码登陆原理,我们先来说说SSH的安全验证,SSH采用的是”非对称密钥系统”,即耳熟能详的公钥私钥加密系统,其安全验证又分为两种级别. 1. 基于口令的安全验证 这种方式使用用户 ...
- [LC] 572. Subtree of Another Tree
Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and no ...
- python数据类型:字典Dictionary
python数据类型:字典Dictionary 字典是一种可变容器模型,可以存储任意类型对象 键是唯一的,但是值不需要唯一 值可以取任何数据类型,但是键必须是不可变的,如字符串,数字,元组 创建字典: ...
- MS12-020 3389蓝屏攻击
MS12-020 3389蓝屏攻击 search ms12_020 use exploit/dos/windows/rdp/ms12_020_maxchannelids set rhost 192.1 ...
- FFT算法的verilog实现
首先需要明白傅里叶相关的基本知识:还是 借用这位英雄的文章,真心写的让人佩服不已http://blog.jobbole.com/70549/ 然后是卷积的理解http://blog.csdn.net/ ...
- [LC] 238. Product of Array Except Self
Given an array nums of n integers where n > 1, return an array output such that output[i] is equ ...
- Python实现:生产者消费者模型(Producer Consumer Model)
#!/usr/bin/env python #encoding:utf8 from Queue import Queue import random,threading,time #生产者类 clas ...