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

Redis配置、测试及分布式缓存实现

一、引入Redis依赖

引入spring-boot-starter-data-redis依赖,不需要指定version,默认和springboot的version保持一致

<!-- Redis -->  
<dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-data-redis</artifactId>  
</dependency>

二、配置Redis

spring:  # Redis 配置  redis:  database: 3  host: 127.0.0.1  port: 6379  timeout: 5000

1、database指定Redis的数据库索引,Redis默认有16个逻辑数据库(从0到15)

2、host Redis 服务器的地址,如果要部署上线需要改成服务器IP

3、port 部署Redis的端口,一般默认是6379,为了安全也可以进行修改

4、不需要指定username,指定的话也不要写成root !!默认的用户的defult

三、测试Redis

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;/*** @author <a href="https://github.com/lieeew">leikooo</a>* @date 2024/12/25* @description*/
@SpringBootTest
public class RedisTemplate {@Resourceprivate StringRedisTemplate stringRedisTemplate;@Testpublic void testRedis() {ValueOperations<String, String> valueOps = stringRedisTemplate.opsForValue();String key = "testKey";String value = "testValue";// 1、测试新政和更新操作valueOps.set(key, value, 2, TimeUnit.MINUTES);String storeValue = valueOps.get(key);System.out.println(storeValue);  //结果:testValueassertEquals(storeValue, value, "存储的值和预期不一致");// 2、测试修改String updateValue = "updateValue";valueOps.set(key, updateValue);storeValue = valueOps.get(key);System.out.println(storeValue);// 结果:updateValueassertEquals(storeValue, "updateValue", "存储的值和预期不一致");// 3、测试查询操作valueOps.get(key);storeValue = valueOps.get(key);System.out.println(storeValue);// 结果:updateValueassertEquals(storeValue, updateValue, "存储的值和预期不一致");// 4、测试删除操作stringRedisTemplate.delete(key);storeValue = valueOps.get(key);System.out.println(storeValue); //结果:nullassertNull(storeValue, "删除后值不为 null ");}
}

四、Redis分布式缓存的简单实现

@Resourceprivate StringRedisTemplate stringRedisTemplate;public List<Object> getRedisCacheList() {// 限制爬虫 --->一次性只能拿多少条数据// 构建缓存 keyString redisKey = "xxxxxxxxx";// 从 Redis 缓存中查询  ValueOperations<String, String> valueOps = stringRedisTemplate.opsForValue();String cachedValue = valueOps.get(redisKey);if (cachedValue != null) {// 如果缓存命中,返回结果  List<Object> cachedPage = JSONUtil.toBean(cachedValue, Object.class);return cachedPage;}// 查询数据库,封装数据,具体内容根据业务逻辑封装List<Object> pictureVOPage = new ArrayList<>();// 存入 Redis 缓存  String cacheValue = JSONUtil.toJsonStr(pictureVOPage);// 5 - 10 分钟随机过期,防止雪崩  int cacheExpireTime = 300 + RandomUtil.randomInt(0, 300);valueOps.set(redisKey, cacheValue, cacheExpireTime, TimeUnit.SECONDS);// 返回结果  return pictureVOPage;}

http://www.dtcms.com/a/320063.html

相关文章:

  • Android 之 Jetpack - Paging
  • 《C语言》函数练习题--2
  • ElasticSearch相关术语介绍
  • 使用 decimal 包解决 go float 浮点数运算失真
  • 小鸡模拟器安卓版:经典街机游戏的移动体验
  • 利用Axure与JavaScript打造动态图片上传原型:设计案例分享
  • spring-cglib代理-初探01
  • 深度学习-卷积神经网络CNN-1×1卷积层
  • Flink-1.19.0源码详解9-ExecutionGraph生成-后篇
  • UE5多人MOBA+GAS 39、制作角色上半身UI
  • 字符串匹配(重点解析KMP算法)
  • 6 大模块!重构物业运营方式
  • 跨境电商增长突围:多维变局下的战略重构与技术赋能
  • 数智先锋 | Bonree ONE 赋能通威股份有限公司提升全栈可观测性能力
  • 深入解析NVIDIA Nsight工具套件:原理、功能与实战指南
  • 房产证识别在房产行业的技术实现及应用原理
  • Python Socket 脚本深度解析与开发指南
  • 扣扣号码展示网站源码_号码售卖展示系统源码 全开源 带后台(源码下载)
  • 5、倒计时翻页效果
  • 工作任务管理
  • 《C语言》指针练习题--1
  • Python入门Day17:函数式编程(map/filter/reduce/lambda)
  • 浏览器渲染与GPU进程通信图解
  • Numpy科学计算与数据分析:Numpy数组操作入门:合并、分割与重塑
  • PWM常用库函数(STC8系列)
  • 【Linux基础知识系列】第八十七篇 - 使用df命令查看磁盘空间
  • 橙河网络:Cint站点如何注册?好做吗?
  • 街道垃圾识别准确率↑32%:陌讯多模态融合算法实战解析
  • 解锁制药新质生产力:合规与效率双赢的数字化转型之道
  • 基于肌电信号的神经网络动作识别系统