✨✨个人主页:沫洺的主页
📚📚系列专栏: 📖 JavaWeb专栏📖 JavaSE专栏 📖 Java基础专栏📖vue3专栏
📖MyBatis专栏📖Spring专栏📖SpringMVC专栏📖SpringBoot专栏
📖Docker专栏📖Reids专栏📖MQ专栏📖SpringCloud专栏
💖💖如果文章对你有所帮助请留下三连✨✨
Redis 有 5 种基础数据结构:
string (字符串) 、 list (列表) 、 hash (字典) 、 set (集合) 和 zset (有序集合)
@SpringBootTest class AppTests_String {@Autowiredprivate StringRedisTemplate stringRedisTemplate;private final String key = "A";@Testpublic void test1() {//设KEY为A,value为100,30秒过期stringRedisTemplate.opsForValue().set(key, "100", 30, TimeUnit.SECONDS);//获取A的值String value = stringRedisTemplate.opsForValue().get(key);System.out.println(value);//拼接stringRedisTemplate.opsForValue().set("B","aaa");stringRedisTemplate.opsForValue().append("B", "--bbb");System.out.println(stringRedisTemplate.opsForValue().get("B"));//获取后删除String b = stringRedisTemplate.opsForValue().getAndDelete("B");System.out.println(b);//加指定值,默认1Long c = stringRedisTemplate.opsForValue().increment("C");System.out.println(c);Long c1 = stringRedisTemplate.opsForValue().increment("C", 9);System.out.println(c1);//setnx锁Boolean d1 = stringRedisTemplate.opsForValue().setIfAbsent("D", "1");System.out.println(d1);Boolean d2 = stringRedisTemplate.opsForValue().setIfAbsent("D", "1");System.out.println(d2);//设置声明周期String d3 = stringRedisTemplate.opsForValue().getAndExpire("D", 60, TimeUnit.SECONDS);System.out.println(d1);//批量设置Map
map = new HashMap<>();map.put("F1","1");map.put("F2","2");map.put("F3","3");stringRedisTemplate.opsForValue().multiSet(map);//批量获取List keys = new ArrayList<>();keys.add("F1");keys.add("F2");keys.add("F3");List values = stringRedisTemplate.opsForValue().multiGet(keys);System.out.println(values);} }
@SpringBootTest class AppTests_List {@Autowiredprivate StringRedisTemplate stringRedisTemplate;private static final String name = "wangwu";@Testvoid test1() {//左推stringRedisTemplate.opsForList().leftPush(name, "aaa");//批量左推Long aLong = stringRedisTemplate.opsForList().leftPushAll(name, "aaa", "bbb", "ccc");System.out.println(aLong);//左弹String v = stringRedisTemplate.opsForList().leftPop(name);System.out.println(v);//右弹,限时30秒后结束监听String v1 = stringRedisTemplate.opsForList().rightPop(name, 30, TimeUnit.MINUTES);System.out.println("--------------v1------------");System.out.println(v1);//一直监听,有值就弹出//while (true) {// String v2 = stringRedisTemplate.opsForList().rightPop(name, 10, TimeUnit.SECONDS);// if (v2 != null) {// System.out.println("------------获取到数据------------");// System.out.println(v2);// } else {// System.out.println("-----------没有拉取到新数据,继续监听----------");// }//}//长度Long size = stringRedisTemplate.opsForList().size(name);System.out.println(size);} }
@SpringBootTest class AppTests_Set {@Autowiredprivate StringRedisTemplate stringRedisTemplate;private static final String name1 = "zhangsan";private static final String name2 = "lisi";@Testvoid test1() {//添加stringRedisTemplate.opsForSet().add(name1, "张三", "李四", "王五");stringRedisTemplate.opsForSet().add(name2, "张三丰", "李四", "王五");//交集Set
intersect = stringRedisTemplate.opsForSet().intersect(name1, name2);//stringRedisTemplate.opsForSet().intersectAndStore()System.out.println(intersect);//并集Set union = stringRedisTemplate.opsForSet().union(name1, name2);//stringRedisTemplate.opsForSet().unionAndStore()System.out.println(union);//差集Set difference = stringRedisTemplate.opsForSet().difference(name1, name2);//stringRedisTemplate.opsForSet().differenceAndStore()System.out.println(difference);//是否存在Boolean a = stringRedisTemplate.opsForSet().isMember(name1, "张三");Boolean b = stringRedisTemplate.opsForSet().isMember(name1, "张三丰");System.out.println(a);System.out.println(b);} }
@SpringBootTest class AppTests_Zset {@Autowiredprivate StringRedisTemplate stringRedisTemplate;private static final String name1 = "zhangsan";@Testvoid test1() {stringRedisTemplate.opsForZSet().add(name1, "语文", 110);stringRedisTemplate.opsForZSet().add(name1, "数学", 130);stringRedisTemplate.opsForZSet().add(name1, "英语", 90);//数量Long size = stringRedisTemplate.opsForZSet().size(name1);System.out.println(size);//范围数量Long count = stringRedisTemplate.opsForZSet().count(name1, 100, 150);System.out.println(count);//弹出最值//ZSetOperations.TypedTuple
max = stringRedisTemplate.opsForZSet().popMax(name1);//ZSetOperations.TypedTuple min = stringRedisTemplate.opsForZSet().popMin(name1);//System.out.println(max+"---"+min);//范围内的值,根据分数升序Set strings = stringRedisTemplate.opsForZSet().rangeByScore(name1, 0, 150);System.out.println(strings);//排名Long rank = stringRedisTemplate.opsForZSet().rank(name1, "数学");System.out.println(rank);//倒叙Set strings1 = stringRedisTemplate.opsForZSet().reverseRangeByScore(name1, 0, 150);System.out.println(strings1);} }
@SpringBootTest class AppTests_Hash {@Autowiredprivate StringRedisTemplate stringRedisTemplate;private static final String name = "zhangsan";private static final String name2 = "lisi";@Testvoid test1() {//放stringRedisTemplate.opsForHash().put(name, "20221107", "洛阳");stringRedisTemplate.opsForHash().put(name, "20221108", "新乡");stringRedisTemplate.opsForHash().put(name, "20221109", "郑州");//取Object o = stringRedisTemplate.opsForHash().get(name, "20221108");System.out.println(o);//所有valueList
@SpringBootTest class AppTests_BitMap {@Autowiredprivate StringRedisTemplate stringRedisTemplate;private final String name1 = "张三";@Testpublic void test1() {stringRedisTemplate.opsForValue().setBit(name1, 1, true);stringRedisTemplate.opsForValue().setBit(name1, 100, true);stringRedisTemplate.opsForValue().setBit(name1, 365, true);Boolean bit = stringRedisTemplate.opsForValue().getBit(name1, 1);Boolean bit1 = stringRedisTemplate.opsForValue().getBit(name1, 2);System.out.println(bit + "---" + bit1);//原生统计范围内1/true的个数RedisCallback
callback = (connection) ->connection.bitCount(name1.getBytes(StandardCharsets.UTF_8), 0, 366);Long aLong = stringRedisTemplate.execute(callback);System.out.println(aLong);//stringRedisTemplate.opsForValue().getOperations().execute(callback);} }