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

相关内容

热门资讯

【MySQL】锁 锁 文章目录锁全局锁表级锁表锁元数据锁(MDL)意向锁AUTO-INC锁...
【内网安全】 隧道搭建穿透上线... 文章目录内网穿透-Ngrok-入门-上线1、服务端配置:2、客户端连接服务端ÿ...
GCN的几种模型复现笔记 引言 本篇笔记紧接上文,主要是上一篇看写了快2w字,再去接入代码感觉有点...
数据分页展示逻辑 import java.util.Arrays;import java.util.List;impo...
Redis为什么选择单线程?R... 目录专栏导读一、Redis版本迭代二、Redis4.0之前为什么一直采用单线程?三、R...
【已解决】ERROR: Cou... 正确指令: pip install pyyaml
关于测试,我发现了哪些新大陆 关于测试 平常也只是听说过一些关于测试的术语,但并没有使用过测试工具。偶然看到编程老师...
Lock 接口解读 前置知识点Synchronized synchronized 是 Java 中的关键字,...
Win7 专业版安装中文包、汉... 参考资料:http://www.metsky.com/archives/350.htm...
3 ROS1通讯编程提高(1) 3 ROS1通讯编程提高3.1 使用VS Code编译ROS13.1.1 VS Code的安装和配置...
大模型未来趋势 大模型是人工智能领域的重要发展趋势之一,未来有着广阔的应用前景和发展空间。以下是大模型未来的趋势和展...
python实战应用讲解-【n... 目录 如何在Python中计算残余的平方和 方法1:使用其Base公式 方法2:使用statsmod...
学习u-boot 需要了解的m... 一、常用函数 1. origin 函数 origin 函数的返回值就是变量来源。使用格式如下...
常用python爬虫库介绍与简... 通用 urllib -网络库(stdlib)。 requests -网络库。 grab – 网络库&...
药品批准文号查询|药融云-中国... 药品批文是国家食品药品监督管理局(NMPA)对药品的审评和批准的证明文件...
【2023-03-22】SRS... 【2023-03-22】SRS推流搭配FFmpeg实现目标检测 说明: 外侧测试使用SRS播放器测...
有限元三角形单元的等效节点力 文章目录前言一、重新复习一下有限元三角形单元的理论1、三角形单元的形函数(Nÿ...
初级算法-哈希表 主要记录算法和数据结构学习笔记,新的一年更上一层楼! 初级算法-哈希表...
进程间通信【Linux】 1. 进程间通信 1.1 什么是进程间通信 在 Linux 系统中,进程间通信...
【Docker】P3 Dock... Docker数据卷、宿主机与挂载数据卷的概念及作用挂载宿主机配置数据卷挂载操作示例一个容器挂载多个目...