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使用示例的更多相关文章

  1. redis 配置文件示例

    # redis 配置文件示例 # 当你需要为某个配置项指定内存大小的时候,必须要带上单位,# 通常的格式就是 1k 5gb 4m 等酱紫:## 1k  => 1000 bytes# 1kb =& ...

  2. redis pipelined 示例

    redis 常用的数据类型 有序集合(sort set).无序集合 (set),hashMap redis pipelined 示例 List<Object> list = jedisTe ...

  3. spring data redis使用示例

    1. 配置依赖文件 <dependencies> <dependency> <groupId>org.springframework.data</groupI ...

  4. 阿里云PHP Redis代码示例

    测试代码示例 <?php /* 这里替换为连接的实例host和port */ $host = "localhost"; $port = 6379; /* 这里替换为实例id和 ...

  5. redis -- python操作连接redis简单示例

    1.先安装 redis,pyredis sudo pip install redis sudo pip install python-redis 2.示例: importredis >>& ...

  6. 使用Linux自带日志滚动工具logrotate滚动redis日志示例

    截至到redis-5.0版本,redis仍然不会自动滚动日志文件,如果不处理则日志文件日积月累越来越大,最终将导致磁盘满告警: # ls -lh total 12G -rw-r--r-- 1 redi ...

  7. Spring Data Redis入门示例:基于RedisTemplate (三)

    使用底层API:RedisConnection操作Redis,需要对数据进行手动转换(String <---->byte),需要进行多数重复性工作,效率低下:org.springframe ...

  8. Spring Data Redis入门示例:数据序列化 (四)

    概述 RedisTemplate默认使用的是基于JDK的序列化器,所以存储在Redis的数据如果不经过相应的反序列化,看到的结果是这个样子的: 可以看到,出现了乱码,在程序层面上,不会影响程序的运行, ...

  9. Spring Data Redis入门示例:基于Jedis及底层API (二)

    使用底层API:RedisConnectionFactory和RedisConnection可以直接操作Redis,下面是一个简单的例子: ### Maven依赖 <properties> ...

  10. Redis代码示例

    RedisTemplate 如果想要在java中使用Redis相关的数据结构,要先注入RedisTemplate. @Autowired private RedisTemplate<K,V> ...

随机推荐

  1. React Navite环境搭建

    俗话说"工欲善其事,必先利其器."所以,我们第一步就是搭建React Native开发坏境. 一.安装Node.js.npm.yarn 1.1 React native需要借助no ...

  2. vue实现全部防抖

    // 全局注册防抖 Vue.component("ElButton").mixin({   data() {     return {       debounce: false ...

  3. idea-lombok使用

    一.添加 lombok 插件 file -- setting  -- plugins - 输入 Lombok 搜索,进行安装 二.引入依赖 <dependency> <groupId ...

  4. iPhone添加节假日日历地址

    添加苹果节假日日历地址 手动订阅节假日: 打开"设置">"日历">"帐户">"添加帐户">&qu ...

  5. spark2.0.1源码编译

    一.编译源码步骤演示详解 . 编译spark环境要求 1.必须在linux系统下编译(以centos6.4为例) 2.编译使用的JDK版本必须是1.6以上(以JDK1.8为例) 3.编译需要使用Mav ...

  6. Callback/Callable类型

    自PHP5.4起可用callable类型指定回调类型callback. 一些函数如call_user_func()或usort()可以接受用户自定义的回调函数作为参数.回调函数不止可以是简单函数,还可 ...

  7. winform 中 label透明化

    label1.BackColor = Color.Transparent;//设置背景颜色为透明 label1.Parent = pictureBox1;//将pictureBox1设为标签的父控件, ...

  8. 第九章:用Python处理省份城市编码数据

    文章目录 项目背景 项目代码 新增省份编码 获取编码映射数据 合并数据 统计省份用户数 使用SQL实现 源码地址 本文可以学习到以下内容: 免费获取全国省份.城市编码以及经纬度数 使用 pandas ...

  9. Java基础学习:9、访问修饰符

    Java提供四种访问控制修饰符,用于控制方法和属性(成员变量)的访问权限(范围). 注意事项: 1.修饰符可以访问类中的属性.成员方法.以及类. 2.只用默认修饰符.public可以修饰类. 3.成员 ...

  10. 搭建Redis高可用集群的哨兵模式(Redis-Sentinel)【Windows环境】

    参考 https://blog.csdn.net/itanping/article/details/100544152 哨兵模式搭建好,Java中配置和使用Redis高可用集群的哨兵模式,引入Jedi ...