Browse Source

0126 redis

Qing 1 year ago
parent
commit
c8c62a3fbf

+ 5 - 0
springboot-demo/pom.xml

@@ -98,6 +98,11 @@
             <version>2.3.32</version>
         </dependency>
 
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-data-redis</artifactId>
+        </dependency>
+
     </dependencies>
 
     <build>

+ 1 - 1
springboot-demo/src/main/java/com/sf/DemoApplication.java

@@ -38,7 +38,7 @@ public class DemoApplication implements CommandLineRunner {
         String[] names = context.getBeanDefinitionNames();
 //        System.out.println(Arrays.toString(names));
         // 先获取数组对应的stream(数据流)  然后使用foreach进行遍历 遍历过程中进行打印
-        Arrays.stream(names).forEach(System.out::println);
+//        Arrays.stream(names).forEach(System.out::println);
     }
 
     // @SpringBootConfiguration -> @Configuration

+ 4 - 4
springboot-demo/src/main/java/com/sf/config/MyAutoConfig.java

@@ -36,10 +36,10 @@ public class MyAutoConfig {
             //  com.sf.config.MyAutoConfig.OtherAutoConfig1,com.sf.config.MyAutoConfig.OtherAutoConfig2
 //            List<String> factoryNames = SpringFactoriesLoader.loadFactoryNames(MyImportSelector.class, null);
             List<String> factoryNames = ImportCandidates.load(MyImportSelector.class, null).getCandidates();
-            List<String> configurations = ImportCandidates.load(AutoConfiguration.class, null).getCandidates();
-            System.out.println(">>>>>>>>>>>>>>>>>");
-            configurations.forEach(System.out::println);
-            System.out.println(">>>>>>>>>>>>>>>>>");
+//            List<String> configurations = ImportCandidates.load(AutoConfiguration.class, null).getCandidates();
+//            System.out.println(">>>>>>>>>>>>>>>>>");
+//            configurations.forEach(System.out::println);
+//            System.out.println(">>>>>>>>>>>>>>>>>");
 //            List<String> factoryNamesAuto = SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class, null);
 //            return factoryNames.toArray(new String[0]);
             return StringUtils.toStringArray(factoryNames);

+ 28 - 0
springboot-demo/src/main/java/com/sf/config/RedisConfig.java

@@ -0,0 +1,28 @@
+package com.sf.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.redis.connection.RedisConnectionFactory;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
+import org.springframework.data.redis.serializer.StringRedisSerializer;
+
+@Configuration
+public class RedisConfig {
+
+    @Bean("redisTemplate")
+    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
+        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
+        redisTemplate.setConnectionFactory(factory);
+
+        // 序列化方式 自定义
+        StringRedisSerializer keySerializer = new StringRedisSerializer();
+        GenericJackson2JsonRedisSerializer valueSerializer = new GenericJackson2JsonRedisSerializer();
+
+        redisTemplate.setKeySerializer(keySerializer);
+        redisTemplate.setHashKeySerializer(keySerializer);
+        redisTemplate.setValueSerializer(valueSerializer);
+        redisTemplate.setHashValueSerializer(valueSerializer);
+        return redisTemplate;
+    }
+}

+ 23 - 0
springboot-demo/src/main/java/com/sf/controller/RedisController.java

@@ -0,0 +1,23 @@
+package com.sf.controller;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@RequestMapping("/redis")
+public class RedisController {
+
+    @Autowired
+    private RedisTemplate redisTemplate;
+
+    // http://localhost:18080/redis/save?key=key11&value=value11
+    @GetMapping("/save")
+    public String save(String key, String value) {
+        // 存储到redis中
+        redisTemplate.opsForValue().set(key, value);
+        return "success";
+    }
+}

+ 4 - 0
springboot-demo/src/main/resources/application.properties

@@ -13,6 +13,10 @@ spring.datasource.username=root
 spring.datasource.password=root123456
 #spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
 
+#spring.data.redis.host=localhost
+#spring.data.redis.port=6379
+#spring.data.redis.host=localhost
+
 mybatis.config-location=classpath:mybatis-config.xml
 mybatis.mapper-locations=classpath*:mapper/**/*.xml
 

+ 5 - 21
springboot-demo/src/main/resources/application.yml

@@ -1,24 +1,8 @@
-food:
-    meat: 烤肉
-    rice: 拌饭
-
-mybatis:
-    config-location: classpath:mybatis-config.xml
-    mapper-locations: classpath*:mapper/**/*.xml
-
-spring:
-    datasource:
-        password: root123456
-        url: jdbc:mysql://localhost:3306/novels?useUnicode=true&characterEncoding=utf-8&allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=Asia/Shanghai
-        username: root
----
 spring:
     banner:
         location: classpath:banner/FromWangHongMing.txt
-
-#lines: |
-#    我是第一行
-#    我是第二行
-#      我是第三行
-#        我是第四行
-#    我是第五行
+    data:
+        redis:
+            host: 127.0.0.1
+            port: 6379
+            password:

+ 55 - 0
springboot-demo/src/test/java/com/sf/RedisTests.java

@@ -0,0 +1,55 @@
+package com.sf;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.data.redis.core.ListOperations;
+import org.springframework.data.redis.core.RedisTemplate;
+
+@SpringBootTest
+public class RedisTests {
+
+    @Autowired
+    private RedisTemplate redisTemplate;
+
+    @Test
+    public void testString() {
+
+//        // 来自于 spring-redis-data.jar
+//        redisTemplate.opsForValue().set("key12", abc);
+//        System.out.println(redisTemplate.opsForValue().get("key12"));
+
+        redisTemplate.opsForValue().set("key11", 10L);
+        redisTemplate.opsForValue().increment("key11");
+        System.out.println(redisTemplate.opsForValue().get("key11"));
+
+        redisTemplate.opsForValue().append("key14", "10");
+        redisTemplate.opsForValue().append("key14", "20");
+        System.out.println(redisTemplate.opsForValue().get("key14"));
+
+//         mylist 1 2 3 4 5  rpush
+//         mylist 10 20 30 40 50 1 2 3 4 5  lpush
+//         mylist 长度 llen
+//         mylist 弹出最后一个  rpop  10 20 30 40 50 1 2 3 4
+//         mylist 只取出前五个  ltrim  10 20 30 40 50
+//        redisTemplate.opsForList().
+    }
+
+    @Test
+    public void testList() {
+        ListOperations forList = redisTemplate.opsForList();
+        String key = "mylist11";
+        forList.rightPushAll(key, "1", "2", "3", "4", "5");  //rightPush
+//        forList.leftPushAll(key, "10", "20", "30", "40", "50");
+        // 50 40 30 20 10 1 2 3 4 5
+        forList.leftPushAll(key, "50", "40", "30", "20", "10");
+        // llen
+        System.out.println(forList.size(key));
+        // lrange
+        System.out.println(forList.range(key, 0, -1));
+
+        forList.rightPop(key);
+        forList.trim(key, 0, 4);
+        System.out.println(forList.range(key, 0, -1));
+    }
+}