import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTemplateTests {
@Autowired
RedisTemplate<String, String> redisTemplate;
@Test
//字符串操作
public void opsForValue() {
redisTemplate.opsForValue().set("key","abc");//插入key
System.out.println(redisTemplate.opsForValue().get("key"));//获取value
System.out.println(redisTemplate.opsForValue().size("key"));//获取value长度>>3
System.out.println(redisTemplate.delete("key"));//删除key
System.out.println(redisTemplate.opsForValue().get("key"));//获取value>>null
System.out.println(redisTemplate.opsForValue().getAndSet("key","def"));//获取value,如果没有就设值
System.out.println(redisTemplate.opsForValue().get("key",0,1));//获取并截取指定长度的value>>de
System.out.println(redisTemplate.opsForValue().append("key","fcd"));//拼接字符串并返回拼接后的长度>>6
}
@Test
//set操作
public void opsForSet() {
redisTemplate.opsForSet().add("set1","a","b","c","d");//插入元素
redisTemplate.opsForSet().add("set2","c","d","e","f");//插入元素
System.out.println(redisTemplate.opsForSet().size("set1"));//获取长度
System.out.println(redisTemplate.opsForSet().members("set1"));//获取所有元素>>[b, d, c, a]
System.out.println(redisTemplate.opsForSet().randomMember("set1"));//随机获取列表中的某个元素
System.out.println(redisTemplate.opsForSet().randomMembers("set1",2));//随机获取列表中的n个元素>>[c, a]
System.out.println(redisTemplate.opsForSet().isMember("set1","a"));//判断是否有指定元素>>true
System.out.println(redisTemplate.opsForSet().difference("set1", "set2"));//比较1和2的不同>>[b, a]
System.out.println(redisTemplate.opsForSet().difference("set2", "set1"));//[e, f]
System.out.println(redisTemplate.opsForSet().intersect("set1","set2"));//获取两个集合交集>>[d, c]
System.out.println(redisTemplate.opsForSet().intersectAndStore("set1","set2","set3"));//获取两个集合交集后存储到另一个集合,并返回元素个数>>2
System.out.println(redisTemplate.opsForSet().union("set1","set2"));//合并两个集合>>[d, c, f, e, a]
System.out.println(redisTemplate.opsForSet().unionAndStore("set1","set2","set4"));//合并两个集合后存储到另一个集合,并返回元素个数>>5
redisTemplate.opsForSet().remove("set1","b");//移除指定元素
System.out.println(redisTemplate.opsForSet().members("set1"));
redisTemplate.opsForSet().pop("set3");//从右侧弹出变量中的元素
redisTemplate.opsForSet().pop("set4",2);//从右侧弹出变量中的n个元素
}
@Test
//列表操作-在列表的左侧添加元素
public void opsForList_leftPush() {
redisTemplate.opsForList().leftPush("list","a");//列表插入一个元素
redisTemplate.opsForList().leftPush("list","b");//追加多一个元素
redisTemplate.opsForList().leftPushAll("list","c","d");//列表插入多个元素
List<String> list = new ArrayList<String>(){{add("e");add("f");}};
redisTemplate.opsForList().leftPushAll("list",list);//插入列表
for (int i = 8; i >= 0; i--) {
System.out.println(i+": "+redisTemplate.opsForList().leftPop("list"));
}
}
@Test
//列表操作-在列表的右侧添加元素
public void opsForList_rightPush() {
redisTemplate.opsForList().rightPush("list","a");//列表插入一个元素
redisTemplate.opsForList().rightPush("list","b");//追加多一个元素
redisTemplate.opsForList().rightPushAll("list","c","d");//列表插入多个元素
List<String> list = new ArrayList<String>(){{add("e");add("f");}};
redisTemplate.opsForList().rightPushAll("list",list);//插入列表
for (int i = 8; i >= 0; i--) {
System.out.println(i+": "+redisTemplate.opsForList().rightPop("list"));
}
}
@Test
//列表操作-其它操作
public void opsForList_other() {
redisTemplate.opsForList().rightPushAll("list","a","b","c","d");
System.out.println(redisTemplate.opsForList().index("list", 0));//获取第n个元素
System.out.println(redisTemplate.opsForList().size("list"));//获取列表长度
System.out.println(redisTemplate.opsForList().range("list", 0, -1));//获取指定区间的值
redisTemplate.opsForList().remove("list",-1,"c");//n>0就是从左边删除n个元素d:n<0就是从右边删除n个元素;n为0就是删除所有;
System.out.println(redisTemplate.opsForList().range("list", 0, -1));
redisTemplate.opsForList().set("list",1,"a");//在指定索引插入元素
System.out.println(redisTemplate.opsForList().range("list", 0, -1));
redisTemplate.opsForList().trim("list",0,5);//截取并保留指定范围内长度
System.out.println(redisTemplate.opsForList().range("list", 0, -1));
}
}
RedisTemplate常用操作及解析
最新推荐文章于 2023-05-24 21:37:51 发布