Selaa lähdekoodia

setnx代码 未测试有问题

dinglan 2 vuotta sitten
vanhempi
commit
47e270e99a

+ 1 - 0
src/main/java/com/sw/config/RedisConfig.java

@@ -28,6 +28,7 @@ public class RedisConfig {
         redisTemplate.setHashKeySerializer(redisSerializer);
         RedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
         redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
+        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
         return redisTemplate;
     }
 

+ 30 - 1
src/test/java/com/sw/RedisTest.java

@@ -5,10 +5,14 @@ import com.sw.dao.SysUserDao;
 import com.sw.dao.TMaskDao;
 import com.sw.dao.TOrderDao;
 import com.sw.service.TMaskService;
+import io.lettuce.core.SetArgs;
+import io.lettuce.core.api.async.RedisAsyncCommands;
+import io.lettuce.core.cluster.api.async.RedisAdvancedClusterAsyncCommands;
 import org.junit.Assert;
 import org.junit.Test;
 import org.junit.jupiter.api.Order;
 import org.junit.runner.RunWith;
+import org.redisson.api.RedissonClient;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.data.redis.core.RedisTemplate;
@@ -75,7 +79,32 @@ public class RedisTest {
     public void decr(){
 //        Long res = redisTemplate.opsForHash().increment("usableMasks", "mask-1", -5l);
 //        System.out.println(res);
-        redisTemplate.opsForValue().set("remain", 100);
+        byte[] key = redisTemplate.getKeySerializer().serialize("mask-setnx-mq");
+        byte[] value = redisTemplate.getValueSerializer().serialize(1);
+
+        Object nativeConnection = redisTemplate.getConnectionFactory().getConnection().getNativeConnection();
+        //lettuce连接下 redis单机setnx
+        if(nativeConnection instanceof RedisAsyncCommands){
+            RedisAsyncCommands commands = (RedisAsyncCommands) nativeConnection;
+            String res = commands.getStatefulConnection().sync()
+                    .set(key, value, SetArgs.Builder.nx().ex(10));
+            System.out.println(res);
+        }
+        if(nativeConnection instanceof RedissonClient){
+            RedisAsyncCommands commands = (RedisAsyncCommands) nativeConnection;
+            String res = commands.getStatefulConnection().sync()
+                    .set(key, value, SetArgs.Builder.nx().ex(10));
+            System.out.println(res);
+        }
+        //lettuce连接下 redis集群setnx
+        if(nativeConnection instanceof RedisAdvancedClusterAsyncCommands){
+            RedisAdvancedClusterAsyncCommands commands = (RedisAdvancedClusterAsyncCommands) nativeConnection;
+            String res = commands.getStatefulConnection().sync()
+                    .set(key, value, SetArgs.Builder.nx().ex(10));
+            System.out.println(res);
+        }
+//        racac.getStatefulConnection().async().setnx("")
+//        redisTemplate.opsForValue().set("remain", 100);
     }
 
     @Test