[Redis] Spring Boot 使用Redis---StringRedisTemplate
admin
2024-02-01 23:43:12
0

✨✨个人主页:沫洺的主页

📚📚系列专栏: 📖 JavaWeb专栏📖 JavaSE专栏 📖 Java基础专栏📖vue3专栏 

                           📖MyBatis专栏📖Spring专栏📖SpringMVC专栏📖SpringBoot专栏

                           📖Docker专栏📖Reids专栏📖MQ专栏📖SpringCloud专栏     

💖💖如果文章对你有所帮助请留下三连✨✨

✨Redis结构体

Redis 有 5 种基础数据结构: 

string (字符串) 、 list (列表) 、 hash (字典) 、 set (集合) 和 zset (有序集合) 

🎊Spring Boot 使用Redis

🎉String(字符串)

@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);}
}

🎉List(列表)

@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);}
}

🎉Set(集合)

@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);}
}

🎉ZSet(有序集合)

@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);}
}

🎉Hash(字典)

@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 values = stringRedisTemplate.opsForHash().values(name);System.out.println(values);//所有hashKeySet keys = stringRedisTemplate.opsForHash().keys(name);System.out.println(keys);//所有hashKey和valueMap entries = stringRedisTemplate.opsForHash().entries(name);System.out.println(entries);//是否存在指定hashKeyBoolean b = stringRedisTemplate.opsForHash().hasKey(name, "20221108-");System.out.println(b);// stringRedisTemplate.opsForHash().put(name,"20221109","开封");//如果没有.则放Boolean b1 = stringRedisTemplate.opsForHash().putIfAbsent(name, "20221110", "南阳");System.out.println(b1);//删除// stringRedisTemplate.opsForHash().delete(name,"20221110");//批量获取List objects = stringRedisTemplate.opsForHash().multiGet(name, CollUtil.newArrayList("20221107", "20221109"));System.out.println(objects);//批量放Map map = new HashMap<>();map.put("20221107", "驻马店");map.put("20221108", "信阳");map.put("20221109", "平顶山");stringRedisTemplate.opsForHash().putAll(name2, map);//加减Long qty = stringRedisTemplate.opsForHash().increment("cateory.1", "product.108", 5);//Long qty = stringRedisTemplate.opsForHash().increment("cateory.1", "product.108", 100);//Long qty = stringRedisTemplate.opsForHash().increment("cateory.1", "product.108", -8);System.out.println(qty);//随机获取List keys = stringRedisTemplate.opsForHash().randomKeys(name2, 2);System.out.println(keys);}
}
 
 

🎉BitMap(String)

@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);}
}

相关内容

热门资讯

智慧吴江app安卓系统,安卓系... 你知道吗?最近吴江地区掀起了一股智慧风潮,一款名为“智慧吴江app”的应用在安卓系统上大受欢迎。这款...
苹果系统听歌app安卓,跨平台... 你有没有发现,无论是走在街头还是坐在家里,音乐总是能瞬间点燃我们的心情?而在这个音乐无处不在的时代,...
安卓系统卡顿根源,性能瓶颈与优... 手机用久了是不是感觉越来越卡?是不是每次打开应用都要等半天,甚至有时候直接卡死?别急,今天就来跟你聊...
电脑系统怎么装安卓系统,电脑系... 你有没有想过,把安卓系统装在你的电脑上,是不是就像给电脑穿上了时尚的新衣呢?想象你可以在电脑上直接使...
安卓系统华为手环app,健康管... 你有没有发现,现在的生活越来越离不开智能设备了?手机、平板、手表……这些小玩意儿不仅让我们的生活变得...
switch lite刷安卓系... 你有没有想过,你的Switch Lite除了玩那些可爱的任天堂游戏,还能干些什么呢?没错,今天我要给...
想买华为但是安卓系统,尽享安卓... 最近是不是也被华为的新款手机给迷住了?看着那流畅的线条和强大的性能,是不是心动了呢?但是,一想到安卓...
怎么拷安卓系统文件,安卓系统文... 你有没有想过,手机里的那些安卓系统文件,其实就像是一扇通往手机世界的秘密通道呢?想要深入了解你的安卓...
安卓系统移植按键失灵,安卓系统... 最近你的安卓手机是不是也遇到了按键失灵的尴尬情况呢?这可真是让人头疼啊!别急,今天就来给你详细解析一...
安卓系统更新管理在哪,全面解析... 你有没有发现,你的安卓手机最近是不是总在提醒你更新系统呢?别急,别急,今天就来手把手教你,安卓系统更...
安卓系统哪里出的,从诞生地到全... 你有没有想过,我们每天离不开的安卓系统,它究竟是从哪里冒出来的呢?是不是觉得这个问题有点儿像是在问星...
最好的电脑安卓系统,最佳电脑安... 亲爱的电脑迷们,你是否在寻找一款既能满足你工作需求,又能让你畅享娱乐的电脑操作系统呢?今天,我要给你...
安卓系统保密性,守护隐私的坚实... 你知道吗?在这个信息爆炸的时代,保护个人隐私变得比以往任何时候都重要。尤其是对于安卓系统用户来说,了...
苹果系统下载安卓版本,安卓版本... 你有没有想过,为什么苹果系统的手机那么受欢迎,却还有人想要下载安卓版本呢?这背后可是有着不少故事呢!...
安卓系统如何下载carplay... 你是不是也和我一样,对安卓系统上的CarPlay功能充满了好奇?想象在安卓手机上就能享受到苹果Car...
退回安卓系统的理由,揭秘安卓系... 你有没有想过,为什么有些人会选择退回到安卓系统呢?这可不是一件简单的事情,背后可是有着不少原因哦!让...
安卓机系统互通吗,共创智能生态 你有没有想过,你的安卓手机里的应用和电脑上的安卓应用是不是可以无缝对接呢?是不是有时候觉得手机上的某...
安卓源码 添加系统应用,系统应... 你有没有想过,手机里的那些系统应用是怎么来的?是不是觉得它们就像天外来物,神秘又神奇?其实,只要你愿...
安卓系统能否播放flv,全面解... 你有没有想过,你的安卓手机里那些珍贵的FLV视频文件,到底能不能顺利播放呢?这可是个让人挠头的问题,...
奔驰c系安卓系统,智能驾驶体验... 你有没有发现,最近开奔驰C系的小伙伴们都在悄悄地谈论一个新玩意儿——安卓系统!没错,就是那个我们手机...