Writing LUA Scripts
This script function: first check whether the value of a key in Redis is consistent with the expected value V1, if it is consistent, modify it to a new value V2 and return True, otherwise false. Actually, it's CAs.
Local current = Redis.call ('GET', keys[1])if current = = argv[ 1 ] Then redis.call ('SET', keys[1], argv[2]) return true End return false
Note that the variables in the Lua script are local and cannot be global variables. Otherwise you will get an error. See HTTP://DOC.REDISFANS.COM/SCRIPT/EVAL.HTML#ID6
Loading LUA scripts with Defaultredisscript
@Bean Public Defaultredisscript<boolean> Redisscript () { defaultredisscriptnew Defaultredisscript <>(); Redisscript.setscriptsource (New Resourcescriptsource (new Classpathresource (" Lua/checkandset.lua " ))); Redisscript.setresulttype (Boolean. class ); return redisscript; }
Test
@Autowired Defaultredisscript<Boolean>Redisscript; /** * Test redis LUA * * @return*/@RequestMapping (Value="Testredislua", method ={requestmethod.get}) @ResponseBody PublicObject 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","3333"); S= Redistemplate.opsforvalue ().Get(key); Log.info (s); return NULL; }
Use LUA scripts in spring boot