苍穹外卖--SpringDataRedis针对redis中5种数据类型指令和通用指令的API操作
This commit is contained in:
179
sky-server/src/test/java/com/sky/test/SpringDataRedisTest.java
Normal file
179
sky-server/src/test/java/com/sky/test/SpringDataRedisTest.java
Normal file
@@ -0,0 +1,179 @@
|
|||||||
|
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.List;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
public class SpringDataRedisTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RedisTemplate redisTemplate;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRedisTemplate(){
|
||||||
|
System.out.println(redisTemplate);
|
||||||
|
//字符串数据
|
||||||
|
ValueOperations valueOperations = redisTemplate.opsForValue();
|
||||||
|
//哈希数据
|
||||||
|
HashOperations hashOperations = redisTemplate.opsForHash();
|
||||||
|
//列表数据
|
||||||
|
ListOperations listOperations = redisTemplate.opsForList();
|
||||||
|
//集合数据
|
||||||
|
SetOperations setOperations = redisTemplate.opsForSet();
|
||||||
|
//有序集合数据
|
||||||
|
ZSetOperations zSetOperations = redisTemplate.opsForZSet();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//Spring Data Redis使用方式_操作字符串类型的数据
|
||||||
|
@Test
|
||||||
|
public void testString(){
|
||||||
|
//set get setex setnx
|
||||||
|
//set get
|
||||||
|
redisTemplate.opsForValue().set("city","常州");
|
||||||
|
/*
|
||||||
|
为何插入数据的时候是object,而不是java中的String呢??redis存取数据时,会将java的对象调用toString转为redis中的字符串数据
|
||||||
|
*/
|
||||||
|
String city = (String) redisTemplate.opsForValue().get("city");
|
||||||
|
//setex -- 计时保存key-value
|
||||||
|
redisTemplate.opsForValue().set("code","3306",10, TimeUnit.SECONDS);
|
||||||
|
|
||||||
|
//setnx-- 先判断是否有该key值,如果没有才保存
|
||||||
|
System.out.println(redisTemplate.opsForValue().setIfAbsent("age", "1"));
|
||||||
|
System.out.println(redisTemplate.opsForValue().setIfAbsent("age", "2"));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//Spring Data Redis使用方式_操作哈希类型的数据
|
||||||
|
@Test
|
||||||
|
public void testHash(){
|
||||||
|
//hset hget hdel hkeys hvals
|
||||||
|
HashOperations hashOperations = redisTemplate.opsForHash();
|
||||||
|
//hset
|
||||||
|
hashOperations.put("s100","name","tom");
|
||||||
|
hashOperations.put("s100","age","20");
|
||||||
|
//hget
|
||||||
|
String name = (String) hashOperations.get("s100", "name");
|
||||||
|
String age = (String) hashOperations.get("s100", "age");
|
||||||
|
System.out.println(name);
|
||||||
|
System.out.println(age);
|
||||||
|
//hkeys
|
||||||
|
Set keys = hashOperations.keys("s100");
|
||||||
|
System.out.println(keys);
|
||||||
|
|
||||||
|
//hvals
|
||||||
|
List values = hashOperations.values("s100");
|
||||||
|
System.out.println(values);
|
||||||
|
//hdel
|
||||||
|
hashOperations.delete("s100","age");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//---------------------------------------以下代码演示查看一下--------------------------------
|
||||||
|
/*
|
||||||
|
操作列表类型的数据
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testList(){
|
||||||
|
//lpush lrange rpop llen
|
||||||
|
ListOperations listOperations = redisTemplate.opsForList();
|
||||||
|
//lpush
|
||||||
|
listOperations.leftPushAll("mylist", "a", "b", "c");
|
||||||
|
listOperations.leftPush("mylist", "d");
|
||||||
|
//lrange
|
||||||
|
List mylist = listOperations.range("mylist", 0, -1);
|
||||||
|
System.out.println(mylist);
|
||||||
|
//rpop
|
||||||
|
listOperations.rightPop("mylist");
|
||||||
|
//llen
|
||||||
|
Long size = listOperations.size("mylist");
|
||||||
|
System.out.println(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
操作集合类型的数据
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testSet(){
|
||||||
|
//sadd smembers scard sinter sunion srem
|
||||||
|
SetOperations setOperations = redisTemplate.opsForSet();
|
||||||
|
//sadd
|
||||||
|
setOperations.add("set1", "a", "b", "c", "d");
|
||||||
|
setOperations.add("set2", "a", "b", "e", "f");
|
||||||
|
//smembers
|
||||||
|
Set members = setOperations.members("set1");
|
||||||
|
System.out.println(members);
|
||||||
|
//scard
|
||||||
|
Long size = setOperations.size("set1");
|
||||||
|
System.out.println(size);
|
||||||
|
//sinter
|
||||||
|
Set intersect = setOperations.intersect("set1", "set2");
|
||||||
|
System.out.println(intersect);
|
||||||
|
//sunion
|
||||||
|
Set union = setOperations.union("set1", "set2");
|
||||||
|
System.out.println(union);
|
||||||
|
//srem
|
||||||
|
setOperations.remove("set1", "a","b");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
操作有序集合类型的数据
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testLinkedSet(){
|
||||||
|
//zadd zrange zincrby zrem
|
||||||
|
ZSetOperations zSetOperations = redisTemplate.opsForZSet();
|
||||||
|
//zadd
|
||||||
|
zSetOperations.add("zset1", "a", 10);
|
||||||
|
zSetOperations.add("zset1", "b", 12);
|
||||||
|
zSetOperations.add("zset1", "c", 15);
|
||||||
|
//zrange
|
||||||
|
Set zset1 = zSetOperations.range("zset1", 0, -1);
|
||||||
|
System.out.println(zset1);
|
||||||
|
//zincrby
|
||||||
|
zSetOperations.incrementScore("zset1", "b", 6);
|
||||||
|
//zrem
|
||||||
|
zSetOperations.remove("zset1", "b","c");
|
||||||
|
|
||||||
|
System.out.println(zSetOperations.range("zset1", 0, -1));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
通用命令操作
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testCommon(){
|
||||||
|
//keys exists type del
|
||||||
|
//keys
|
||||||
|
Set keys = redisTemplate.keys("*");
|
||||||
|
System.out.println(keys);
|
||||||
|
|
||||||
|
//exists
|
||||||
|
Boolean isNameExists = redisTemplate.hasKey("name");
|
||||||
|
Boolean isSetExists = redisTemplate.hasKey("set1");
|
||||||
|
|
||||||
|
System.out.println(isNameExists);
|
||||||
|
System.out.println(isSetExists);
|
||||||
|
|
||||||
|
for (Object key : keys) {
|
||||||
|
//type
|
||||||
|
DataType type = redisTemplate.type(key);
|
||||||
|
System.out.println(key+"的类型:"+type);
|
||||||
|
}
|
||||||
|
|
||||||
|
//del
|
||||||
|
redisTemplate.delete("mylist");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user