Skip to content

Commit 05a4914

Browse files
author
YiFengJava
committed
加入redis工具类
1 parent 1a3222d commit 05a4914

File tree

5 files changed

+1509
-12
lines changed

5 files changed

+1509
-12
lines changed

manage-admin/manage-admin.iml

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
<orderEntry type="sourceFolder" forTests="false" />
2828
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-data-jpa:2.0.5.RELEASE" level="project" />
2929
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:2.0.5.RELEASE" level="project" />
30-
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:2.0.5.RELEASE" level="project" />
31-
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-autoconfigure:2.0.5.RELEASE" level="project" />
3230
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-logging:2.0.5.RELEASE" level="project" />
3331
<orderEntry type="library" name="Maven: ch.qos.logback:logback-classic:1.2.3" level="project" />
3432
<orderEntry type="library" name="Maven: ch.qos.logback:logback-core:1.2.3" level="project" />
@@ -56,16 +54,13 @@
5654
<orderEntry type="library" name="Maven: org.springframework.data:spring-data-jpa:2.0.10.RELEASE" level="project" />
5755
<orderEntry type="library" name="Maven: org.springframework.data:spring-data-commons:2.0.10.RELEASE" level="project" />
5856
<orderEntry type="library" name="Maven: org.springframework:spring-orm:5.0.9.RELEASE" level="project" />
59-
<orderEntry type="library" name="Maven: org.springframework:spring-context:5.0.9.RELEASE" level="project" />
6057
<orderEntry type="library" name="Maven: org.springframework:spring-tx:5.0.9.RELEASE" level="project" />
6158
<orderEntry type="library" name="Maven: org.springframework:spring-beans:5.0.9.RELEASE" level="project" />
62-
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.25" level="project" />
6359
<orderEntry type="library" name="Maven: org.springframework:spring-aspects:5.0.9.RELEASE" level="project" />
6460
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-data-redis:2.0.5.RELEASE" level="project" />
6561
<orderEntry type="library" name="Maven: org.springframework.data:spring-data-redis:2.0.10.RELEASE" level="project" />
6662
<orderEntry type="library" name="Maven: org.springframework.data:spring-data-keyvalue:2.0.10.RELEASE" level="project" />
6763
<orderEntry type="library" name="Maven: org.springframework:spring-oxm:5.0.9.RELEASE" level="project" />
68-
<orderEntry type="library" name="Maven: org.springframework:spring-context-support:5.0.9.RELEASE" level="project" />
6964
<orderEntry type="library" name="Maven: io.lettuce:lettuce-core:5.0.5.RELEASE" level="project" />
7065
<orderEntry type="library" name="Maven: io.projectreactor:reactor-core:3.1.9.RELEASE" level="project" />
7166
<orderEntry type="library" name="Maven: org.reactivestreams:reactive-streams:1.0.2" level="project" />
@@ -75,6 +70,14 @@
7570
<orderEntry type="library" name="Maven: io.netty:netty-resolver:4.1.29.Final" level="project" />
7671
<orderEntry type="library" name="Maven: io.netty:netty-handler:4.1.29.Final" level="project" />
7772
<orderEntry type="library" name="Maven: io.netty:netty-codec:4.1.29.Final" level="project" />
73+
<orderEntry type="library" name="Maven: org.apache.commons:commons-pool2:2.5.0" level="project" />
74+
<orderEntry type="library" name="Maven: org.springframework.session:spring-session-data-redis:2.0.6.RELEASE" level="project" />
75+
<orderEntry type="library" name="Maven: org.springframework.session:spring-session-core:2.0.6.RELEASE" level="project" />
76+
<orderEntry type="library" name="Maven: org.springframework:spring-jcl:5.0.9.RELEASE" level="project" />
77+
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-cache:2.0.5.RELEASE" level="project" />
78+
<orderEntry type="library" name="Maven: org.springframework:spring-context:5.0.9.RELEASE" level="project" />
79+
<orderEntry type="library" name="Maven: org.springframework:spring-expression:5.0.9.RELEASE" level="project" />
80+
<orderEntry type="library" name="Maven: org.springframework:spring-context-support:5.0.9.RELEASE" level="project" />
7881
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-thymeleaf:2.0.5.RELEASE" level="project" />
7982
<orderEntry type="library" name="Maven: org.thymeleaf:thymeleaf-spring5:3.0.9.RELEASE" level="project" />
8083
<orderEntry type="library" name="Maven: org.thymeleaf:thymeleaf:3.0.9.RELEASE" level="project" />
@@ -96,7 +99,6 @@
9699
<orderEntry type="library" name="Maven: javax.validation:validation-api:2.0.1.Final" level="project" />
97100
<orderEntry type="library" name="Maven: org.springframework:spring-web:5.0.9.RELEASE" level="project" />
98101
<orderEntry type="library" name="Maven: org.springframework:spring-webmvc:5.0.9.RELEASE" level="project" />
99-
<orderEntry type="library" name="Maven: org.springframework:spring-expression:5.0.9.RELEASE" level="project" />
100102
<orderEntry type="library" scope="RUNTIME" name="Maven: mysql:mysql-connector-java:5.1.47" level="project" />
101103
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-starter-test:2.0.5.RELEASE" level="project" />
102104
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-test:2.0.5.RELEASE" level="project" />
@@ -116,7 +118,6 @@
116118
<orderEntry type="library" scope="TEST" name="Maven: org.skyscreamer:jsonassert:1.5.0" level="project" />
117119
<orderEntry type="library" scope="TEST" name="Maven: com.vaadin.external.google:android-json:0.0.20131108.vaadin1" level="project" />
118120
<orderEntry type="library" name="Maven: org.springframework:spring-core:5.0.9.RELEASE" level="project" />
119-
<orderEntry type="library" name="Maven: org.springframework:spring-jcl:5.0.9.RELEASE" level="project" />
120121
<orderEntry type="library" scope="TEST" name="Maven: org.springframework:spring-test:5.0.9.RELEASE" level="project" />
121122
<orderEntry type="library" scope="TEST" name="Maven: org.xmlunit:xmlunit-core:2.5.1" level="project" />
122123
<orderEntry type="library" name="Maven: org.springframework.cloud:spring-cloud-starter-oauth2:2.0.1.RELEASE" level="project" />
@@ -152,5 +153,10 @@
152153
<orderEntry type="library" name="Maven: org.springframework.social:spring-social-web:1.1.4.RELEASE" level="project" />
153154
<orderEntry type="library" name="Maven: javax.inject:javax.inject:1" level="project" />
154155
<orderEntry type="library" name="Maven: org.projectlombok:lombok:1.16.18" level="project" />
156+
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-devtools:2.0.5.RELEASE" level="project" />
157+
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:2.0.5.RELEASE" level="project" />
158+
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-autoconfigure:2.0.5.RELEASE" level="project" />
159+
<orderEntry type="library" name="Maven: org.thymeleaf.extras:thymeleaf-extras-springsecurity5:3.0.4.RELEASE" level="project" />
160+
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.25" level="project" />
155161
</component>
156162
</module>

manage-admin/src/main/java/xyz/yudong520/manageadmin/core/redis/CacheConfig.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import java.util.stream.Collectors;
2828

2929
@Configuration
30-
@EnableCaching
30+
@EnableCaching //开启缓存
3131
public class CacheConfig extends CachingConfigurerSupport {
3232
@Bean
3333
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
@@ -50,14 +50,16 @@ public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connec
5050
}
5151

5252

53-
54-
55-
53+
/**
54+
* 用redis作为cache管理
55+
* @param redisConnectionFactory
56+
* @return
57+
*/
5658
@Bean
5759
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
5860
return new RedisCacheManager(
5961
RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory),
60-
this.getRedisCacheConfigurationWithTtl(30*60), // 默认策略,未配置的 key 会使用这个
62+
this.getRedisCacheConfigurationWithTtl(60), // 默认策略,未配置的 key 会使用这个
6163
this.getRedisCacheConfigurationMap() // 指定 key 策略
6264
);
6365
}
@@ -87,6 +89,10 @@ private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer second
8789
return redisCacheConfiguration;
8890
}
8991

92+
/**
93+
* cache的生产策略
94+
* @return
95+
*/
9096
@Bean
9197
public KeyGenerator wiselyKeyGenerator() {
9298
return new KeyGenerator() {
Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
package xyz.yudong520.manageadmin.core.redis;
2+
3+
//import redis.clients.jedis.Jedis;
4+
//import redis.clients.jedis.JedisPool;
5+
//import redis.clients.jedis.JedisPoolConfig;
6+
//
7+
//import java.util.Map;
8+
//import java.util.Set;
9+
10+
/**
11+
* 简单作用描述
12+
*
13+
* @ProjectName: springcloudproject
14+
* @Package: com.yudong.shiro_test.redis
15+
* @Description:
16+
* @Author: yudong
17+
* @CreateDate: 2018/8/23 9:30
18+
* @UpdateUser: yudong
19+
* @UpdateDate: 2018/8/23 9:30
20+
* @UpdateRemark:
21+
* @Version: 1.0
22+
* Copyright: Copyright (c) 2018
23+
*/
24+
public class RedisDb {
25+
//
26+
// private static JedisPool jedisPool;
27+
// // session 在redis过期时间是30分钟30*60
28+
// private static int expireTime = 1800;
29+
// // 计数器的过期时间默认2天
30+
// private static int countExpireTime = 2*24*3600;
31+
// private static String password = "ladlee123";
32+
// private static String redisIp = "47.92.1.158";
33+
// private static int redisPort = 6379;
34+
// private static int maxActive = 200;
35+
// private static int maxIdle = 200;
36+
// private static long maxWait = 5000;
37+
//
38+
// static {
39+
// initPool();
40+
// }
41+
// // 初始化连接池
42+
// public static void initPool(){
43+
// JedisPoolConfig config = new JedisPoolConfig();
44+
// config.setMaxTotal(maxActive);
45+
// config.setMaxIdle(maxIdle);
46+
// config.setMaxWaitMillis(maxWait);
47+
// config.setTestOnBorrow(false);
48+
//
49+
// jedisPool = new JedisPool(config, redisIp, redisPort, 10000, password);
50+
// }
51+
// // 从连接池获取redis连接
52+
// public static Jedis getJedis(){
53+
// Jedis jedis = null;
54+
// try{
55+
// jedis = jedisPool.getResource();
56+
//// jedis.auth(password);
57+
// } catch(Exception e){
58+
// e.printStackTrace();
59+
//// ExceptionCapture.logError(e);
60+
// }
61+
//
62+
// return jedis;
63+
// }
64+
// // 回收redis连接
65+
// public static void recycleJedis(Jedis jedis){
66+
// if(jedis != null){
67+
// try{
68+
// jedis.close();
69+
// } catch(Exception e){
70+
// e.printStackTrace();
71+
//// ExceptionCapture.logError(e);
72+
// }
73+
// }
74+
// }
75+
// // 保存字符串数据
76+
// public static void setString(String key, String value){
77+
// Jedis jedis = getJedis();
78+
// if(jedis != null){
79+
// try{
80+
//// jedis.set(key, value);
81+
// jedis.setex(key,expireTime ,value);
82+
// } catch(Exception e){
83+
// e.printStackTrace();
84+
//// ExceptionCapture.logError(e);
85+
// } finally{
86+
// recycleJedis(jedis);
87+
// }
88+
// }
89+
//
90+
// }
91+
// // 获取字符串类型的数据
92+
// public static String getString(String key){
93+
// Jedis jedis = getJedis();
94+
// String result = "";
95+
// if(jedis != null){
96+
// try{
97+
// result = jedis.get(key);
98+
// }catch(Exception e){
99+
// e.printStackTrace();
100+
//// ExceptionCapture.logError(e);
101+
// } finally{
102+
// recycleJedis(jedis);
103+
// }
104+
// }
105+
//
106+
// return result;
107+
// }
108+
// // 删除字符串数据
109+
// public static void delString(String key){
110+
// Jedis jedis = getJedis();
111+
// if(jedis != null){
112+
// try{
113+
// jedis.del(key);
114+
// }catch(Exception e){
115+
// e.printStackTrace();
116+
//// ExceptionCapture.logError(e);
117+
// } finally{
118+
// recycleJedis(jedis);
119+
// }
120+
// }
121+
// }
122+
// // 保存byte类型数据
123+
// public static void setObject(byte[] key, byte[] value){
124+
// Jedis jedis = getJedis();
125+
// String result = "";
126+
// if(jedis != null){
127+
// try{
128+
// if(!jedis.exists(key)){
129+
// jedis.set(key, value);
130+
// }
131+
// // redis中session过期时间
132+
// jedis.expire(key, expireTime);
133+
// } catch(Exception e){
134+
// e.printStackTrace();
135+
//// ExceptionCapture.logError(e);
136+
// } finally{
137+
// recycleJedis(jedis);
138+
// }
139+
// }
140+
// }
141+
// // 获取byte类型数据
142+
// public static byte[] getObject(byte[] key){
143+
// Jedis jedis = getJedis();
144+
// byte[] bytes = null;
145+
// if(jedis != null){
146+
// try{
147+
// bytes = jedis.get(key);;
148+
// }catch(Exception e){
149+
// e.printStackTrace();
150+
//// ExceptionCapture.logError(e);
151+
// } finally{
152+
// recycleJedis(jedis);
153+
// }
154+
// }
155+
// return bytes;
156+
//
157+
// }
158+
//
159+
// // 更新byte类型的数据,主要更新过期时间
160+
// public static void updateObject(byte[] key){
161+
// Jedis jedis = getJedis();
162+
// if(jedis != null){
163+
// try{
164+
// // redis中session过期时间
165+
// jedis.expire(key, expireTime);
166+
// }catch(Exception e){
167+
// e.printStackTrace();
168+
//// ExceptionCapture.logError(e);
169+
// } finally{
170+
// recycleJedis(jedis);
171+
// }
172+
// }
173+
//
174+
// }
175+
//
176+
// // key对应的整数value加1
177+
// public static void inc(String key){
178+
// Jedis jedis = getJedis();
179+
// if(jedis != null){
180+
// try{
181+
// if(!jedis.exists(key)){
182+
// jedis.set(key, "1");
183+
// jedis.expire(key, countExpireTime);
184+
// } else {
185+
// // 加1
186+
// jedis.incr(key);
187+
// }
188+
// }catch(Exception e){
189+
// e.printStackTrace();
190+
//// ExceptionCapture.logError(e);
191+
// } finally{
192+
// recycleJedis(jedis);
193+
// }
194+
// }
195+
// }
196+
//
197+
// // 获取所有keys
198+
// public static Set<String> getAllKeys(String pattern){
199+
// Jedis jedis = getJedis();
200+
// if(jedis != null){
201+
// try{
202+
// return jedis.keys(pattern);
203+
// }catch(Exception e){
204+
// e.printStackTrace();
205+
//// ExceptionCapture.logError(e);
206+
// } finally{
207+
// recycleJedis(jedis);
208+
// }
209+
// }
210+
// return null;
211+
// }
212+
//
213+
//
214+
// public static void setHashObject(byte[] key,Map<byte[],byte[]> value){
215+
// Jedis jedis = getJedis();
216+
// if(jedis != null){
217+
// try{
218+
// jedis.hmset(key,value);
219+
// } catch(Exception e){
220+
// e.printStackTrace();
221+
//// ExceptionCapture.logError(e);
222+
// } finally{
223+
// recycleJedis(jedis);
224+
// }
225+
// }
226+
// }
227+
}

0 commit comments

Comments
 (0)