实践-spring-redis

本章是整理知识内容,为强化知识长期更新。

spring-redis

为后续单元测试搭建环境。

注意搭建好redis环境。

项目地址:redis-operating

  • pom
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
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.z201.learning.redis</groupId>
<artifactId>redis-operating</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>redis-operating</name>
<description>Demo project for Spring Boot</description>

<properties>
<junit.version>4.12</junit.version>
<java.version>1.8</java.version>
<!-- pom文件需要指定打包编码集,[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent! -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>


<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.0</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>8</source>
<target>8</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
</build>

</project>
  • 编写接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package cn.z201.learning.redis.operating.service;

import java.util.Set;

/**
* @author z201.coding@gmail.com
**/
public interface RedisToolI {

/**
* 获取key字节数组结构
* @param pattern
* @return
*/
Set<String> keys(String pattern);

/**
* 删除key
* @param key
* @return
*/
Boolean del(String key);

}
  • 接口实现
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
package cn.z201.learning.redis.operating.service.impl;

import cn.z201.learning.redis.operating.service.RedisToolI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Service;

import java.util.LinkedHashSet;
import java.util.Set;

/**
* @author z201.coding@gmail.com
**/
@Service
public class RedisToolImpl implements RedisToolI {

@Autowired
private RedisTemplate redisTemplate;

@Override
public Set<String> keys(String pattern) {
return (Set) redisTemplate.execute((RedisCallback<Set>) connection -> {
RedisSerializer<String> stringRedisSerializer = redisTemplate.getStringSerializer();
Set<byte[]> sets = connection.keys(stringRedisSerializer.serialize(pattern));
Set keys = new LinkedHashSet<String>();
sets.forEach(item -> keys.add(stringRedisSerializer.deserialize(item)));
return keys;
});
}

@Override
public Boolean del(String key) {
return (Boolean) redisTemplate.execute((RedisCallback<Boolean>) connection -> {
RedisSerializer<String> stringRedisSerializer = redisTemplate.getStringSerializer();
Long status = connection.del(stringRedisSerializer.serialize(key));
if (1 == status) {
return true;
}
return false;
});
}
}
  • 单元测试
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
@Test
public void testRedisTool() {
Set<String> keys = redisTool.keys("*");
log.info("size -> {} ", keys.size());
keys.forEach(item -> log.info(" keys - > {} ", item));
if (!keys.isEmpty()) {
log.info("随机删除一个");
redisTool.del(keys.iterator().next());
keys = redisTool.keys("*");
log.info("size -> {} ", keys.size());
keys.forEach(item -> log.info(" keys - > {} ", item));
} else {
log.info("已经没有key了");
}
}

@Test
public void testDelKey() {
Set<String> keys = redisTool.keys("*");
log.info("size -> {} ", keys.size());
keys.forEach(item ->
redisTool.del(item));
keys = redisTool.keys("*");
if (!keys.isEmpty()) {
log.info("size -> {} ", keys.size());
keys.forEach(item -> log.info(" keys - > {} ", item));
} else {
log.info("已经没有key了");
}
}
  • 在redis中添加数据
1
2
192.168.31.7:6379> set key value
OK
  • 运行单元测试testRedisTool
1
2
3
4
5
6
[main] INFO  io.lettuce.core.EpollProvider - Starting without optional epoll library
[main] INFO io.lettuce.core.KqueueProvider - Starting without optional kqueue library
[main] INFO c.z.l.r.o.RedisOperatingApplicationTests - size -> 1
[main] INFO c.z.l.r.o.RedisOperatingApplicationTests - keys - > key
[main] INFO c.z.l.r.o.RedisOperatingApplicationTests - 随机删除一个
[main] INFO c.z.l.r.o.RedisOperatingApplicationTests - size -> 0
  • 单元测试testDelKey就不测试了。

END