Skip to content

Commit 9d20580

Browse files
committed
redis测试
1 parent b57b595 commit 9d20580

File tree

4 files changed

+552
-0
lines changed

4 files changed

+552
-0
lines changed

redis/src/main/java/info/xiaomo/redis/RedisMain.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
5+
import org.springframework.cache.CacheManager;
6+
import org.springframework.cache.annotation.EnableCaching;
7+
import org.springframework.context.annotation.Bean;
58
import org.springframework.context.annotation.ComponentScan;
69
import org.springframework.context.annotation.Configuration;
10+
import org.springframework.data.redis.cache.RedisCacheManager;
11+
import org.springframework.data.redis.connection.RedisConnectionFactory;
12+
import org.springframework.data.redis.core.RedisTemplate;
13+
import org.springframework.data.redis.core.StringRedisTemplate;
14+
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
715

816
/**
917
* 把今天最好的表现当作明天最新的起点..~
@@ -22,9 +30,23 @@
2230
@Configuration
2331
@EnableAutoConfiguration
2432
@ComponentScan("info.xiaomo")
33+
@EnableCaching
2534
public class RedisMain {
2635
public static void main(String[] args) throws Exception {
2736
SpringApplication.run(RedisMain.class, args);
2837
}
2938

39+
@Bean
40+
public CacheManager cacheManager(RedisTemplate redisTemplate) {
41+
return new RedisCacheManager(redisTemplate);
42+
}
43+
44+
@Bean
45+
public RedisTemplate<String, String> redisTemplate(
46+
RedisConnectionFactory factory) {
47+
final StringRedisTemplate template = new StringRedisTemplate(factory);
48+
template.setValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class)); //请注意这里
49+
return template;
50+
}
51+
3052
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package info.xiaomo.redis.controller;
2+
3+
import info.xiaomo.core.controller.Result;
4+
import info.xiaomo.redis.dao.CommonRedisDao;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.PathVariable;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RequestMethod;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
/**
12+
* 把今天最好的表现当作明天最新的起点..~
13+
* いま 最高の表現 として 明日最新の始発..~
14+
* Today the best performance as tomorrow newest starter!
15+
* Created by IntelliJ IDEA.
16+
*
17+
* @author: xiaomo
18+
* @github: https://github.com/qq83387856
19+
* @email: hupengbest@163.com
20+
* @QQ_NO: 83387856
21+
* @Date: 2016/11/14 17:25
22+
* @Copyright(©) 2015 by xiaomo.
23+
**/
24+
25+
@RestController
26+
@RequestMapping("/redis")
27+
public class TestController {
28+
29+
private final CommonRedisDao dao;
30+
31+
@Autowired
32+
public TestController(CommonRedisDao dao) {
33+
this.dao = dao;
34+
}
35+
36+
@RequestMapping(value = "get/{key}", method = RequestMethod.GET)
37+
public Result find(@PathVariable("key") String key) {
38+
String value = dao.getValue(key);
39+
return new Result(value);
40+
}
41+
42+
@RequestMapping(value = "add/{key}/{value}", method = RequestMethod.GET)
43+
public Result add(@PathVariable("value") String value, @PathVariable("key") String key) {
44+
return new Result(dao.cacheValue(key, value));
45+
}
46+
47+
@RequestMapping(value = "del/{key}", method = RequestMethod.GET)
48+
public Result del(@PathVariable("key") String key) {
49+
return new Result(dao.removeValue(key));
50+
}
51+
52+
@RequestMapping(value = "count/{key}", method = RequestMethod.GET)
53+
public Result count(@PathVariable("key") String key) {
54+
return new Result(dao.getListSize(key));
55+
}
56+
57+
58+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package info.xiaomo.redis.dao;
2+
3+
import org.springframework.data.redis.core.ListOperations;
4+
5+
import java.util.List;
6+
import java.util.Set;
7+
8+
/**
9+
* 把今天最好的表现当作明天最新的起点..~
10+
* いま 最高の表現 として 明日最新の始発..~
11+
* Today the best performance as tomorrow newest starter!
12+
* Created by IntelliJ IDEA.
13+
*
14+
* @author: xiaomo
15+
* @github: https://github.com/qq83387856
16+
* @email: hupengbest@163.com
17+
* @QQ_NO: 83387856
18+
* @Date: 2016/11/14 17:59
19+
* @Description: 用户实体类
20+
* @Copyright(©) 2015 by xiaomo.
21+
**/
22+
23+
public interface CommonRedisDao {
24+
25+
/**
26+
* 添加
27+
*/
28+
boolean cacheValue(String key, String value, long time);
29+
boolean cacheValue(String key, String value);
30+
31+
/**
32+
* 是否包含
33+
*/
34+
boolean containsValueKey(String key);
35+
boolean containsSetKey(String key);
36+
boolean containsListKey(String key);
37+
boolean containsKey(String key);
38+
39+
/**
40+
* 获取缓存
41+
*/
42+
String getValue(String key);
43+
44+
/**
45+
* 移除缓存
46+
*/
47+
boolean removeValue(String key);
48+
boolean removeSet(String key);
49+
boolean removeList(String key);
50+
51+
/**
52+
* 缓存Set
53+
*/
54+
boolean cacheSet(String key, String value, long time);
55+
boolean cacheSet(String key, String value);
56+
boolean cacheSet(String k, Set<String> v, long time);
57+
boolean cacheSet(String k, Set<String> v);
58+
59+
/**
60+
* 获取Set
61+
*/
62+
Set<String> getSet(String k);
63+
64+
/**
65+
* 缓存List
66+
*/
67+
boolean cacheList(String k, String v, long time);
68+
boolean cacheList(String k, String v);
69+
boolean cacheList(String k, List<String> v, long time);
70+
boolean cacheList(String k, List<String> v);
71+
72+
/**
73+
* 获取List
74+
*/
75+
List<String> getList(String k, long start, long end);
76+
77+
/**
78+
* 获取页码
79+
*/
80+
long getListSize(String key);
81+
long getListSize(ListOperations<String, String> listOps, String k);
82+
83+
/**
84+
* 移除list缓存
85+
*/
86+
boolean removeOneOfList(String k);
87+
}

0 commit comments

Comments
 (0)