zoukankan      html  css  js  c++  java
  • Redis学习笔记#10 lua脚本,整合springboot调用

    lua代码:

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

    配置:

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {

      RedisTemplate<String, Object> template = new RedisTemplate<>();  
      template.setConnectionFactory(factory);
      template.setKeySerializer(new StringRedisSerializer());
      template.setValueSerializer(new StringRedisSerializer());
      return template;
    }


    @Bean
    public RedisScript<Boolean> redisScript() {   DefaultRedisScript<Boolean> redisScript = new DefaultRedisScript<>(); ScriptSource scriptSource = new ResourceScriptSource(new ClassPathResource("META-INF/scripts/checkandset.lua")); redisScript.setResultType(Boolean.class); redisScript.setScriptSource(scriptSource); return redisScript;
    }

    调用:

    @Autowired
    RedisTemplate redisTemplate;
    
    @Autowired
    RedisScript<Boolean> redisScript;
    
    List<String> keys = new ArrayList<>();
    keys.add("stock");
    return redisTemplate.execute(redisScript, keys, 2);
    

      

  • 相关阅读:
    promethus监控JVM jar包
    ubuntu中文乱码解决办法
    IT焦躁中的赤子青年
    ftp neo4j http kafka搭建
    查看python脚本执行过程
    解决coredns-7f9c544f75-2sjs2一直处于ContainerCreating
    番茄工作法
    数据库的性能优化
    MyBatis
    CentOS下安装JDK,Tomcat,Redis,Mysql,及项目发布
  • 原文地址:https://www.cnblogs.com/sunang/p/12778875.html
Copyright © 2011-2022 走看看