project
learn on project springboot-learning-example
1
git clone https://github.com/JeffLi1993/springboot-learning-example.git
copy module springboot-mybatis-redis
to springboot-redis
modify pom.xml
, remove mybatis/mysql, add lombok
1 | <?xml version="1.0" encoding="UTF-8"?> |
Config
use spring-context.xml
define bean in resources
1 |
|
add annotation in test package
1 | .class) (SpringJUnit4ClassRunner |
or use application.properties
springboot configuration
add annotation in test package
1 | .class) (SpringJUnit4ClassRunner |
Cluster
add property class1
2
3
4
5
6
7
8
9
10
11
12
13
"spring.redis.cluster") (prefix =
public class ClusterConfigurationProperties {
/*
* spring.redis.cluster.nodes[0] = 127.0.0.1:7379
* spring.redis.cluster.nodes[1] = 127.0.0.1:7380
* ...
*/
List<String> nodes;
}
add springboot bean configuration1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class RedisConfiguration {
ClusterConfigurationProperties clusterProperties;
"redisTemplateCluster") (
public RedisTemplate redisTemplateCluster() {
RedisTemplate template = new RedisTemplate();
template.setConnectionFactory(
new JedisConnectionFactory(
new RedisClusterConfiguration(clusterProperties.getNodes())));
return template;
}
}
Template
base operate1
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.class) (SpringRunner
@SpringBootTest(classes = Application.class)
public class RedisTemplateTest {
private RedisTemplate<String, String> redisTemplate;
/**
* Description: 展示String数据结构操作
*/
public void testValueOperations() {
String key = "RedisTemplateTest:TEST:String:1";
ValueOperations<String, String> valueOp = redisTemplate.opsForValue();
valueOp.set(key, "1");
Assert.assertEquals("1", valueOp.get(key));
redisTemplate.delete(key);
Assert.assertNull(valueOp.get(key));
}
/**
* Description: 展示Set数据结构操作
*/
public void testSetOperations() {
String key = "RedisTemplateTest:TEST:Set:1";
SetOperations<String, String> op = redisTemplate.opsForSet();
op.add(key, "1", "2");
op.add(key, "2", "3", "4");
op.remove(key, "4");
Assert.assertEquals(Long.valueOf(3), op.size(key));
redisTemplate.delete(key);
Assert.assertEquals(Long.valueOf(0), op.size(key));
}
/**
* Description: 展示SortedSet数据结构操作
*/
public void testZSetOperations() {
String key = "RedisTemplateTest:TEST:ZSet:1";
ZSetOperations<String, String> op = redisTemplate.opsForZSet();
// 添加Key及用于排序的得分
op.add(key, "1", 1);
op.add(key, "2", 2);
op.add(key, "3", 3);
op.add(key, "4", 2);
// 更新
op.add(key, "4", 4);
Assert.assertEquals(Long.valueOf(2), op.count(key, 3, 4));
op.remove(key, "3");
Assert.assertEquals(Long.valueOf(3), op.size(key));
redisTemplate.delete(key);
Assert.assertEquals(Long.valueOf(0), op.size(key));
}
/**
* Description: 展示Hash数据结构操作
*/
public void testHashOperations() {
String key = "RedisTemplateTest:TEST:Hash:1";
HashOperations<String, String, String> op = redisTemplate.opsForHash();
op.put(key, "1", "1");
Map<String, String> map = new HashMap<String, String>();
map.put("2", "2");
map.put("3", "3");
map.put("4", "4");
map.put("5", "5");
op.putAll(key, map);
Assert.assertEquals(5, op.entries(key).size());
op.delete(key, "4", "5");
Assert.assertEquals(3, op.entries(key).size());
redisTemplate.delete(key);
Assert.assertEquals(0, op.entries(key).size());
}
}
Repository
POJO
1 |
|
Converter
1 |
|
Conversions
add conversions in springboot configuration1
2
3
4
5
public CustomConversions redisCustomConversions(){
return new CustomConversions(
Arrays.asList(new AddressToBytesConverter(), new BytesToAddressConverter()));
}
Interface
As our repository extends CrudRepository it provides basic CRUD and finder operations.
The thing we need in between to glue things together is the according Spring configuration.
1 | public interface PersonRepository extends CrudRepository<Person, String> { |
Test
case:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19.class) (SpringRunner
@SpringBootTest(classes = Application.class)
public class RepositoryRedisTemplateTest {
PersonRepository repo;
public void basicCrudOperations() {
Person lin = Person.builder()
.firstName("lin").address(new Address("JJ", "CHINA")).build();
repo.save(lin);
Assert.assertEquals(lin, repo.findOne(rand.getId()));
Assert.assertEquals(1, repo.count());
repo.delete(lin);
Assert.assertEquals(0, repo.count());
}
}
playground-5:0>hgetall persons:b9d5c6ce-44b1-4d3d-a2dd-8120c794b6f1
1) “_class”
2) “org.spring.springboot.domain.Person”
3) “id”
4) “b9d5c6ce-44b1-4d3d-a2dd-8120c794b6f1”
5) “firstName”
6) “lin”
7) “address”
8) “{“city”:”JJ”,”country”:”CHINA”}”
Redis之序列化POJO
Spring Redis(6)Redis持久化
Redis - How to configure custom conversions
https://docs.spring.io/spring-data/data-redis/docs/current/reference/html/#reference
https://docs.spring.io/spring-data/redis/docs/2.0.3.RELEASE/reference/html/#redis.repositories