外卖项目day05-在Java中操作Redis_Spring Data Redis使用方式_其他类型的数据操作
This commit is contained in:
@@ -3,6 +3,7 @@ package com.sky.test;
|
|||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.data.redis.connection.DataType;
|
||||||
import org.springframework.data.redis.core.*;
|
import org.springframework.data.redis.core.*;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -79,4 +80,103 @@ public class SpringDateRedisTest {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
操作列表类型的数据
|
||||||
|
*/
|
||||||
|
@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);//dcba
|
||||||
|
//rpop
|
||||||
|
listOperations.rightPop("mylist");
|
||||||
|
//llen
|
||||||
|
Long size = listOperations.size("mylist");
|
||||||
|
System.out.println(size);//3
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
操作集合类型的数据
|
||||||
|
*/
|
||||||
|
@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);//abcd
|
||||||
|
//scard
|
||||||
|
Long size = setOperations.size("set1");
|
||||||
|
System.out.println(size);//4
|
||||||
|
//sinter
|
||||||
|
Set intersect = setOperations.intersect("set1", "set2");
|
||||||
|
System.out.println(intersect);//ab
|
||||||
|
//sunion
|
||||||
|
Set union = setOperations.union("set1", "set2");
|
||||||
|
System.out.println(union);//abcdef
|
||||||
|
//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