redis使用示例
package com.atguigu.gulimall.product;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.junit.jupiter.api.Test;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@SpringBootTest
public class RedisTest {
@Autowired
StringRedisTemplate redisTemplate;
@Test
public void testRedis() {
ValueOperations<String, String> ops = redisTemplate.opsForValue();
//保存
ops.set("hello", "world" + UUID.randomUUID().toString());
//查询
String hello = ops.get("hello");
System.out.println("之前保存的数据是:" + hello);
}
@Test
public Map<String, List<Catelog2Vo>> getCatelogJson() {
String catelogJSON = redisTemplate.opsForValue().get("catelogJSON");
if(StringUtils.isBlank(catelogJSON)) {
//缓存中没有,查询数据库
Map<String, List<Catelog2Vo>> catelogJsonFromDb = getCatelogJsonFromDb();
//查到的数据再放入缓存,并将对象转为json类型
String s = JSON.toJSONString(catelogJsonFromDb);
redisTemplate.opsForValue().set("catelogJSON", s);
return catelogJsonFromDb;
}
//转换为我们指定的对象。反序列化:因为之前存入redis前序列换为json类型的数据了,现在取出来也要转为最初的对象类型数据。
Map<String, List<Catelog2Vo>> result = JSON.parseObject(catelogJSON, new TypeReference<Map<String, List<Catelog2Vo>>>() {});
return result;
}
private Map<String, List<Catelog2Vo>> getCatelogJsonFromDb() {
return null;
}
public class Catelog2Vo {
}
}
redis使用示例的更多相关文章
- redis 配置文件示例
# redis 配置文件示例 # 当你需要为某个配置项指定内存大小的时候,必须要带上单位,# 通常的格式就是 1k 5gb 4m 等酱紫:## 1k => 1000 bytes# 1kb =& ...
- redis pipelined 示例
redis 常用的数据类型 有序集合(sort set).无序集合 (set),hashMap redis pipelined 示例 List<Object> list = jedisTe ...
- spring data redis使用示例
1. 配置依赖文件 <dependencies> <dependency> <groupId>org.springframework.data</groupI ...
- 阿里云PHP Redis代码示例
测试代码示例 <?php /* 这里替换为连接的实例host和port */ $host = "localhost"; $port = 6379; /* 这里替换为实例id和 ...
- redis -- python操作连接redis简单示例
1.先安装 redis,pyredis sudo pip install redis sudo pip install python-redis 2.示例: importredis >>& ...
- 使用Linux自带日志滚动工具logrotate滚动redis日志示例
截至到redis-5.0版本,redis仍然不会自动滚动日志文件,如果不处理则日志文件日积月累越来越大,最终将导致磁盘满告警: # ls -lh total 12G -rw-r--r-- 1 redi ...
- Spring Data Redis入门示例:基于RedisTemplate (三)
使用底层API:RedisConnection操作Redis,需要对数据进行手动转换(String <---->byte),需要进行多数重复性工作,效率低下:org.springframe ...
- Spring Data Redis入门示例:数据序列化 (四)
概述 RedisTemplate默认使用的是基于JDK的序列化器,所以存储在Redis的数据如果不经过相应的反序列化,看到的结果是这个样子的: 可以看到,出现了乱码,在程序层面上,不会影响程序的运行, ...
- Spring Data Redis入门示例:基于Jedis及底层API (二)
使用底层API:RedisConnectionFactory和RedisConnection可以直接操作Redis,下面是一个简单的例子: ### Maven依赖 <properties> ...
- Redis代码示例
RedisTemplate 如果想要在java中使用Redis相关的数据结构,要先注入RedisTemplate. @Autowired private RedisTemplate<K,V> ...
随机推荐
- MySQL Galera cluster 集群常用参数说明
1.wsrep_local_state_uuid: 与集群的wsrep_cluster_state_uuid一致 root@[(none)]> show status like '%wsrep_ ...
- Qt使用图片旋转绘制圆环,圆弧类UI界面(方法可适用于winform和WPF等其余UI绘制)
先上效果图: 这个主要是一个试剂杯盘的循环图,相信大家伙一定都想到了使用GDI和三角函数来进行计算,但如果没有实际尝试,可能会踩一些坑,留此文帮助大家方便快捷的绘制出类似圆环的UI效果. 这里主要是使 ...
- 配置python库源为清华源
目录 Windows Ubuntu pip较低版本 pip较高版本 Windows %HOMEPATH%/pip/pip.ini [global] index-url = https://pypi.t ...
- Workbench download Document
1. package.xml <?xml version="1.0" encoding="UTF-8"?> <Package xmlns=&q ...
- 微信小程序 table表格 固定表头和首列 右侧表格可以左右滚动(多种表格演练)
https://blog.csdn.net/m0_61073617/article/details/124430213
- windows elasticsearch中文乱码
一:找到安装目录下的config jvm.options文件 二:修改或增加: -Dfile.encoding=GBK 参考文章地址:https://www.shuzhiduo.com/A/rV57O ...
- T137288 铸星
有以下三种情况: 只有一个数,直接输出. 有正数也有复数,那就先让负数撞正数得到更小的负数,直到剩下最后一个正数撞所有的负数,答案是所有数的绝对值之和. 仅有正数或者仅有负数,就先找一对不相等的相邻的 ...
- Mac连接交换机
接上免驱动的绿联,可以看到有一个usb串口设备: ls /dev/tty.* /dev/tty.Bluetooth-Incoming-Port /dev/tty.usbserial-AB0LU5PY ...
- 【java数据结构与算法】选择排序
选择排序原理剖析: 假设数组arr使用选择排序 每一轮选出数组最小的元素 arr.lenth个元素,只需要找出arr.length-1个元素的正确位置 选择排序便进行结束 外层for循环控制选择排序的 ...
- vue 中的 provide/inject
provide/inject 是 vue 2.2.0 版本新增 类型: provide:Object | () => Object inject:Array<string> | { ...