springboot操作redis出現的問題


對代碼進行加redis分布式鎖

    public String addRedisCache(String catalogJSON,ValueOperations<String, String> operations){
        if (StringUtils.isEmpty(catalogJSON)){
            String uuid = UUID.randomUUID().toString();
            Boolean lock = redisTemplate.opsForValue().setIfAbsent("lock", uuid,300,TimeUnit.SECONDS);
            if (lock) {
//                redisTemplate.expire("lock",30, TimeUnit.SECONDS);//設置過期時間
                try {
                    Map<String, List<Catelog2Vo>> catalogJsonFromDb = getCatalogJsonFromDb();
                    catalogJSON = JSON.toJSONString(catalogJsonFromDb);
                    if (!StringUtils.isEmpty(catalogJSON)) {
                        operations.set("catalogJSON", catalogJSON, 1, TimeUnit.DAYS);
                    }
                }finally {
                    String script = "if redis.call('get',KEYS[1]) == ARGV[1] then return redis.call('del',KEYS[1]) else return 0 end";
                    redisTemplate.execute(new DefaultRedisScript<Integer>(script,Integer.class),Arrays.asList("lock"),uuid);
                }
                //解鎖
//                String lock1 = operations.get("lock");
//                if (lock1.equals(uuid)){
//                    redisTemplate.delete("lock");
//                }
                //使用lua腳本
            }else{
                addRedisCache(catalogJSON,operations);
            }
        }
        return catalogJSON;
    }

  使用lua腳本進行解鎖出現異常

java.lang.IllegalStateException: null
	at io.lettuce.core.output.CommandOutput.set(CommandOutput.java:85) ~[lettuce-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
	at io.lettuce.core.protocol.RedisStateMachine.safeSet(RedisStateMachine.java:358) ~[lettuce-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
	at io.lettuce.core.protocol.RedisStateMachine.decode(RedisStateMachine.java:139) ~[lettuce-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
	at io.lettuce.core.protocol.CommandHandler.decode(CommandHandler.java:716) ~[lettuce-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
	at io.lettuce.core.protocol.CommandHandler.decode0(CommandHandler.java:680) ~[lettuce-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
	at io.lettuce.core.protocol.CommandHandler.decode(CommandHandler.java:675) ~[lettuce-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
	at io.lettuce.core.protocol.CommandHandler.decode(CommandHandler.java:596) ~[lettuce-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
	at io.lettuce.core.protocol.CommandHandler.channelRead(CommandHandler.java:565) ~[lettuce-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:377) ~[netty-transport-4.1.45.Final.jar:4.1.45.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:363) ~[netty-transport-4.1.45.Final.jar:4.1.45.Final]
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:355) ~[netty-transport-4.1.45.Final.jar:4.1.45.Final]
	at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410) ~[netty-transport-4.1.45.Final.jar:4.1.45.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:377) ~[netty-transport-4.1.45.Final.jar:4.1.45.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:363) ~[netty-transport-4.1.45.Final.jar:4.1.45.Final]
	at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919) ~[netty-transport-4.1.45.Final.jar:4.1.45.Final]
	at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163) ~[netty-transport-4.1.45.Final.jar:4.1.45.Final]
	at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:714) ~[netty-transport-4.1.45.Final.jar:4.1.45.Final]
	at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:650) ~[netty-transport-4.1.45.Final.jar:4.1.45.Final]
	at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:576) ~[netty-transport-4.1.45.Final.jar:4.1.45.Final]
	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493) ~[netty-transport-4.1.45.Final.jar:4.1.45.Final]
	at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989) ~[netty-common-4.1.45.Final.jar:4.1.45.Final]
	at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.45.Final.jar:4.1.45.Final]
	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.45.Final.jar:4.1.45.Final]
	at java.lang.Thread.run(Thread.java:745) [na:1.8.0_121]

  解決方法:

1、new DefaultRedisScript<Integer>(script,Integer.class)修改成 new DefaultRedisScript<>(script,Long.class)
2、修改配置:使用jedis,不使用lettuce


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM