Skip to content

Commit e95d8aa

Browse files
committed
3.学习 springboot集成redis
1 parent 8cdf3b7 commit e95d8aa

5 files changed

Lines changed: 314 additions & 0 deletions

File tree

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ dependencies {
8383
compile 'org.springframework.boot:spring-boot-starter-aop:1.5.4.RELEASE'
8484

8585
runtime 'mysql:mysql-connector-java:5.1.39'
86+
compile 'org.springframework.boot:spring-boot-starter-redis:1.4.7.RELEASE'
87+
8688

8789
testCompile 'org.springframework.boot:spring-boot-starter-test:1.5.4.RELEASE'
8890
compile 'org.springframework.boot:spring-boot-starter-freemarker:1.5.4.RELEASE'
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package acheng1314.cn;
2+
3+
import com.fasterxml.jackson.annotation.JsonAutoDetect;
4+
import com.fasterxml.jackson.annotation.PropertyAccessor;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import org.springframework.cache.CacheManager;
7+
import org.springframework.cache.annotation.CachingConfigurerSupport;
8+
import org.springframework.cache.annotation.EnableCaching;
9+
import org.springframework.cache.interceptor.KeyGenerator;
10+
import org.springframework.context.annotation.Bean;
11+
import org.springframework.context.annotation.Configuration;
12+
import org.springframework.data.redis.cache.RedisCacheManager;
13+
import org.springframework.data.redis.connection.RedisConnectionFactory;
14+
import org.springframework.data.redis.core.RedisTemplate;
15+
import org.springframework.data.redis.core.StringRedisTemplate;
16+
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
17+
18+
import java.lang.reflect.Method;
19+
20+
21+
/*
22+
添加cache配置类
23+
配置参考:https://www.cnblogs.com/haitao-xie/p/6323423.html
24+
*/
25+
@Configuration
26+
@EnableCaching
27+
public class RedisConfig extends CachingConfigurerSupport {
28+
29+
/**
30+
* 生成key的策略
31+
* @return
32+
*/
33+
@Bean
34+
public KeyGenerator keyGenerator() {
35+
return new KeyGenerator() {
36+
@Override
37+
public Object generate(Object target, Method method, Object... params) {
38+
StringBuilder sb = new StringBuilder();
39+
sb.append(target.getClass().getName());
40+
sb.append(method.getName());
41+
for (Object obj : params) {
42+
sb.append(obj.toString());
43+
}
44+
return sb.toString();
45+
}
46+
47+
48+
};
49+
}
50+
51+
/**
52+
* 管理缓存
53+
*/
54+
@Bean
55+
public CacheManager cacheManager(RedisTemplate redisTemplate) {
56+
RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
57+
return rcm;
58+
}
59+
60+
/**
61+
* RedisTemplate配置
62+
*/
63+
@Bean
64+
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
65+
StringRedisTemplate template = new StringRedisTemplate(factory);
66+
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
67+
ObjectMapper om = new ObjectMapper();
68+
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
69+
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
70+
jackson2JsonRedisSerializer.setObjectMapper(om);
71+
template.setValueSerializer(jackson2JsonRedisSerializer);
72+
template.afterPropertiesSet();
73+
return template;
74+
}
75+
76+
77+
78+
}
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
package acheng1314.cn.service;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.data.redis.core.*;
5+
import org.springframework.stereotype.Service;
6+
7+
import java.io.Serializable;
8+
import java.util.List;
9+
import java.util.Set;
10+
import java.util.concurrent.TimeUnit;
11+
12+
/**
13+
* 编写redis的service类
14+
*/
15+
@Service
16+
public class RedisService {
17+
@Autowired
18+
private RedisTemplate redisTemplate;
19+
/**
20+
* 写入缓存
21+
* @param key
22+
* @param value
23+
* @return
24+
*/
25+
public boolean set(final String key, Object value) {
26+
boolean result = false;
27+
try {
28+
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
29+
operations.set(key, value);
30+
result = true;
31+
} catch (Exception e) {
32+
e.printStackTrace();
33+
}
34+
return result;
35+
}
36+
/**
37+
* 写入缓存设置时效时间
38+
* @param key
39+
* @param value
40+
* @return
41+
*/
42+
public boolean set(final String key, Object value, Long expireTime) {
43+
boolean result = false;
44+
try {
45+
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
46+
operations.set(key, value);
47+
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
48+
result = true;
49+
} catch (Exception e) {
50+
e.printStackTrace();
51+
}
52+
return result;
53+
}
54+
/**
55+
* 批量删除对应的value
56+
* @param keys
57+
*/
58+
public void remove(final String... keys) {
59+
for (String key : keys) {
60+
remove(key);
61+
}
62+
}
63+
64+
/**
65+
* 批量删除key
66+
* @param pattern
67+
*/
68+
public void removePattern(final String pattern) {
69+
Set<Serializable> keys = redisTemplate.keys(pattern);
70+
if (keys.size() > 0)
71+
redisTemplate.delete(keys);
72+
}
73+
/**
74+
* 删除对应的value
75+
* @param key
76+
*/
77+
public void remove(final String key) {
78+
if (exists(key)) {
79+
redisTemplate.delete(key);
80+
}
81+
}
82+
/**
83+
* 判断缓存中是否有对应的value
84+
* @param key
85+
* @return
86+
*/
87+
public boolean exists(final String key) {
88+
return redisTemplate.hasKey(key);
89+
}
90+
/**
91+
* 读取缓存
92+
* @param key
93+
* @return
94+
*/
95+
public Object get(final String key) {
96+
Object result = null;
97+
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
98+
result = operations.get(key);
99+
return result;
100+
}
101+
/**
102+
* 哈希 添加
103+
* @param key
104+
* @param hashKey
105+
* @param value
106+
*/
107+
public void hmSet(String key, Object hashKey, Object value){
108+
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
109+
hash.put(key,hashKey,value);
110+
}
111+
112+
/**
113+
* 哈希获取数据
114+
* @param key
115+
* @param hashKey
116+
* @return
117+
*/
118+
public Object hmGet(String key, Object hashKey){
119+
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
120+
return hash.get(key,hashKey);
121+
}
122+
123+
/**
124+
* 列表添加
125+
* @param k
126+
* @param v
127+
*/
128+
public void lPush(String k,Object v){
129+
ListOperations<String, Object> list = redisTemplate.opsForList();
130+
list.rightPush(k,v);
131+
}
132+
133+
/**
134+
* 列表获取
135+
* @param k
136+
* @param l
137+
* @param l1
138+
* @return
139+
*/
140+
public List<Object> lRange(String k, long l, long l1){
141+
ListOperations<String, Object> list = redisTemplate.opsForList();
142+
return list.range(k,l,l1);
143+
}
144+
145+
/**
146+
* 集合添加
147+
* @param key
148+
* @param value
149+
*/
150+
public void add(String key,Object value){
151+
SetOperations<String, Object> set = redisTemplate.opsForSet();
152+
set.add(key,value);
153+
}
154+
155+
/**
156+
* 集合获取
157+
* @param key
158+
* @return
159+
*/
160+
public Set<Object> setMembers(String key){
161+
SetOperations<String, Object> set = redisTemplate.opsForSet();
162+
return set.members(key);
163+
}
164+
165+
/**
166+
* 有序集合添加
167+
* @param key
168+
* @param value
169+
* @param scoure
170+
*/
171+
public void zAdd(String key,Object value,double scoure){
172+
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
173+
zset.add(key,value,scoure);
174+
}
175+
176+
/**
177+
* 有序集合获取
178+
* @param key
179+
* @param scoure
180+
* @param scoure1
181+
* @return
182+
*/
183+
public Set<Object> rangeByScore(String key,double scoure,double scoure1){
184+
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
185+
return zset.rangeByScore(key, scoure, scoure1);
186+
}
187+
}

src/main/java/acheng1314/cn/util/GsonUtils.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
* Created by linjizong on 15/7/20.
2121
*/
2222
@SuppressWarnings("unchecked")
23+
24+
2325
public class GsonUtils {
2426

2527
private static Gson gson;
@@ -183,4 +185,27 @@ public static String toJsonObjStr(Object object, ResponseCode code, String msg)
183185
}
184186
return toJson(result);
185187
}
188+
/**
189+
* 学习教程:
190+
*
191+
简介:java.lang.SuppressWarnings是J2SE5.0中标准的Annotation之一。可以标注在类、字段、方法、参数、构造方法,以及局部变量上。
192+
作用:告诉编译器忽略指定的警告,不用在编译完成后出现警告信息。
193+
使用:
194+
1. @SuppressWarnings(“”)
195+
2. @SuppressWarnings({})
196+
3. @SuppressWarnings(value={})
197+
* 示例:
198+
· @SuppressWarnings("unchecked")
199+
告诉编译器忽略 unchecked 警告信息,如使用List,ArrayList等未进行参数化产生的警告信息。
200+
· @SuppressWarnings("serial")
201+
如果编译器出现这样的警告信息:The serializable class WmailCalendar does notdeclare a static final serialVersionUID field of type long
202+
使用这个注释将警告信息去掉。
203+
· @SuppressWarnings("deprecation")
204+
如果使用了使用@Deprecated注释的方法,编译器将出现警告信息。
205+
使用这个注释将警告信息去掉。
206+
· @SuppressWarnings("unchecked", "deprecation")
207+
告诉编译器同时忽略unchecked和deprecation的警告信息。
208+
· @SuppressWarnings(value={"unchecked", "deprecation"})
209+
等同于@SuppressWarnings("unchecked", "deprecation")
210+
*/
186211
}

src/main/resources/application.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,28 @@ mybatis-plus:
138138
server:
139139
port: 8181
140140

141+
# Redis配置
142+
redis:
143+
# Redis服务器地址
144+
host: 192.168.1.101
145+
# Redis服务器连接端口
146+
port: 6379
147+
# Redis服务器连接密码(默认为空)
148+
password:
149+
# 连接超时时间(毫秒)
150+
timeout: 10000
151+
pool:
152+
# 连接池最大连接数(使用负值表示没有限制)
153+
# 连接池中的最大空闲连接
154+
max-idle: 20
155+
# 连接池中的最小空闲连接
156+
min-idle: 5
157+
# 连接池最大连接数(使用负值表示没有限制)
158+
max-active: 20
159+
# 连接池最大阻塞等待时间(使用负值表示没有限制)
160+
max-wait: 2
161+
162+
141163
#server:
142164
# servlet-path: "/"
143165
# context-path: "/"

0 commit comments

Comments
 (0)