Use LUA scripts in spring boot

Source: Internet
Author: User
Tags lua

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.