编写LUA脚本

  该脚本功能:先检查redis中某个key的值是否与期望的值V1一致,如果一致则将其修改为新的值V2并返回true,否则返回false。其实就是CAS。

local current = redis.call('GET', KEYS[])
if current == ARGV[]
then redis.call('SET', KEYS[], ARGV[])
return true
end
return false

  注意,lua脚本中的变量都要是local 的,不可以是全局变量。否则会报错。详见 http://doc.redisfans.com/script/eval.html#id6

使用DefaultRedisScript加载lua脚本

@Bean
public DefaultRedisScript<Boolean> redisScript() {
DefaultRedisScript<Boolean> redisScript = new DefaultRedisScript<>();
redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("lua/checkandset.lua")));
redisScript.setResultType(Boolean.class);
return redisScript;
}

测试

@Autowired
DefaultRedisScript<Boolean> redisScript;
/**
* 测试redis lua
*
* @return
*/
@RequestMapping(value = "testredislua", method = {RequestMethod.GET})
@ResponseBody
public Object testredislua() {
String key = "testredislua"; redisTemplate.delete(key);
redisTemplate.opsForValue().set(key, "hahaha");
String s = redisTemplate.opsForValue().get(key);
log.info(s); redisTemplate.execute(redisScript, Collections.singletonList(key), "hahaha", "");
s = redisTemplate.opsForValue().get(key);
log.info(s); return null;
}

spring boot 中使用LUA脚本的更多相关文章

  1. Spring Boot中通过CORS解决跨域问题

    今天和小伙伴们来聊一聊通过CORS解决跨域问题. 同源策略 很多人对跨域有一种误解,以为这是前端的事,和后端没关系,其实不是这样的,说到跨域,就不得不说说浏览器的同源策略. 同源策略是由Netscap ...

  2. spring boot中使用@Async实现异步调用任务

    本篇文章主要介绍了spring boot中使用@Async实现异步调用任务,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看看吧 什么是“异步调用”? “异步调用”对应的是“同步 ...

  3. 学习Spring Boot:(二十三)Spring Boot 中使用 Docker

    前言 简单的学习下怎么在 Spring Boot 中使用 Docker 进行构建,发布一个镜像,现在我们通过远程的 docker api 构建镜像,运行容器,发布镜像等操作. 这里只介绍两种方式: 远 ...

  4. 如何在Spring Boot中使用Cookies

    一. 导读 本文大纲 读取HTTP Cookie 设置HTTP Cookie 读取所有Cookie[] 为Cookie设置过期时间 Https与Cookie HttpOnly Cookie 删除Coo ...

  5. 在Spring Boot中加载初始化数据

    文章目录 依赖条件 data.sql文件 schema.sql 文件 @sql注解 @SqlConfig 注解 在Spring Boot中加载初始化数据 在Spring Boot中,Spring Bo ...

  6. 在 Spring Boot 中使用 Flyway

    一.Flyway 介绍 Flyway 是一个开源的数据库迁移工具,MySQL, SQL Server, Oracle 等二十多种数据库 在 Flyway 中数据库的所有改变均称为迁移(migratio ...

  7. spring boot(三):Spring Boot中Redis的使用

    spring boot对常用的数据库支持外,对nosql 数据库也进行了封装自动化. redis介绍 Redis是目前业界使用最广泛的内存数据存储.相比memcached,Redis支持更丰富的数据结 ...

  8. Spring Boot中的事务管理

    原文  http://blog.didispace.com/springboottransactional/ 什么是事务? 我们在开发企业应用时,对于业务人员的一个操作实际是对数据读写的多步操作的结合 ...

  9. Spring Boot中的注解

    文章来源:http://www.tuicool.com/articles/bQnMra 在Spring Boot中几乎可以完全弃用xml配置文件,本文的主题是分析常用的注解. Spring最开始是为了 ...

随机推荐

  1. linux安装dpkg安装缺少依赖项的解决

    问题: dpkg: error processing package rxvt:i386 (--install): dependency problems - leaving unconfigured ...

  2. 【转】利用virtualenv管理Python环境

    virtualenv virtualenv用于创建独立的Python环境,多个python相互独立,互不影响,它能够:1. 在没有权限的情况下安装新套件2. 不同应用可以使用不同的套件版本3. 套件升 ...

  3. 2019.01.19 codeforces343D.Water Tree(树剖+ODT)

    传送门 ODTODTODT板子题. 支持子树01覆盖,路径01覆盖,询问一个点的值. 思路:当然可以用树剖+线段树,不过树剖+ODTODTODT也可以很好的水过去. 注意修改路径时每次跳重链都要修改. ...

  4. Codeforces Round #543 (Div. 2)B,C

    https://codeforces.com/contest/1121 B 题意 给你n(<=1000)个数ai,找出最多对和相等的数,每个数只能用一次,且每个数保证各不相同 题解 重点:每个数 ...

  5. best-case analysis in real-time system

    ECRTS: Exact Best-Case Response Time Analysis of Fixed Priority Scheduled Tasks motivation Real-time ...

  6. adb push init.rc /

    http://blog.csdn.net/jumper511/article/details/28856249 修改Android init.rc文件后,需要将修改后的文件上传到手机,但是发下如下问题 ...

  7. xslt 和一个demo

    https://www.w3.org/1999/XSL/Transform Specifications The XSLT language has three versions which are ...

  8. ID、句柄、指针、对象互相转换

    /*************************************************************************************************** ...

  9. 【ORA错误大全】 ORA-19527

    在做主备切换的时候,需要将备库的联机日志文件清除(clear online redo logfile),为了加快switchover的速度,Oracle10g在将备库置于manged standby状 ...

  10. POJ2456--Aggressive cows(Binary Search) unsolved

    Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...