Ver Fonte

feature: add Redis config

ggbocoder há 2 anos atrás
pai
commit
ceb30eb5f4

+ 9 - 0
pom.xml

@@ -61,6 +61,15 @@
             <version>3.12.0</version> <!-- 使用最新版本 -->
         </dependency>
 
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-data-redis</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.commons</groupId>
+            <artifactId>commons-pool2</artifactId>
+        </dependency>
+
 
         <dependency>
             <groupId>io.jsonwebtoken</groupId>

+ 47 - 0
src/main/java/cn/seecoder/fdroidrepository/config/RedisConfig.java

@@ -0,0 +1,47 @@
+package cn.seecoder.fdroidrepository.config;
+
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.serializer.RedisSerializer;
+
+@Configuration
+public class RedisConfig {
+
+    /**
+     * *redis序列化的工具定置类,下面这个请一定开启配置
+     * *127.0.0.1:6379> keys *
+     * *1) “ord:102” 序列化过
+     * *2)“\xaclxedlxeelx05tixeelaord:102” 野生,没有序列化过
+     * *this.redisTemplate.opsForValue(); //提供了操作string类型的所有方法
+     * *this.redisTemplate.opsForList();// 提供了操作List类型的所有方法
+     * *this.redisTemplate.opsForset(); //提供了操作set类型的所有方法
+     * *this.redisTemplate.opsForHash(); //提供了操作hash类型的所有方认
+     * *this.redisTemplate.opsForZSet(); //提供了操作zset类型的所有方法
+     * param LettuceConnectionFactory
+     * return
+     */
+
+    @Bean
+    public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
+        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
+        redisTemplate.setConnectionFactory(lettuceConnectionFactory);
+        // 设置key序列化方式string
+        redisTemplate.setKeySerializer(RedisSerializer.string()); // RedisSerializer.string() 等价于 new StringRedisSerializer()
+
+        // 设置value的序列化方式json,使用GenericJackson2JsonRedisSerializer替换默认序列化
+        redisTemplate.setValueSerializer(RedisSerializer.json()); // RedisSerializer.json() 等价于 new GenericJackson2JsonRedisSerializer()
+
+        // 设置hash的key的序列化方式
+        redisTemplate.setHashKeySerializer(RedisSerializer.string());
+
+        // 设置hash的value的序列化方式
+        redisTemplate.setHashValueSerializer(RedisSerializer.json());
+
+        // 使配置生效
+        redisTemplate.afterPropertiesSet();
+        return redisTemplate;
+    }
+}

+ 13 - 0
src/main/resources/application.properties

@@ -14,6 +14,19 @@ community.path.domain=http://127.0.0.1:8080
 
 
 
+# Redis配置
+spring.data.redis.password=123456
+
+spring.data.redis.cluster.max-redirects=3
+spring.data.redis.lettuce.pool.max-active=8
+spring.data.redis.lettuce.pool.max-wait=-1ms
+spring.data.redis.lettuce.pool.max-idle=8
+spring.data.redis.lettuce.pool.min-idle=0
+
+spring.data.redis.lettuce.cluster.refresh.adaptive=true
+
+spring.data.redis.lettuce.cluster.refresh.period=2000
+spring.data.redis.cluster.nodes=192.168.88.130:6379,192.168.88.130:6382,192.168.88.131:6380,192.168.88.131:6383,192.168.88.132:6381,192.168.88.132:6384
 
 
 

+ 68 - 0
src/test/java/cn/seecoder/fdroidrepository/RedisTests.java

@@ -0,0 +1,68 @@
+package cn.seecoder.fdroidrepository;
+
+
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.test.context.ContextConfiguration;
+
+@SpringBootTest
+@ContextConfiguration(classes =  FdroidRepositoryApplication.class)
+public class RedisTests {
+
+    @Autowired
+    private RedisTemplate redisTemplate;
+
+    @Test
+    void testStrings() {
+        String redisKey = "test:cout";
+        redisTemplate.opsForValue().set(redisKey,1);
+        int count = (int) redisTemplate.opsForValue().get(redisKey);
+        Assertions.assertEquals(count+1,redisTemplate.opsForValue().increment(redisKey));
+        Assertions.assertEquals(count,redisTemplate.opsForValue().decrement(redisKey));
+    }
+
+    @Test
+    void testList() {
+        String redisKey = "test:ids";
+        redisTemplate.opsForList().leftPush(redisKey,101);
+        redisTemplate.opsForList().leftPush(redisKey,102);
+        redisTemplate.opsForList().leftPush(redisKey,103);
+
+        System.out.println(redisTemplate.opsForList().size(redisKey));
+        System.out.println(redisTemplate.opsForList().index(redisKey,0));
+        System.out.println(redisTemplate.opsForList().range(redisKey, 0, -1));
+
+        System.out.println(redisTemplate.opsForList().leftPop(redisKey));
+        System.out.println(redisTemplate.opsForList().leftPop(redisKey));
+        System.out.println(redisTemplate.opsForList().leftPop(redisKey));
+
+    }
+
+    @Test
+    void testSets(){
+        String redisKey = "test:teachers";
+        redisTemplate.opsForSet().add(redisKey,"chinese","math");
+        System.out.println(redisTemplate.opsForSet().size(redisKey));
+        System.out.println(redisTemplate.opsForSet().pop(redisKey));
+        System.out.println(redisTemplate.opsForSet().members(redisKey));
+    }
+
+    @Test
+    void testZSet() {
+        String redisKey = "test:students";
+        redisTemplate.opsForZSet().add(redisKey,"studentA",80);
+        redisTemplate.opsForZSet().add(redisKey,"studentB",90);
+        redisTemplate.opsForZSet().add(redisKey,"studentC",70);
+        System.out.println(redisTemplate.opsForZSet().zCard(redisKey));
+        System.out.println(redisTemplate.opsForZSet().score(redisKey,"studentA"));
+        System.out.println(redisTemplate.opsForZSet().reverseRank(redisKey,"studentA"));
+        System.out.println(redisTemplate.opsForZSet().range(redisKey,0,-1));
+    }
+
+
+}