[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,背后其实有个强大的会员管理系统在默默支持呢?没错,就是那...
安卓系统软件使用技巧,解锁软件... 你有没有发现,用安卓手机的时候,总有一些小技巧能让你玩得更溜?别小看了这些小细节,它们可是能让你的手...
安卓系统提示音替换 你知道吗?手机里那个时不时响起的提示音,有时候真的能让人心情大好,有时候又让人抓狂不已。今天,就让我...
安卓开机不了系统更新 手机突然开不了机,系统更新还卡在那里,这可真是让人头疼的问题啊!你是不是也遇到了这种情况?别急,今天...
安卓系统中微信视频,安卓系统下... 你有没有发现,现在用手机聊天,视频通话简直成了标配!尤其是咱们安卓系统的小伙伴们,微信视频功能更是用...
安卓系统是服务器,服务器端的智... 你知道吗?在科技的世界里,安卓系统可是个超级明星呢!它不仅仅是个手机操作系统,竟然还能成为服务器的得...
pc电脑安卓系统下载软件,轻松... 你有没有想过,你的PC电脑上安装了安卓系统,是不是瞬间觉得世界都大不一样了呢?没错,就是那种“一机在...
电影院购票系统安卓,便捷观影新... 你有没有想过,在繁忙的生活中,一部好电影就像是一剂强心针,能瞬间让你放松心情?而我今天要和你分享的,...
安卓系统可以写程序? 你有没有想过,安卓系统竟然也能写程序呢?没错,你没听错!这个我们日常使用的智能手机操作系统,竟然有着...
安卓系统架构书籍推荐,权威书籍... 你有没有想过,想要深入了解安卓系统架构,却不知道从何下手?别急,今天我就要给你推荐几本超级实用的书籍...
安卓系统看到的炸弹,技术解析与... 安卓系统看到的炸弹——揭秘手机中的隐形威胁在数字化时代,智能手机已经成为我们生活中不可或缺的一部分。...
鸿蒙系统有安卓文件,畅享多平台... 你知道吗?最近在科技圈里,有个大新闻可是闹得沸沸扬扬的,那就是鸿蒙系统竟然有了安卓文件!是不是觉得有...
宝马安卓车机系统切换,驾驭未来... 你有没有发现,现在的汽车越来越智能了?尤其是那些豪华品牌,比如宝马,它们的内饰里那个大屏幕,简直就像...
p30退回安卓系统 你有没有听说最近P30的用户们都在忙活一件大事?没错,就是他们的手机要退回安卓系统啦!这可不是一个简...
oppoa57安卓原生系统,原... 你有没有发现,最近OPPO A57这款手机在安卓原生系统上的表现真是让人眼前一亮呢?今天,就让我带你...
安卓系统输入法联想,安卓系统输... 你有没有发现,手机上的输入法真的是个神奇的小助手呢?尤其是安卓系统的输入法,简直就是智能生活的点睛之...
怎么进入安卓刷机系统,安卓刷机... 亲爱的手机控们,你是否曾对安卓手机的刷机系统充满好奇?想要解锁手机潜能,体验全新的系统魅力?别急,今...
安卓系统程序有病毒 你知道吗?在这个数字化时代,手机已经成了我们生活中不可或缺的好伙伴。但是,你知道吗?即使是安卓系统,...
奥迪中控安卓系统下载,畅享智能... 你有没有发现,现在汽车的中控系统越来越智能了?尤其是奥迪这种豪华品牌,他们的中控系统简直就是科技与艺...