当前位置: 首页 > news >正文

RedisTemplate使用

文章目录

  • RedisTemplate使用
    • String类型
    • Hash类型
    • List类型
    • Set类型
    • Zset类型

RedisTemplate使用

String类型

    @Overridepublic void testString() {// t11();String key = "k1";String currentNum;// 用法1:key是否存在Boolean value = client.hasKey(key);log.info("[{}]是否存在[{}]", key, value);// 用法2:添加元素client.opsForValue().set(key, "v1", DateConstant.TIME_OF_DAY, TimeUnit.SECONDS);// 用法3:获取元素String getValue = client.opsForValue().get(key);log.info("getValue : [{}]", getValue);// 用法4:计数String counter = "counter:key";client.opsForValue().set(counter, "0", DateConstant.TIME_OF_DAY, TimeUnit.SECONDS);client.opsForValue().increment(counter);client.opsForValue().increment(counter);currentNum = client.opsForValue().get(counter);log.info("currentNum : [{}]", currentNum);client.opsForValue().decrement(counter);currentNum = client.opsForValue().get(counter);log.info("currentNum : [{}]", currentNum);// 用法5:存储list<map>结构数据List<Map<String, String>> multiMapList = Lists.newArrayList();for (int i = 0; i < 5; i++) {LinkedHashMap<String, String> itemMap = Maps.newLinkedHashMap();itemMap.put("name", "jack" + i);if (i % 2 == 0) {itemMap.put("age", String.valueOf(10 + i));itemMap.put("sex", "男");} else {itemMap.put("age", String.valueOf(11 + i));itemMap.put("sex", "女");}multiMapList.add(itemMap);}String multiMapStr = JSON.toJSONString(multiMapList);client.opsForValue().set("str:multiusers", multiMapStr, DateConstant.TIME_OF_DAY, TimeUnit.SECONDS);String userList = client.opsForValue().get("str:multiusers");List<Map<String, String>> maps = CastBeanUtil.castListMap(JSON.parse(userList), String.class, String.class);log.info("maps : [{}]", maps);// 用法6:存储list<entity>List<TzArea> areaList = Lists.newArrayList();for (int i = 0; i < 10; i++) {TzArea item = new TzArea();item.setAreaId((long) i);item.setAreaName("江苏省");item.setLevel(1);item.setParentId(1L);areaList.add(item);}client.opsForValue().set("str:multiareas", JSON.toJSONString(areaList), DateConstant.TIME_OF_DAY, TimeUnit.SECONDS);String res = client.opsForValue().get("str:multiareas");List<TzArea> tzAreas = JSON.parseArray(res, TzArea.class);log.info("tzAreas : [{}]", JSON.toJSONString(tzAreas));}

Hash类型

@Overridepublic void testHash() {// 用法1:添加一个字段client.opsForHash().put("hash:user:single", "name", "pmb");client.expire("user", DateConstant.TIME_OF_DAY, TimeUnit.SECONDS);Map<Object, Object> result = client.opsForHash().entries("hash:user:single");log.info("result : [{}]", result);// 用法2:添加一个对象信息Map<Object, Object> handleMap = Maps.newLinkedHashMap();handleMap.put("name", "jack");handleMap.put("age", "18");handleMap.put("sex", "男");String key = "hash:user:all";client.opsForHash().putAll(key, handleMap);Map<Object, Object> allElements = client.opsForHash().entries(key);log.info("allElements : [{}]", allElements);// 用法3:只获取map中key集合Set<Object> keyList = client.opsForHash().keys(key);log.info("keyList : [{}]", keyList);// 用法4:只获取map中value集合List<Object> valueList = client.opsForHash().values(key);log.info("valueList : [{}]", valueList);}

List类型

@Overridepublic void testList() {// 用法1:顺序添 加元素1,2,3,4// rightPush 列表右侧添加元素String key = "list:phoneList";client.opsForList().rightPush(key, "16607024161");client.opsForList().rightPush(key, "16607024162");client.opsForList().rightPush(key, "16607024163");client.expire(key, DateConstant.TIME_OF_DAY, TimeUnit.SECONDS);// 删除元素client.opsForList().rightPop(key);// 元素个数Long size = client.opsForList().size(key);assert size !=null;log.info(SIZE_FORMAT, size);// 查找元素// index 查找指定下标的元素 下标从0开始,最后一个size-1String firstItem = client.opsForList().index(key, 0);log.info("firstItem : [{}]", firstItem);String secondItem = client.opsForList().index(key, 1);log.info("secondItem : [{}]", secondItem);String thirdItem = client.opsForList().index(key, 2);log.info("thirdItem : [{}]", thirdItem);// list中所有元素List<String> res = client.opsForList().range(key, 0, size - 1);log.info("res : [{}]", res);// 修改制定位置数据client.opsForList().set(key, 0, "12");// 实现栈 先进后出client.opsForList().leftPush(key, "1");client.opsForList().leftPush(key, "2");client.opsForList().leftPush(key, "3");client.opsForList().leftPop(key);client.opsForList().leftPop(key);client.opsForList().leftPop(key);// 实现队列 先进先出client.opsForList().leftPush(key, "one");client.opsForList().leftPush(key, "two");client.opsForList().rightPop(key);client.opsForList().rightPop(key);}

Set类型

   @Overridepublic void testSet() {String key = "set:nums";// 用法1:添加元素client.opsForSet().add(key, "1", "2", "3");client.expire(key, DateConstant.TIME_OF_DAY, TimeUnit.SECONDS);//用法2:获取集合的元素Set<String> members = client.opsForSet().members(key);log.info("members : [{}]", members);// 用法3:判断某个元素是否存在Boolean member = client.opsForSet().isMember(key, "2");log.info("member : [{}]", member);String intersection = "set:nums:intersection";// 用法4:交集client.opsForSet().add(intersection, "1", "2");client.expire(intersection, DateConstant.TIME_OF_DAY, TimeUnit.SECONDS);Set<String> intersectList = client.opsForSet().intersect(key, intersection);log.info("intersectList : [{}]", intersectList);// 用法5:并集Set<String> unionList = client.opsForSet().union(key, intersection);log.info("unionList : [{}]", unionList);// 用法6:查集Set<String> differenceList = client.opsForSet().difference(key, intersection);log.info("differenceList : [{}]", differenceList);}

Zset类型

  @Overridepublic void testZset() {String key = "zset:nums";// 用法1:添加元素client.opsForZSet().add(key, "one", 1);client.opsForZSet().add(key, "three", 30);client.opsForZSet().add(key, "two", 20);client.opsForZSet().add(key, "four", 44);client.opsForZSet().add(key, "five", 55);client.expire(key, DateConstant.TIME_OF_DAY, TimeUnit.SECONDS);// 用法2:返回指定元素排名Long one = client.opsForZSet().rank(key, "five");log.info("rank : [{}]", one);Long size = client.opsForZSet().size(key);assert size !=null;log.info(SIZE_FORMAT, size);// 用法3:返回指定区间元素Set<String> range = client.opsForZSet().range(key, 0, size - 1);log.info("range : [{}]", range);// 用法4:指定分数区间用户Set<String> userList = client.opsForZSet().rangeByScore(key, 1, 60);log.info("userList : [{}]", userList);// 用法5:移除一个或者多个元素client.opsForZSet().remove(key, "two");log.info(SIZE_FORMAT, client.opsForZSet().size(key));// 用法6:计算指定分数之间用户个数Long count = client.opsForZSet().count(key, 1, 20);log.info("count : [{}]", count);Set<String> invertedOrder = client.opsForZSet().reverseRange(key, 0, -1);log.info("InvertedOrder : [{}]", invertedOrder);// 用法7:显示所有成员score以及对应用户Set<ZSetOperations.TypedTuple<String>> allLis = client.opsForZSet().rangeWithScores(key, 0, -1);assert allLis != null;for (ZSetOperations.TypedTuple<String> next : allLis) {String value = next.getValue();Double score = next.getScore();log.info("通过rangeWithScores(K key, long start, long end)方法获取RedisZSetCommands.Tuples的区间值:[{}],[{}]", value, score);}// 用法8:相同score返回处理,取值第一个String lexKey = "zset:lex";client.delete(lexKey);client.opsForZSet().add(lexKey, "zs", 55);client.opsForZSet().add(lexKey, "ls", 55);client.opsForZSet().add(lexKey, "ww", 54);client.opsForZSet().add(lexKey, "zl", 55);client.expire(lexKey, DateConstant.TIME_OF_DAY, TimeUnit.SECONDS);// 取出score集合Set<ZSetOperations.TypedTuple<String>> typedTuples = client.opsForZSet().rangeWithScores(lexKey, 0, -1);if(Objects.isNull(typedTuples)){throw new IllegalArgumentException("出现异常了");}Map<String, Double> cachedMap = new ConcurrentHashMap<>(16);Iterator<ZSetOperations.TypedTuple<String>> scoreIterator = typedTuples.iterator();String firstUser = null;while (scoreIterator.hasNext()) {ZSetOperations.TypedTuple<String> item = scoreIterator.next();Double score = item.getScore();String value = item.getValue();if (cachedMap.containsValue(score)) {// 找到重复scorefirstUser = value;}cachedMap.put(value, score);}log.info("firstUser : [{}]", firstUser);}
http://www.lryc.cn/news/397462.html

相关文章:

  • 文献解读-多组学-第十八期|《整合 WES 和 RNA-Seq 数据以进行短变异发现》
  • 科学技术奖 | 畜禽粪污源头减排关键技术推广与种养循环一体化农业实践
  • 【漏洞复现】锐捷校园网自助服务系统 任意文件读取
  • Centos9安装部署及静态ip配置方案
  • 利用Altair One 云平台,轻松实现全球企业产品研发创新与优化
  • 数据库树状查询
  • 【实战场景】@Transactional中使用for update的注意点
  • 好用的声音分析的软件和网站
  • 开发情绪识别人工智能时的道德考量
  • MongoDB:基础语句及练习
  • 百度智能云创新业务部总经理李想:发挥AI企业科技创新优势 助力职业教育人才扬帆远航
  • 了解股票沽空及其风险
  • 【Sql Server修改列类型错误信息:对象名依赖于列】
  • 【ACM珠海分会,IEEE Fellow加盟,CPS出版】第四届管理科学和软件工程国际学术会议(ICMSSE 2024,7月19-21)
  • kmeans.fit_predict 和 kmeans.fit有什么区别
  • 香港优才计划续签难吗?一次性说清楚优才续签要求,不在香港居住也能续签成功!
  • react获取访问过的路由历史记录
  • 基于深度学习的点云降噪
  • 数据结构-二叉搜索树与红黑树
  • 52771-009P 同轴连接器
  • 鸿蒙语言基础类库:【@ohos.util.Vector (线性容器Vector)】
  • 使用Python绘制堆积面积图
  • 代码还原动态调试之 pstree 乘法变加法
  • C++:获取当前可执行核心数(开辟线程)
  • 【简历】吉林某985大学:JAVA实习简历指导,面试通过率相当低
  • C#中的MD5摘要算法与哈希算法
  • 使用 python 构建企业级高可用海量爬虫调度系统
  • IDEA常用技巧荟萃:精通开发利器的艺术
  • GD32F303之CAN通信
  • postgres 的dblink使用,远程连接数据库