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

相关内容

热门资讯

制作安卓系统主题软件,安卓系统... 你有没有想过,给你的安卓手机换一个全新的面貌?没错,就是那种一打开手机,就能感受到完全不同的风格和氛...
安卓系统平板怎么截屏,操作指南... 亲爱的平板用户,你是不是也和我一样,有时候想记录下平板上的精彩瞬间,却发现截屏功能有点神秘?别担心,...
安卓系统不推送更新,揭秘背后的... 最近是不是发现你的安卓手机有点儿“懒”啊?更新推送总是慢吞吞的,让人等得花儿都谢了。别急,今天就来给...
ape格式转换安卓系统,享受音... 你有没有想过,你的安卓手机里的ape格式音乐文件,竟然可以通过一个小小的转换,焕发出全新的生命力?没...
获取安卓系统加载器,核心功能与... 你有没有想过,你的安卓手机里那些神奇的软件和游戏是怎么被安装到你的设备上的呢?没错,就是通过一个叫做...
安卓系统文件夹在哪,安卓系统文... 你有没有遇到过这样的情况:手机里乱糟糟的,想找个文件却找不到?别急,今天就来给你揭秘安卓系统文件夹的...
安卓手感最好的裸机系统,安卓手... 安卓手感最好的裸机系统:探索极致体验的秘密武器在数字世界中,我们常常被各种功能和复杂操作所包围,尤其...
nas如何刷回安卓系统,轻松刷... 你有没有想过,你的NAS(网络附加存储)突然间变成了一个安卓的小天地?别急,这可不是什么天方夜谭,而...
荣耀沿用的安卓系统吗,打造个性... 你有没有注意到,最近荣耀的新机发布,大家都在热议一个问题:荣耀沿用的安卓系统吗?这可是个让人好奇不已...
快麦erp系统安卓下载,一键下... 你有没有听说最近一款叫做快麦ERP系统的软件在安卓平台上大受欢迎呢?没错,就是那个能让你企业管理如虎...
华为安卓系统下载app,一步到... 你有没有发现,最近华为手机的用户们都在忙活一件大事儿?没错,那就是下载安卓系统上的各种app啦!这可...
原生安卓系统游戏模式,畅享沉浸... 亲爱的手机游戏爱好者们,你是否曾为手机游戏运行不畅而烦恼?又或者,你是否渴望在游戏中获得更极致的体验...
安卓9改系统语言设置,轻松切换... 你有没有发现,手机里的语言设置有时候真的让人头疼?比如说,你突然想用一下安卓9的系统语言设置,结果发...
怎么升级安卓最新系统,畅享安卓... 亲爱的手机控们,你是不是也和我一样,对安卓系统的更新充满了期待?每次系统升级,都仿佛给我们的手机带来...
安卓系统电视跳舞毯,家庭娱乐新... 你有没有想过,家里的电视除了用来追剧、看电影,还能变成一个充满活力的娱乐中心?没错,我要给你介绍的就...
安卓系统维护周期,全方位守护您... 亲爱的手机控们,你是不是也和我一样,对安卓系统的维护周期充满了好奇呢?毕竟,我们的手机可是我们日常生...
安卓系统电脑怎么往下滑,一扫即... 你有没有发现,用安卓系统电脑的时候,有时候屏幕上会出现一些小图标或者应用,你想要快速浏览或者切换,却...
手机中判断安卓系统苹果系统js... 你有没有想过,你的手机里到底装的是安卓系统还是苹果系统呢?这可不是一个小问题哦,因为不同的系统,就像...
window系统和安卓系统还原... 你有没有遇到过手机或电脑突然卡顿,或者不小心删掉了重要的文件?别急,今天就来给你详细说说如何让win...
安卓系统打电话变声器,轻松实现... 安卓系统打电话变声器:探索数字时代的通信革新在数字化浪潮中,智能手机已经成为我们生活中不可或缺的一部...