当前位置:网站首页>Redis的使用
Redis的使用
2022-08-05 05:25:00 【技术的搬运工】
前言
SprintBoot 项目中,用到的 redis 的方式有四种:
- 利用注解
- 利用 RedisTemplate
- 利用 CacheManager
- 利用 Jedis
这里只提供给大家可以使用的几种方式, 关于这四种方式网上的文章很多,这里不再赘述
1、注解方式
主要注解有:
- @Cacheable
- @Caching
- @CacheEvic
这里需要注意一下,@Cacheable 所在的 package 是 spring 的,
有些bug是因为使用了swagger里面的@Cacheable注解导致缓存无效。
import org.springframework.cache.annotation.Cacheable; // 正解的import
import springfox.documentation.annotations.Cacheable; // 错误的import
另外,需要注意同一个类内部调用缓存不生效和不同类调用缓存无法清除等问题,
可以参考以下文章:
SpringCache @Cacheable 在同一个类中调用方法,导致缓存不生效的问题及解决办法
2、RedisTemplate方式
使用这种方式操作redis,需要注意如果缓存的是一个对象,这对象的必须要是可序列化的,即必须要继承Serializable,否则缓存时会报序列化失败。
使用redisTemplate方式的一个好处提供的方法比较多,配置过期时间比较自由,可以发挥redis的诸多功能。
3、CacheManager方式
用由工具类CacheUtils来操作,UserUtils 里面用来 CacheUtils 缓存用户信息。
为了方便使用,我加了一个泛型方法
/**
* 获取缓存,没有则执行表达式,并将结果放入缓存
* @param cacheName
* @param key
* @param supplier
* @param <T>
* @return
*/
public static <T> T get(String cacheName, String key, Supplier<T> supplier) {
Object obj = get(cacheName, key);
if (obj != null) {
return (T) obj;
} else {
T result = supplier.get();
put(cacheName, key, result);
return result;
}
}
4、Jedis方式
Jedis 工具类
package org.linlinjava.litemall.common.utils;
/**
* @title: StringUtils.java
* @package cn.jeeweb.common.util
* @description: Jedis Cache 工具类
*/
public class JedisUtils {
// 省略其他代码
}
边栏推荐
- From "dual card dual standby" to "dual communication", vivo took the lead in promoting the implementation of the DSDA architecture
- VS Code私有服务器部署(私有化)
- el-progress implements different colors of the progress bar
- 超简单的白鹭egret项目添加图片详细教程
- Shadowless Cloud Desktop
- vscode notes
- Drools规则引擎快速入门(一)
- In-depth analysis if according to data authority @datascope (annotation + AOP + dynamic sql splicing) [step by step, with analysis process]
- 无法导入torchvision.io.read_image
- Quick Start to Drools Rule Engine (1)
猜你喜欢

BIO,NIO,AIO实践学习笔记(便于理解理论)

config.js related configuration summary

白鹭egret添加新页面教程,如何添加新页面

el-progress implements different colors of the progress bar

Writing OpenCV in VSCode

人人AI(吴恩达系列)
![[Problem has been resolved]-Virtual machine error contains a file system with errors check forced](/img/07/1222a44dd52b359bf7873e6f3b7ebf.png)
[Problem has been resolved]-Virtual machine error contains a file system with errors check forced

花花省V5淘宝客APP源码无加密社交电商自营商城系统带抖音接口

Chengyun Technology was invited to attend the 2022 Alibaba Cloud Partner Conference and won the "Gathering Strength and Going Far" Award

亚马逊美国站:马术头盔CPC认证标准要求
随机推荐
LaTeX使用frame制作PPT图片没有标号
滚动条问题,未解决
多行文本省略
Email management Filter emails
Collision, character controller, Cloth components (cloth), joints in the Unity physics engine
ES2020新特性
Configuration of routers and static routes
NB-IOT智能云家具项目系列实站
Does flink cdc currently support Gauss database sources?
Programmers should understand I/O this way
NAT experiment
NACOS Configuration Center Settings Profile
网络协议基础-学习笔记
深入分析若依数据权限@datascope (注解+AOP+动态sql拼接) 【循序渐进,附分析过程】
LeetCode practice and self-comprehension record (1)
ALC experiment
Cocos Creator Mini Game Case "Stick Soldier"
[Problem has been resolved]-Virtual machine error contains a file system with errors check forced
NACOS配置中心设置配置文件
Teach you simple steps to achieve industrial raspberries pie properly installed RS232 USB drive