当前位置:网站首页>Redis redisTemplate.execute 执行锁
Redis redisTemplate.execute 执行锁
2022-08-09 23:33:00 【川流不息的车呀】
Redis 脚本执行redis锁代码如下:
/**
* 加锁
*/
public final static String SCRIPT_LOCK = "local key = KEYS[1];\n"
+ "local value = ARGV[1];\n"
+ "local ttlMillis = tonumber(ARGV[2]);\n"
+ "if ( redis.call('SETNX', key, value) == 1 ) then\n"
+ " redis.call('PEXPIRE', key, ttlMillis);\n"
+ " return true;\n"
+ "end;";
public boolean tryLock(String key, String val, Long timeMillis) {
return tryLock(RedisConstants.KEY + key,SCRIPT_LOCK, val, timeMillis,
TimeUnit.MILLISECONDS);
}
public boolean tryLock(String key, String scriptText, String val, Long timeout, TimeUnit unit) {
DefaultRedisScript<Boolean> script = new DefaultRedisScript<>();
script.setScriptText(scriptText);
script.setResultType(Boolean.class);
long rawTimeout = TimeoutUtils.toMillis(timeout, unit);
return redisTemplate.execute(script, Collections.singletonList(key), val, rawTimeout);
}
@Configuration
public class RedisTemplateAutoConfiguration {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory rcf) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(rcf);
//使用StringRedisSerializer对Key序列号,使用Jackson对Value序列化
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringRedisSerializer);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashKeySerializer(stringRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
边栏推荐
- 【Infiltration tool】Browser data export tool
- [Cloud native] Kubernetes orchestration tools
- 南大通用数据库-Gbase-8a-学习-04-部署分布式集群
- 共创 Ray 中文社区,Ray Forward Meetup 2022 直播邀你参加!
- 程序员从佩洛西窜访事件中可以学到什么?
- redis分布式锁代码示例
- 深度剖析 Apache EventMesh 云原生分布式事件驱动架构
- 7. type( )函数——查询数据类型
- Distributed database problem (3): data consistency
- 深入理解Aarch64内存管理
猜你喜欢
[C language] In-depth understanding of pointers and arrays (issue 4)
信息系统项目管理师核心考点(六十四)信息安全基础知识重要概念
[Cloud native] Kubernetes orchestration tools
GoLang 使用 goroutine 停止的几种办法
Creo5.0入门教程赠素材
RebatMq消息中间件(一) 各个中间件介绍
YOLOV5 study notes (7) - training your own data set
AirFlow介绍
[C language] Address book "Static Memory Version"
LSTM-based distributed energy generation prediction (Matlab code implementation)
随机推荐
工程 (七) ——PolarSeg点云语义分割
如何抑制告警风暴?
C语言学习之旅 【操作符(残缺版)】
程序员从佩洛西窜访事件中可以学到什么?
FreeRTOS任务基础
CST Studio Suite 2021软件安装包和安装教程
Creo5.0入门教程赠素材
下载markdown软件Obsidian(解决官网下载速度慢)
服务发现@EnableDiscoveryClient
go语言的并发原理(goroutine)
Alibaba Cloud SMS Service Activation
ECCV 2022 | Microsoft Open Source TinyViT: Pre-training Capabilities for Small Models
MATLB|And her ups and downs and finally reached the peak of life [Romantic Journey]
[SSH]如何敲一条线
【集训DAY3】挖金矿【二分答案】
博弈小游戏
【集训DAY5】堆箱子【数学】
算法---整数替换(Kotlin)
基于 LSTM 的分布式能源发电预测(Matlab代码实现)
无源晶振负载电容值CL匹配方法及说明