本文最后更新于97 天前,其中的信息可能已经过时,如有错误请发送邮件到z18096561915@outlook.com
package com.sky.test;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* redisTemplate测试类
*/
//@SpringBootTest
public class SpringDateRedisTest {
// @Autowired
private RedisTemplate redisTemplate;
/**
* 测试RedisTemplate
*/
@Test
public void testRedisTemplate() {
ValueOperations valueOperations = redisTemplate.opsForValue();
HashOperations hashOperations = redisTemplate.opsForHash();
ListOperations listOperations = redisTemplate.opsForList();
SetOperations setOperations = redisTemplate.opsForSet();
ZSetOperations zSetOperations = redisTemplate.opsForZSet();
}
/**
* 测试String类型
*/
@Test
public void testString() {
//set get setex setnx
redisTemplate.opsForValue().set("city", "beijing");
String city = (String) redisTemplate.opsForValue().get("city");
System.out.println(city);
redisTemplate.opsForValue().set("code", "12345", 3, TimeUnit.MINUTES);
redisTemplate.opsForValue().setIfAbsent("lock", "1");
redisTemplate.opsForValue().setIfAbsent("lock", "2");
}
/**
* 测试Hash类型
*/
@Test
public void testHash() {
//put get putAll entries
redisTemplate.opsForHash().put("user", "name", "zhangsan");
redisTemplate.opsForHash().put("user", "age", "20");
Object name = redisTemplate.opsForHash().get("user", "name");
System.out.println(name);
Set keys = redisTemplate.opsForHash().keys("user");
System.out.println(keys);
List user = redisTemplate.opsForHash().values("user");
System.out.println(user);
redisTemplate.opsForHash().delete("user", "age");
}
/**
* 测试List类型
*/
@Test
public void testList() {
//leftPush rightPush leftPop rightPop size range
ListOperations listOperations = redisTemplate.opsForList();
listOperations.leftPushAll("list", "1", "2", "3");
listOperations.leftPush("list", "4");
List mylist = listOperations.range("list", 0, -1);
System.out.println(mylist);
listOperations.rightPop("list");
Long list = listOperations.size("list");
System.out.println(list);
}
/**
* 测试Set类型
*/
@Test
public void testSet() {
//add remove size members
SetOperations setOperations = redisTemplate.opsForSet();
setOperations.add("set1", "a", "b", "c");
setOperations.add("set2", "a", "b", "x");
Set set1 = setOperations.members("set1");
System.out.println(set1);
setOperations.size("set1");
Set union = setOperations.union("set1", "set2");
System.out.println(union);
setOperations.remove("set1", "a");
}
/**
* 测试ZSet类型
*/
@Test
public void testZSet() {
//zadd zincrby zrange zrem
ZSetOperations zSetOperations = redisTemplate.opsForZSet();
zSetOperations.add("zset1", "a", 1);
zSetOperations.add("zset1", "b", 2);
zSetOperations.add("zset1", "c", 3);
Set zset1 = zSetOperations.range("zset1", 0, -1);
System.out.println(zset1);
zSetOperations.incrementScore("zset1", "a", 10);
zSetOperations.remove("zset1", "b");
}
/**
* 通用命令操作
*/
@Test
public void testCommon(){
//keys exists type del
Set keys = redisTemplate.keys("*");
System.out.println(keys);
Boolean name = redisTemplate.hasKey("name");
Boolean set1 = redisTemplate.hasKey("set1");
for (Object key : keys) {
DataType type = redisTemplate.type(key);
System.out.println(key);
}
redisTemplate.delete("mylist");
}
}