一:安装Redis
因本人电脑是windows系统,从https://github.com/ServiceStack/redis-windows下载了兼容windows系统的redis
下载后直接解压到自定义目录,运行cmd命令,进入到这个文件夹,在这个文件夹下运行下面命令,启动redis服务器
redis-server redis.windows.conf
运行下面命令进行测试: redis-cli
二:添加Redis依赖
1
2
3
4
5
6 1<dependency>
2 <groupId>org.springframework.boot</groupId>
3 <artifactId>spring-boot-starter-redis</artifactId>
4 <version>1.4.5.RELEASE</version>
5</dependency>
6
然后鼠标右键选择Maven→Reimport进行依赖下载
三:添加Redis配置信息
在application.properties中添加
1
2
3
4
5 1spring.redis.host=127.0.0.1
2spring.redis.port=6379
3spring.redis.timeout=0
4spring.redis.password=
5
四:创建RedisConfigurer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 1package com.example.demo.core.configurer;
2
3import org.springframework.boot.context.properties.ConfigurationProperties;
4import org.springframework.cache.annotation.CachingConfigurerSupport;
5import org.springframework.cache.annotation.EnableCaching;
6import org.springframework.context.annotation.Bean;
7import org.springframework.context.annotation.Configuration;
8import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
9import org.springframework.data.redis.core.RedisTemplate;
10import org.springframework.data.redis.core.StringRedisTemplate;
11import redis.clients.jedis.JedisPoolConfig;
12
13/**
14 * @author phubing
15 * @Description: redis配置
16 * @date 2019/4/30 10:28
17 */
18@Configuration
19@EnableCaching //开启缓存
20public class RedisConfigurer extends CachingConfigurerSupport {
21
22 @Bean
23 @ConfigurationProperties(prefix="spring.redis")
24 public JedisPoolConfig getRedisConfig(){
25 JedisPoolConfig config = new JedisPoolConfig();
26 return config;
27 }
28
29 @Bean
30 @ConfigurationProperties(prefix="spring.redis")
31 public JedisConnectionFactory getConnectionFactory(){
32 JedisConnectionFactory factory = new JedisConnectionFactory();
33 JedisPoolConfig config = getRedisConfig();
34 factory.setPoolConfig(config);
35 return factory;
36 }
37
38
39 @Bean
40 public RedisTemplate<?, ?> getRedisTemplate(){
41 RedisTemplate<?,?> template = new StringRedisTemplate(getConnectionFactory());
42 return template;
43 }
44}
45
五:创建Redis常用方法
RedisService
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 1package com.example.demo.service;
2
3import java.util.List;
4
5/**
6 * @author phubing
7 * @Description: redis常用方法
8 * @date 2019/4/30 10:35
9 */
10public interface RedisService {
11
12 /**
13 * 设置给定 key 的值。如果 key 已经存储其他值, SET 就覆写旧值,且无视类型。
14 * @param key
15 * @param value
16 * @return
17 */
18 boolean set(String key, String value);
19
20 /**
21 * 获取指定 key 的值。如果 key 不存在,返回 nil 。如果key 储存的值不是字符串类型,返回一个错误。
22 * @param key
23 * @return
24 */
25 String get(String key);
26
27 /**
28 * 设置 key 的过期时间。key 过期后将不再可用。
29 * @param key
30 * @param expire
31 * @return
32 */
33 boolean expire(String key, long expire);
34
35 /**
36 * 存集合
37 * @param key
38 * @param list
39 * @param <T>
40 * @return
41 */
42 <T> boolean setList(String key, List<T> list);
43
44 /**
45 * 取集合
46 * @param key
47 * @param clz
48 * @param <T>
49 * @return
50 */
51 <T> List<T> getList(String key, Class<T> clz);
52
53 /**
54 * 将一个或多个值插入到列表头部。 如果 key 不存在,一个空列表会被创建并执行 LPUSH 操作。
55 * 当 key 存在但不是列表类型时,返回一个错误。
56 * @param key
57 * @param obj
58 * @return
59 */
60 long lpush(String key, Object obj);
61
62 /**
63 * 将一个或多个值插入到列表的尾部(最右边)。
64 * 如果列表不存在,一个空列表会被创建并执行 RPUSH 操作。 当列表存在但不是列表类型时,返回一个错误。
65 * @param key
66 * @param obj
67 * @return
68 */
69 long rpush(String key, Object obj);
70
71 /**
72 * 移除并返回列表的第一个元素。
73 * @param key
74 * @return
75 */
76 String lpop(String key);
77
78 /**
79 * 删除已存在的键。不存在的 key 会被忽略。
80 * @param key
81 * @return
82 */
83 long del(final String key);
84}
85
86
RedisServiceImpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130 1package com.example.demo.service.impl;
2
3import com.alibaba.fastjson.JSON;
4import com.example.demo.service.RedisService;
5import org.springframework.dao.DataAccessException;
6import org.springframework.data.redis.connection.RedisConnection;
7import org.springframework.data.redis.core.RedisCallback;
8import org.springframework.data.redis.core.RedisTemplate;
9import org.springframework.data.redis.serializer.RedisSerializer;
10import org.springframework.stereotype.Service;
11
12import javax.annotation.Resource;
13import java.util.List;
14import java.util.concurrent.TimeUnit;
15
16/**
17 * @author phubing
18 * @Description: redis配置
19 * @date 2019/4/30 10:38
20 */
21@Service
22public class RedisServiceImpl implements RedisService {
23
24 @Resource
25 private RedisTemplate<String, ?> redisTemplate;
26
27 @Override
28 public boolean set(final String key, final String value) {
29 boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
30 @Override
31 public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
32 RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
33 connection.set(serializer.serialize(key), serializer.serialize(value));
34 return true;
35 }
36 });
37 return result;
38 }
39
40 @Override
41 public String get(final String key){
42 String result = redisTemplate.execute(new RedisCallback<String>() {
43 @Override
44 public String doInRedis(RedisConnection connection) throws DataAccessException {
45 RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
46 byte[] value = connection.get(serializer.serialize(key));
47 return serializer.deserialize(value);
48 }
49 });
50 return result;
51 }
52
53 @Override
54 public long del(final String key){
55 long result = redisTemplate.execute(new RedisCallback<Long>() {
56 @Override
57 public Long doInRedis(RedisConnection connection) throws DataAccessException {
58 RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
59 long value = connection.del(serializer.serialize(key));
60 return value;
61 }
62 });
63 return result;
64 }
65
66 @Override
67 public boolean expire(final String key, long expire) {
68 return redisTemplate.expire(key, expire, TimeUnit.SECONDS);
69 }
70
71 @Override
72 public <T> boolean setList(String key, List<T> list) {
73 String value = JSON.toJSONString(list);
74 return set(key,value);
75 }
76
77 @Override
78 public <T> List<T> getList(String key,Class<T> clz) {
79 String json = get(key);
80 if(json!=null){
81 List<T> list = JSON.parseArray(json, clz);
82 return list;
83 }
84 return null;
85 }
86
87 @Override
88 public long lpush(final String key, Object obj) {
89 final String value = JSON.toJSONString(obj);
90 long result = redisTemplate.execute(new RedisCallback<Long>() {
91 @Override
92 public Long doInRedis(RedisConnection connection) throws DataAccessException {
93 RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
94 long count = connection.lPush(serializer.serialize(key), serializer.serialize(value));
95 return count;
96 }
97 });
98 return result;
99 }
100
101 @Override
102 public long rpush(final String key, Object obj) {
103 final String value = JSON.toJSONString(obj);
104 long result = redisTemplate.execute(new RedisCallback<Long>() {
105 @Override
106 public Long doInRedis(RedisConnection connection) throws DataAccessException {
107 RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
108 long count = connection.rPush(serializer.serialize(key), serializer.serialize(value));
109 return count;
110 }
111 });
112 return result;
113 }
114
115 @Override
116 public String lpop(final String key) {
117 String result = redisTemplate.execute(new RedisCallback<String>() {
118 @Override
119 public String doInRedis(RedisConnection connection) throws DataAccessException {
120 RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
121 byte[] res = connection.lPop(serializer.serialize(key));
122 return serializer.deserialize(res);
123 }
124 });
125 return result;
126 }
127
128
129}
130
六:接口测试
创建RedisController
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 1package com.example.demo.controller;
2
3import com.example.demo.core.ret.RetResponse;
4import com.example.demo.core.ret.RetResult;
5import com.example.demo.service.RedisService;
6import org.springframework.web.bind.annotation.PostMapping;
7import org.springframework.web.bind.annotation.RequestMapping;
8import org.springframework.web.bind.annotation.RestController;
9
10import javax.annotation.Resource;
11
12/**
13 * @author phubing
14 * @Description:
15 * @date 2019/4/30 11:28
16 */
17@RestController
18@RequestMapping("redis")
19public class RedisController {
20
21 @Resource
22 private RedisService redisService;
23
24 @PostMapping("/setRedis")
25 public RetResult<String> setRedis(String name) {
26 redisService.set("name",name);
27 return RetResponse.makeOKRsp(name);
28 }
29
30 @PostMapping("/getRedis")
31 public RetResult<String> getRedis() {
32 String name = redisService.get("name");
33 return RetResponse.makeOKRsp(name);
34 }
35
36
37}
38
输入http://localhost:8080/redis/setRedis
输入http://localhost:8080/redis/getRedis