SpringBoot developed twenty -Redis entry Redis and Spring Integration

Install Redis, familiar Redis commands and integrate Redis, using Redis in Spring.

Code

Redis built 16 libraries, the index is 0-15, the default choice of 0

Redis commonly used commands:

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

select 1

flushdb

Keys *

Data type String operations:

K1 the SET 1
// get key value
GET K1
// delete key values
del K1
// increment the value of the key from minus
incr K1
DECR K1

hash operation types of data:
// set the key value
format is: entry key value HSET hash key items of
example: HSET myhash ID . 1
// plurality of pairs of key value
format: the hash key hmset key items of the item value
example: hmset myhash the above mentioned id 1 name xixi
// get the value of key
format: hget hash key of the key items
such as: hget myhash the above mentioned id
// get value for key is a multi-
format: hmget hash key of the key items
such as: myhash the above mentioned id name hmget
// get all values in this key
format is: hgetall hash of key
example: hgetall myhash
// If the item does not exist assignment, there is nothing to do when the
format is: hsetnx hash key items of key items value
example: hsetnx myhash address Earth
// determines whether there is a key
format is: hexists key hash of the key items
example: hexists myhash id

Spring introduced Redis

First to introduce rely on pom.xml

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

Redis then need to configure the database you want to configure parameters in the configuration file application.properties inside.

1
2
3
4
# RedisProperties
spring.redis.database=11
spring.redis.host=localhost
spring.redis.port=6379

Then configure the class to write to construct RedisTemplate. Actually SpringBoot Redis also been configured RedisTemplate, but because Redis is a key-value database, which at the time did become the key configuration Object types, but because we usually use String, so the re-allocation.

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
package com.nowcoder.community.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;


public class {
// 我们要通过 Template 访问数据库,那么它要想具备访问数据库的能力就要能创建连接,而连接又是由连接工厂创建的,所以要把连接工厂注入进来

public RedisTemplate<String, Object> (RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
// 使得Template有访问数据库的能力
template.setConnectionFactory(redisConnectionFactory);

// 设置key的序列化的方式
template.setKeySerializer(RedisSerializer.string());
// 设置value的序列化方式
template.setValueSerializer(RedisSerializer.json());
// 设置hash的key的序列化的方式
template.setHashKeySerializer(RedisSerializer.string());
// 设置hash的value的序列化的方式
template.setHashValueSerializer(RedisSerializer.json());

template.afterPropertiesSet();
return template;
}
}

然后访问数据的时候就是以下用法:

1
2
3
4
5
redisTemplate.opsForValue(); // 访问String
redisTemplate.opsForHash(); // 访问Hash
redisTemplate.opsForList(); // 访问List
redisTemplate.opsForSet(); // 访问Set
redisTemplate.opsForZSet(); // 访问ZSet

现在就可以通过 RedisTemplate 来访问Redis数据库,来添加删除数据。

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
10 大专栏  SpringBoot开发二十-Redis入门以及Spring整合Redis6
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package com.nowcoder.community;

import org.aspectj.lang.annotation.Aspect;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.concurrent.TimeUnit;

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = CommunityApplication.class)
public class RedisTests {
@Autowired
private RedisTemplate redisTemplate;

@Test
public void testStrings() {
String redisKey = "test:count";
redisTemplate.opsForValue().set(redisKey, 1);
System.out.println(redisTemplate.opsForValue().get(redisKey));
System.out.println(redisTemplate.opsForValue().increment(redisKey));
System.out.println(redisTemplate.opsForValue().decrement(redisKey));
}

@Test
public void testHashes() {
String redisKey = "test:user";

redisTemplate.opsForHash().put(redisKey, "id", 1);
redisTemplate.opsForHash().put(redisKey, "username", "zhangsan");

System.out.println(redisTemplate.opsForHash().get(redisKey, "id"));
System.out.println(redisTemplate.opsForHash().get(redisKey, "username"));
}

@Test
public void testLists() {
String redisKey = "test:ids";

redisTemplate.opsForList().leftPush(redisKey, 101);
redisTemplate.opsForList().leftPush(redisKey, 102);
redisTemplate.opsForList().leftPush(redisKey, 103);

System.out.println(redisTemplate.opsForList().size(redisKey));
System.out.println(redisTemplate.opsForList().index(redisKey, 0));
System.out.println(redisTemplate.opsForList().range(redisKey, 0, 2));

// 从左边弹出,左边leftpop,右边rightpop
System.out.println(redisTemplate.opsForList().leftPop(redisKey));
System.out.println(redisTemplate.opsForList().leftPop(redisKey));
System.out.println(redisTemplate.opsForList().leftPop(redisKey));
}

@Test
public void testSets() {
String redisKey = "test:teachers";

redisTemplate.opsForSet().add(redisKey, "刘备", "关羽", "张飞", "赵云", "诸葛亮");

System.out.println(redisTemplate.opsForSet().size(redisKey));
System.out.println(redisTemplate.opsForSet().pop(redisKey));
System.out.println(redisTemplate.opsForSet().members(redisKey));
}

@Test
public void testSortedSets() {
String redisKey = "test:students";

redisTemplate.opsForZSet().add(redisKey, "唐僧", 80);
redisTemplate.opsForZSet().add(redisKey, "悟空", 90);
redisTemplate.opsForZSet().add(redisKey, "八戒", 50);
redisTemplate.opsForZSet().add(redisKey, "沙僧", 70);
redisTemplate.opsForZSet().add(redisKey, "白龙马", 60);

// 统计数据
System.out.println(redisTemplate.opsForZSet().zCard(redisKey));
// 查询某个人的分数
System.out.println(redisTemplate.opsForZSet().score(redisKey, "八戒"));
// 查询排名,默认从小到大
System.out.println(redisTemplate.opsForZSet().reverseRank(redisKey, "八戒"));
// 查询区间排名数据
System.out.println(redisTemplate.opsForZSet().reverseRange(redisKey, 0, 2));
}

@Test
public void testKeys() {
redisTemplate.delete("test:user");

System.out.println(redisTemplate.hasKey("test:user"));

redisTemplate.expire("test:students", 10, TimeUnit.SECONDS);
}

// 多次访问同一个key
@Test
public void testBoundOperations() {
String redisKey = "test:count";
BoundValueOperations operations = redisTemplate.boundValueOps(redisKey);
operations.increment();
operations.increment();
operations.increment();
operations.increment();
operations.increment();
System.out.println(operations.get());
}

// Redis编程序事务,并不一定满足我们之前说过的事务,因为不是关系型数据库
@Test
public void testTransactional() {
Object obj = redisTemplate.execute(new SessionCallback() {
@Override
public Object execute(RedisOperations operations) throws DataAccessException {
String redisKey = "test:tx";
// 启用事务
operations.multi();

operations.opsForSet().add(redisKey, "zhangsan");
operations.opsForSet().add(redisKey, "lisi");
operations.opsForSet().add(redisKey, "wangwu");

System.out.println(operations.opsForSet().members(redisKey));
// 提交事务
return operations.exec();
}
});
System.out.println(obj);
}
}

Guess you like

Origin www.cnblogs.com/lijianming180/p/12014228.html