SrpingBoot整合Redis

1、Maven依赖

 1 <parent>
 2         <groupId>org.springframework.boot</groupId>
 3         <artifactId>spring-boot-starter-parent</artifactId>
 4         <version>2.0.0.RELEASE</version>
 5     </parent>
 6     <dependencies>
 7         <!-- SpringBoot web 核心组件 -->
 8         <dependency>
 9             <groupId>org.springframework.boot</groupId>
10             <artifactId>spring-boot-starter-web</artifactId>
11         </dependency>
12         <dependency>
13             <groupId>org.springframework.boot</groupId>
14             <artifactId>spring-boot-starter-data-redis</artifactId>
15         </dependency>
16     </dependencies>
17 
18 
19     <build>
20         <plugins>
21             <plugin>
22                 <groupId>org.apache.maven.plugins</groupId>
23                 <artifactId>maven-compiler-plugin</artifactId>
24                 <configuration>
25                     <source>1.8</source>
26                     <target>1.8</target>
27                 </configuration>
28             </plugin>
29             <plugin>
30                 <groupId>org.springframework.boot</groupId>
31                 <artifactId>spring-boot-maven-plugin</artifactId>
32                 <configuration>
33                     <maimClass>
34                         com.itmayiedu.controller.IndexController</maimClass>
35                 </configuration>
36                 <executions>
37                     <execution>
38                         <goals>
39                             <goal>repackage</goal>
40                         </goals>
41                     </execution>
42                 </executions>
43 
44             </plugin>
45         </plugins>
46     </build>

2、配置文件

 1 spring:
 2   redis:
 3     database: 0
 4     host: 132.232.44.194
 5     port: 6379
 6     password: 123456
 7     jedis:
 8       pool:
 9         max-active: 8
10         max-wait: -1
11         max-idle: 8
12         min-idle: 0
13     timeout: 10000

3、封装

 1 @Component
 2 public class RedisService {
 3 
 4     @Autowired
 5     private StringRedisTemplate stringRedisTemplate;
 6 
 7     public void set(String key, Object object, Long time) {
 8         // 存放String 类型
 9         if (object instanceof String) {
10             setString(key, object);
11         }
12         // 存放 set类型
13         if (object instanceof Set) {
14             setSet(key, object);
15         }
16         // 设置有效期 以秒为单位
17         stringRedisTemplate.expire(key, time, TimeUnit.SECONDS);
18     }
19 
20     public void setString(String key, Object object) {
21         // 如果是String 类型
22         String value = (String) object;
23         stringRedisTemplate.opsForValue().set(key, value);
24     }
25 
26     public void setSet(String key, Object object) {
27         Set<String> value = (Set<String>) object;
28         for (String oj : value) {
29             stringRedisTemplate.opsForSet().add(key, oj);
30         }
31     }
32 
33     public String getString(String key) {
34         return stringRedisTemplate.opsForValue().get(key);
35     }
36 
37 }

4、整合API调用

 1 @RestController
 2 public class IndexControler {
 3 
 4     @Autowired
 5     private RedisService redisService;
 6 
 7     @RequestMapping("/setString")
 8     public String setString(String key, String value) {
 9         redisService.set(key, value, 60l);
10         return "success";
11     }
12 
13     @RequestMapping("/getString")
14     public String getString(String key) {
15         return redisService.getString(key);
16     }
17 
18     @RequestMapping("/setSet")
19     public String setSet() {
20         Set<String> set = new HashSet<String>();
21         set.add("yushengjun");
22         set.add("lisi");
23         redisService.setSet("setTest", set);
24         return "success";
25     }
26 }

 

posted on 2019-08-13 22:30  JAVA-ROAD  阅读(220)  评论(0编辑  收藏  举报

导航