前言
记录一下redisson 客户端操作redis list 的使用案例demo
一、引入maven
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.15.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
二、 yaml
和 RedisConfig
配置类
spring:
application:
name: dem-backend
redis:
# TODO 密码
password: dev63a2f55f7ff1d588176f87f8dff2fcbc
sentinel:
# TODO 集群名
master: mymaster
# TODO 集群
nodes:
- 192.168.16.125:8190
- 192.168.16.125:8191
- 192.168.16.125:8192
三、 测试类
(一)多节点使用案例
**
* @author lvzb
* @date 2022/12/19 17:29
**/
@Slf4j
@SpringBootTest
public class RedisClientTest {
@Autowired
private RedissonClient client;
private static final String DEM_BUSI_META_DATA = "DEM_BUSI_META_DATA";
@Test
void getTest() {
Iterable<String> keys = client.getKeys().getKeys();
Iterator<String> iterator = keys.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
System.out.println(key);
}
}
@Test
void getKeysTest() {
Iterable<String> keys = client.getKeys().getKeys();
Stream<String> keysStream = StreamSupport.stream(keys.spliterator(), false);
keysStream.filter(k -> k.startsWith("DEM")).forEach(System.out::println);
}
@Test
void getData() {
RList<BusiMetadataVO> list = client.getList(DEM_BUSI_META_DATA);
log.error("{}", list.isExists());
if (list.isExists()) {
list.stream().limit(10).forEach(System.out::println);
list.stream().limit(1).findFirst().ifPresent(v ->
v.setNotice(0)
);
BusiMetadataVO vo = list.get(0);
vo.setNotice(0);
// TODO: 更新时需要使用 fastSet
list.fastSet(0, vo);
// TODO: 移除redis list 中 下标0的内容
list.fastRemove(0);
// TODO: 在某个对象前追加, 和头部追加
list.addBefore(list.get(0), list.get(1));
list.add(0, list.get(3));
}else {
List<BusiMetadataVO> voList = new ArrayList<>();
List<BusiMetadataVO> data = voList.stream()
.sorted(Comparator.comparing(BusiMetadataVO::getUpdateTime).thenComparing(BusiMetadataVO::getEnglish_full_name).reversed())
.collect(toList());
// do cache
RList<BusiMetadataVO> cacheList = client.getList(DEM_BUSI_META_DATA);
if (cacheList.isExists()) {
cacheList.clear();
cacheList.addAll(data);
} else {
cacheList.addAll(data);
cacheList.expire(12, TimeUnit.HOURS);
}
}
log.warn("==========================================");
// TODO: 清空该 redis 该 list的内容,KEY 会继续保留
list.clear();
list.stream().forEach(System.out::println);
}
}
(二)单节点使用案例
/**
* @author lvzb
* @date 2022/12/19 17:02
**/
@Slf4j
public class RedisTest {
public static RedissonClient client;
static List<BusiMetadataVO> generateData() {
List<BusiMetadataVO> result = new ArrayList<>();
for (int i = 0; i < 20; i++) {
BusiMetadataVO vo = BusiMetadataVO.builder()
.termGuid("" + i)
.english_full_name("English full Name" + i)
.english_abbr("english abbr" + i)
.business_term("业务术语" + i)
.variable_properties("变量属性variable" + i)
.meaning_usage("含义和用法usage" + i)
.messagedomain_label("F4" + i)
.remark("remark 备注" + i)
.association(i)
.notice(i)
.updateTime(LocalDateTime.now())
.build();
result.add(vo);
log.warn(">>>> vo:: {}", vo);
}
return result;
}
@BeforeEach
void beforeEach() {
Config config = new Config();
// 使用单机Redis服务
config.useSingleServer().setAddress("redis://127.0.0.1:6379");
// 创建Redisson客户端
client = Redisson.create(config);
}
@Test
void saveKeys() {
final String key = "savKeys";
RList<BusiMetadataVO> kList = client.getList(key);
log.error(">>>>> 是否存在 {}", kList.isExists());
kList.addAll(generateData());
kList.expire(120l, TimeUnit.SECONDS);
}
@Test
void getKeys() {
final String key = "savKeys";
final int start = 2;
final int size = 10;
RList<BusiMetadataVO> kList = client.getList(key);
log.error(">>>>> 是否存在 {}", kList.isExists());
if (kList.isExists()) {
kList.stream().forEach(System.out::println);
}
List<BusiMetadataVO> voList = kList.stream().skip((start - 1) * size).limit(10).collect(toList());
}
@Test
void deleteKey() {
final String key = "savKeys";
long delete = client.getKeys().delete(key);
System.out.println(">>>> 删除key::" + delete);
}
@Test
void getKeysTest() {
Iterable<String> keys = client.getKeys().getKeys();
Stream<String> keysStream = StreamSupport.stream(keys.spliterator(), false);
keysStream.filter(k -> k.startsWith("DEM")).forEach(System.out::println);
}
@Test
void getTest() {
Iterable<String> keys = client.getKeys().getKeys();
Iterator<String> iterator = keys.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
System.out.println(key);
}
}
}