redis集成springboot操作(list,hash,set,zset,string)

####hash####################################################
import com.csw.demoRedis.entity.User;
import com.google.gson.Gson;
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.core.StringRedisTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 1)对象存储,HSET KEY_NAME FIELD VALUE或者HMSET KEY_NAME FIELD1 VALUE1 ...FIELDN VALUEN。
 * 

* 比如购物车的实现 */ @SpringBootTest public class HashTest { @Autowired private StringRedisTemplate stringRedisTemplate; /** * 加入缓存 */ @Test public void test1() { HashMap map = new HashMap(); map.put("aa", "aa"); map.put("user", new User("张三", 1, "好").toString()); map.put("int", "8"); stringRedisTemplate.opsForHash().putAll("AA-hash", map); } /** * 获取一个key 的所有hashkey和value */ @Test public void test2() { Map entries = stringRedisTemplate.opsForHash().entries("AA-hash"); for (Object o : entries.keySet()) { System.out.print("键】" + o); System.out.println("值】" + entries.get(o)); } } /** * 验证指定key下面有没有hash key */ @Test public void test3() { Boolean aa = stringRedisTemplate.opsForHash().hasKey("AA-hash", "aa"); System.out.println("是否含有】" + aa); } /** * 获取指定key 的value值 */ @Test public void test4() { stringRedisTemplate.opsForHash().put("AA-hash", "userJson", new Gson().toJson(new User("张三", 1, "好"))); Object aa = stringRedisTemplate.opsForHash().get("AA-hash", "userJson"); User user = new Gson().fromJson((String) aa, User.class); System.out.println("获取到的值为]" + user.getName()); int anInt = Integer.parseInt((String) stringRedisTemplate.opsForHash().get("AA-hash", "int")); System.out.println("获取到整形的值为]" + anInt); } /** * 删除指定hash 的hashkey并返回删除数量 */ @Test public void test5() { Long delete = stringRedisTemplate.opsForHash().delete("AA-hash", "aa", "int"); System.out.println("删除的个数为]" + delete); } /** * 指定hash 的hashkey做增减操作 * long 和double都可以 */ @Test public void test6() { Double aa = stringRedisTemplate.opsForHash().increment("AA-hash", "aa", 1.0); System.out.println("增加后的值】" + aa); } /** * 获取key 下面的所有hashkey */ @Test public void test7() { Set keys = stringRedisTemplate.opsForHash().keys("AA-hash"); for (Object o : keys) { System.out.println("hashkey】" + o); } List values = stringRedisTemplate.opsForHash().values("AA-hash"); System.out.println("values】" + values.toString()); } /** * 获取key 下面的所有hashkey键值对个数 */ @Test public void test8() { Long size = stringRedisTemplate.opsForHash().size("AA-hash"); System.out.println("hashkey个数大小】" + size); } } ####list##################################################### import com.csw.demoRedis.entity.User; import com.google.gson.Gson; 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.core.StringRedisTemplate; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; /** * 常用的数据结构实现 * 消息流 */ @SpringBootTest public class ListTest { @Autowired private StringRedisTemplate stringRedisTemplate; /** * 测试左右添加 * 左添加 * 左1,左2,右3 结果213 */ @Test public void test1() { List list = new ArrayList(); for (int i = 8; i < 10; i++) { list.add(new User("名字" + i, i, "状态" + i)); } stringRedisTemplate.opsForList().rightPush("AA-list", new Gson().toJson(list)); } /** * 获取指定位置的值 */ @Test public void test2() { String index = stringRedisTemplate.opsForList().index("AA-list", 1); System.out.println("值为】" + index); } /** * 获取指定区间的值 */ @Test public void test3() { List range = stringRedisTemplate.opsForList().range("AA-list", 1, 2); for (String str : range) { System.out.println("zhi】" + str); } } /** * 将一个参数值放到另一个的前面 */ @Test public void test4() { List list = new ArrayList(); for (int i = 8; i < 10; i++) { list.add(new User("名字" + i, i, "状态" + i)); } stringRedisTemplate.opsForList().leftPush("AA-list", new Gson().toJson(list), "放前面"); } /** * 测试左右批量添加 */ @Test public void test5() { stringRedisTemplate.opsForList().leftPushAll("AA-list", "aa", "bb", "cc"); stringRedisTemplate.opsForList().rightPushAll("AA-list", "aa1", "bb1", "cc1"); } /** * 测试集合大小 */ @Test public void test6() { Long size = stringRedisTemplate.opsForList().size("AA-list"); System.out.println("大小为】" + size); } /** * 测试左移除,右移除 * 移除左边第一个元素 */ @Test public void tes7() { stringRedisTemplate.opsForList().leftPop("AA-list"); } /** * 测试左移除,右移除 * 移除左边第一个元素,如果超过等待时间没有就退出 */ @Test public void tes8() { stringRedisTemplate.opsForList().leftPop("AA-list", 1, TimeUnit.MINUTES); } /** * 保留列表特定区间的元素 */ @Test public void tes9() { stringRedisTemplate.opsForList().trim("AA-list", 2, 7); } } ####set##################################################### import com.csw.demoRedis.entity.User; 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.core.StringRedisTemplate; import java.util.List; import java.util.Set; /** * 1)抽奖 * 2)点赞、收藏、标签 * 3)关注模型 */ @SpringBootTest class SetTest { private static final String AA = "aa_set"; private static final String BB = "bb_set"; private static final String VALUE = "花好月圆的缘故都给我465dffgy"; @Autowired private StringRedisTemplate stringRedisTemplate; /** * 将值放入set中 */ @Test public void test1() { stringRedisTemplate.opsForSet().add(AA, BB); } /** * 获取set中的值 */ @Test public void test2() { stringRedisTemplate.opsForSet().add(AA, BB); Set members = stringRedisTemplate.opsForSet().members(AA); for (String str : members) { System.out.println("set中的值】" + str); } } /** * 随机获取变量中指定个数的元素 * 获取会出现重复 */ @Test public void test3() { stringRedisTemplate.opsForSet().add(AA + "3", "aa", "bb", "cc", "dd", "ee"); //不会删除 List members = stringRedisTemplate.opsForSet().randomMembers(AA + "3", 4); //会删除 //stringRedisTemplate.opsForSet().pop("AA" + 3, 2); for (String str : members) { System.out.println("set中的值】" + str); } } /** * 随机获取变量的元素(一个) */ @Test public void test4() { stringRedisTemplate.opsForSet().add(AA + "3", "aa", "bb", "cc", "dd", "ee"); String member = stringRedisTemplate.opsForSet().randomMember(AA + "3"); System.out.println("set中的值】" + member); } /** * 弹出变量中的元素(弹出之后相当于就删掉了) */ @Test public void test5() { String pop = stringRedisTemplate.opsForSet().pop(AA + "3"); System.out.println("弹出了】" + pop); } /** * 获取变量中值的长度 */ @Test public void test6() { Long size = stringRedisTemplate.opsForSet().size(AA + "3"); System.out.println("值的长度】" + size); } /** * 判断value在某个key中是否存在 */ @Test public void test7() { User user = new User("张三", 1, "好"); stringRedisTemplate.opsForSet().add(AA + "3", user.toString()); Boolean dd = stringRedisTemplate.opsForSet().isMember(AA + "3", user.toString()); System.out.println("是否存在】" + dd); } /** * 判断是否转移成功 */ @Test public void test8() { User user = new User("张三", 1, "好"); Boolean move = stringRedisTemplate.opsForSet().move(AA + 3, user.toString(), BB + 3); System.out.println("是否转移成功】" + move); } /** * 批量移除set中的元素 */ @Test public void test9() { Long remove = stringRedisTemplate.opsForSet().remove(AA + 3, "aa", "bb", "cc", "dd", "ee"); System.out.println("成功移除了几个】" + remove); } /** * 通过给定的key求差值 * 差值为】[cc] * 交集为】[bb, aa] * 合集为】[cc, bb, User(name=张三, id=1, status=好), aa] */ @Test public void test10() { User user = new User("张三", 1, "好"); stringRedisTemplate.opsForSet().add(BB + "3", "aa", "bb", user.toString()); stringRedisTemplate.opsForSet().add(AA + "3", "aa", "bb", "cc"); //左边有,右边没有的 Set difference = stringRedisTemplate.opsForSet().difference(AA + 3, BB + 3); System.out.println("差值为】" + difference.toString()); //求合集 Set intersect = stringRedisTemplate.opsForSet().intersect(AA + 3, BB + 3); System.out.println("交集为】" + intersect.toString()); //求合集 Set union = stringRedisTemplate.opsForSet().union(AA + 3, BB + 3); System.out.println("合集为】" + union.toString()); } } ####string################################################### 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.core.StringRedisTemplate; import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeUnit; /** * 1)单值存储,相当于java的map;比如需要做密钥的缓存,set指令 *

* 2)计数器,比如统计阅读数,incr指令 *

* 3)分布式锁,分布式系统有一个问题是多机导致的同步锁问题,使用redis统一控制锁就简单多了。比如使用redisson获取写锁,注意过期时间的问题。set指令 *

* 4)分库时批量申请全局序列号,INCRBY KEY_NAME INCR_AMOUNT指令 */ @SpringBootTest class StringTest { private static final String AA = "aa"; private static final String BB = "bb"; private static final String VALUE = "花好月圆的缘故都给我465dffgy"; @Autowired private StringRedisTemplate stringRedisTemplate; /** * //设置一个key并设置过期时间 */ @Test public void test1() { stringRedisTemplate.opsForValue().set(AA, VALUE, 1, TimeUnit.MINUTES); } /** * //设置一个key(可设置可不设置,建议同时设置),随后设置过期时间 */ @Test public void test2() { stringRedisTemplate.opsForValue().set(BB, VALUE); stringRedisTemplate.expire(AA, 1, TimeUnit.MINUTES); } /** * //查看一个key剩余过期时间,如果是永不失效就重新设置过期时间 */ @Test public void test3() { Long expireTime = stringRedisTemplate.getExpire(BB, TimeUnit.MINUTES); System.out.println("剩余过期时间】" + expireTime); if (expireTime == -1) { stringRedisTemplate.expire(BB, 1, TimeUnit.MINUTES); } } /** * //判断一个key是否存在 */ @Test public void test4() { Boolean aBoolean = stringRedisTemplate.hasKey(AA); System.out.println("key存在吗】" + aBoolean); } /** * //移除指定key的过期时间 */ @Test public void test5() { stringRedisTemplate.opsForValue().set(AA, VALUE, 1, TimeUnit.MINUTES); stringRedisTemplate.boundValueOps(AA).persist(); } /** * 根据key获取值 */ @Test public void test6() { stringRedisTemplate.opsForValue().set(AA, VALUE, 1, TimeUnit.MINUTES); //不存在aa则为null String aa = stringRedisTemplate.opsForValue().get(BB); System.out.println("获取到的值为】" + aa); } /** * 批量添加key */ @Test public void test7() { Map map = new HashMap(); map.put("aa", "aa1"); map.put("bb", VALUE); map.put("啊哈", VALUE); stringRedisTemplate.opsForValue().multiSet(map); } /** * 批量添加key,只有当map中的key都不存在时 */ @Test public void test8() { Map map = new HashMap(); map.put("aa1", "aa1"); map.put("bb1", VALUE); map.put("啊哈1", VALUE); stringRedisTemplate.opsForValue().multiSetIfAbsent(map); } /** * 对一个 key-value 的值进行加减操作, * 如果该 key 不存在 将创建一个key 并赋值该 number * 如果 key 存在,但 value 不是长整型 ,将报错 */ @Test public void test9() { //每次加1 stringRedisTemplate.opsForValue().increment(BB); //每次加10 stringRedisTemplate.opsForValue().increment(AA, 10); stringRedisTemplate.opsForValue().decrement(AA, 1); } } ####zset#################################################### 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.core.StringRedisTemplate; import java.util.Set; /** * 1)有序的点赞,时间戳作为分值 *

* 2) 排行榜 */ @SpringBootTest public class ZsetTest { @Autowired private StringRedisTemplate stringRedisTemplate; /** * 添加元素 */ @Test public void test1() { stringRedisTemplate.opsForZSet().add("AA-zset", "aa", 9); stringRedisTemplate.opsForZSet().add("AA-zset", "bb", 4); stringRedisTemplate.opsForZSet().add("AA-zset", "cc", -3); stringRedisTemplate.opsForZSet().add("AA-zset", "dd", 28); stringRedisTemplate.opsForZSet().add("AA-zset", "ee", 5); } /** * 返回元素的个数 */ @Test public void test2() { Long size = stringRedisTemplate.opsForZSet().size("AA-zset"); System.out.println("元素的个数为】" + size); } /** * 升序,降序返回键值 * 升序】[cc, bb, ee, aa, dd] * 降序】[dd, aa, ee, bb, cc] */ @Test public void test3() { Set range = stringRedisTemplate.opsForZSet().range("AA-zset", 0, -1); System.out.println("升序】" + range.toString()); Set strings = stringRedisTemplate.opsForZSet().reverseRange("AA-zset", 0, -1); System.out.println("降序】" + strings.toString()); } /** * 按照分数查询一个范围内的元素,返回默认升序 * 按照分数范围查询】[bb, ee, aa] */ @Test public void test4() { Set range = stringRedisTemplate.opsForZSet().rangeByScore("AA-zset", 0, 10); System.out.println("按照分数范围查询】" + range.toString()); } /** * 返回排名 * 排名是】3 * 倒叙排名是】1 */ @Test public void test5() { Long aa = stringRedisTemplate.opsForZSet().rank("AA-zset", "aa"); System.out.println("排名是】" + aa); Long aa1 = stringRedisTemplate.opsForZSet().reverseRank("AA-zset", "aa"); System.out.println("倒叙排名是】" + aa1); } /** * 显示一个元素的分数 */ @Test public void test6() { Double aa = stringRedisTemplate.opsForZSet().score("AA-zset", "aa"); System.out.println("分数是】" + aa); } /** * 移除某一个元素 */ @Test public void test7() { stringRedisTemplate.opsForZSet().remove("AA-zset", "bb"); } /** * 给一个特定的元素加分,不存在就会创建 */ @Test public void test8() { stringRedisTemplate.opsForZSet().incrementScore("AA-zset", "bb", 1); } }

你可能感兴趣的:(redis集成springboot操作(list,hash,set,zset,string))