当前位置:网站首页>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;
}
}边栏推荐
猜你喜欢

vmware Exsi 网卡配置

AUTOCAD——形位公差如何标注、CAD打断于点的操作

【云原生】Kubernetes编排工具精讲

Leetcode81. 搜索旋转排序数组 II

信息系统项目管理师核心考点(六十四)信息安全基础知识重要概念

ECCV 2022 | Microsoft Open Source TinyViT: Pre-training Capabilities for Small Models

算法---整数替换(Kotlin)

ES6 Beginner to Mastery #15: Generator Usage

CST Studio Suite 2021 software installation package and installation tutorial

mysql无法远程连接 Can‘t connect to MySQL server on ‘xxx.xxx.xxx.xxx‘ (10060 “Unknown error“)
随机推荐
Wireshark classic practice and interview 13-point summary
CST Studio Suite 2021 software installation package and installation tutorial
When knowledge and action are one
错误提示:Syntax error on token “function”, delete this token
MATLB|和她跌宕起伏最终到达人生之峰【浪漫旅途】
【集训DAY4】异或【字典树】
聚焦热点 | ISC 2022软件供应链安全治理与运营论坛圆满落幕
Seq2Seq论文阅读笔记
Redis 非关系型数据库学习(一) ---- Redis 的安装
In-depth understanding of multithreading (Part 1)
共创 Ray 中文社区,Ray Forward Meetup 2022 直播邀你参加!
selenium和驱动安装
Why don't suggest you run in Docker Mysql?
信息系统项目管理师核心考点(六十四)信息安全基础知识重要概念
ES6 从入门到精通 # 15:生成器 Generator 的用法
[Cloud native] Kubernetes orchestration tools
Leetcode82. 删除排序链表中的重复元素 II
dlopen failed: library “libtaml.so“ not found
CST Studio Suite 2021软件安装包和安装教程
JVM内存和垃圾回收-10.直接内存