首页 > 数据库 >redis使用示例

redis使用示例

时间:2022-12-29 16:36:54浏览次数:30  
标签:catelogJSON Map 示例 使用 redis org import redisTemplate

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 {
}

}

标签:catelogJSON,Map,示例,使用,redis,org,import,redisTemplate
From: https://www.cnblogs.com/sensenh/p/17012907.html

相关文章

  • CH9434-MCU代码移植,芯片使用详细说明(附Linux开发资料链接)
    简介CH9434是一款SPI转四串口转接芯片,提供四组全双工的9线异步串口,用于单片机/嵌入式/安卓系统扩展异步串口。提供25路GPIO,以及支持RS485收发控制引脚TNOW。本篇基于STM32......
  • Redis数据结构存储系统:第二章:如何使用
    Redis与SpringBoot整合:第一步:在项目中引入redis.clientsjedis第二步:将连接池和配置类创建好RedisUtil:importredis.clients.jedis.Jedis;importredis.clients.j......
  • docker compose安装与使用
    简介之前使用docker时,写完一个项目之后需要定义一个dockerfile,再通过dockerbuild生成一个镜像,并通过dockerrun执行;都是通过手动操作,用于单个容器,非常麻烦,现在想要的效......
  • REDM库使用教程01(详细入门)
    写这篇文章初衷在于,虽然开发这套框架的作者很叼,但是教程写的有点糟糕,坑了我一个晚上研究。REDM库的简单介绍​​https://gitee.com/hgy413/REDM​​ 然后要先装好VS2010;装......
  • C++ zip压缩库使用
    这个压缩库,主要是用来解压和压缩相关文件使用,好处就是引入比较方便,而且极其易使用,方便用户操作。首先是引入这四个文件,相关代码如下:首先是​​zip.h​​头文件#ifndef_zip_......
  • make_pair 简单使用
    code:#include<iostream>usingnamespacestd;intmain(){autopair_1=make_pair(1,"2");cout<<pair_1.first<<endl;cout<<pair_1.second<<endl;autopa......
  • Python使用pip自动升级所有第三方库
    大家复制底下的代码并且保存成py文件执行即可,在python3环境下,执行成功。importpipfrompip._internal.utils.miscimportget_installed_distributionsfromsubprocessimp......
  • [DT框架使用教程01]如何在DT框架中创建插件
    [DT框架使用教程01]如何在DT框架中创建插件DT框架代码地址:​​https://github.com/huifeng-kooboo/DT​​由于国内访问速度的问题也可以访问gitee的地址:​​https://git......
  • 锐安信动态信任签章使用教程
    当用户访问一个不熟悉的网站或是没有任何知名度的平台时,如何证明此网站的安全性和真实性,让用户安全放心使用网站成为企业发展业务的一个难题。此时,锐安信(sslTrus)SSL证书动......
  • .Net6 WebApi使用SqlSugar
    1、Nuget先引入:SqlSugarCore2、NetIOC1、注入ISqlSugarClient.NET自带的IOC使用也很方便 先封装一个操作类  //建一个扩展类publicstaticclassSqlsugarS......