zoukankan      html  css  js  c++  java
  • redis 锁

    demo1
    public ErrorCode initDemo1(@RequestParam("orderNo") String orderNo) throws IOException {
            String lockKey = KEY + orderNo;
            Boolean hasKey = null;
            try {
                //锁判断
                hasKey = redisTemplate.hasKey(lockKey);
                
                int index = 0;
                while (hasKey && index < 3) {
                    log.info(">>>>>>>>>>>>>刷新,wait>>>>>>>>>>>>>");
                    index++;
                    Thread.sleep(1500L * index);
                    hasKey = redisTemplate.hasKey(lockKey);
                }
                if (index > 0) {
                    log.info(">>>>>>>>>>>>>wait index:{} hasKey: {}", index, hasKey);
                }
                
                //加锁
                redisTemplate.opsForValue().set(lockKey, "1", 5, TimeUnit.SECONDS);
                
                //业务操作-刷新es todo 业务逻辑
    
                //去锁
                redisTemplate.delete(lockKey);
                return ErrorCode.SUCCESS;
            } catch (Exception e) {
                //去锁
                redisTemplate.delete(lockKey);
                return ErrorCode.SYS_ERROR;
            } 
        }
        
    
    demo2    
    public ErrorCode initDemo2(@RequestParam("orderNo") String orderNo) throws IOException {
            String lockKey = KEY + orderNo;
            Boolean hasKey = null;
            try {
                hasKey = lock(lockKey, orderNo, 5);
                if(hasKey != null && hasKey) {
                    //业务操作-刷新es todo 业务逻辑
                }else {
                    return ErrorCode.LOCK_FAILED;
                }
                return ErrorCode.SUCCESS;
            } catch (Exception e) {
                return ErrorCode.SYS_ERROR;
            } finally{
                if(hasKey != null && hasKey) {
                    redisTemplate.delete(lockKey);
                }
            }
        }
        
        
        public boolean lock(String key, String value, long releaseTime) {
            // 尝试获取锁  spring-data-redis 2.1版本以上     //implementation group: 'org.springframework.data', name: 'spring-data-redis', version: '2.1.0.RELEASE'
    //         Boolean boo = redisTemplate.opsForValue().setIfAbsent(key, value, releaseTime, TimeUnit.SECONDS); //.setIfAbsent(key, value, releaseTime, TimeUnit.SECONDS);
             // 判断结果
    //         return boo != null && boo;
            
            redisTemplate.setEnableTransactionSupport(true);
            redisTemplate.multi();
            redisTemplate.opsForValue().setIfAbsent(key,value);
            redisTemplate.expire(key,releaseTime, TimeUnit.SECONDS);
            List result = redisTemplate.exec(); // 这里result会返回事务内每一个操作的结果,如果setIfAbsent操作失败后,result[0]会为false。
            if(result != null && true == (Boolean)result.get(0)){
                return true;
            }else {
                return false;
            }
        }
  • 相关阅读:
    Netty4学习笔记
    Essential Netty in Action 《Netty 实战(精髓)》
    【转】netty案例
    【转】Delphi TClientDataSet的使用
    Macbook brew 安装软件 一直updating Homebrew
    PHP的gc_collect_cycles函数
    Android开发手记之android.content.res.Resources$NotFoundException: Resource ID #0x7f04005e
    Android开发手记之duplicate entry: res/drawable/a.xml
    Android开发手记之Native Crash
    Java与C、C++的10大区别-总结
  • 原文地址:https://www.cnblogs.com/oktokeep/p/15001100.html
Copyright © 2011-2022 走看看