实现
首先在Mysql中新建一个表bus_student
然后基于此表使用代码生成,前端Vue与后台各层代码生成并添加菜单。
然后来到后台代码中,在后台框架中已经添加了操作redis的相关依赖和工具类。
但是这里还需要添加aspect依赖
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects --><dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.3.14.RELEASE</version> </dependency>
然后在存放配置类的地方新建新增redis缓存的注解
package com.ruoyi.system.redisAop; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;/* * @Author * @Description 新增redis缓存 **/@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD)public @interface AopCacheEnable {//redis缓存key String[] key();//redis缓存存活时间默认值(可自定义)long expireTime() default 3600; }
以及删除redis缓存的注解
package com.ruoyi.system.redisAop; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;/* * @Description 删除redis缓存注解 **/@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME)public @interface AopCacheEvict {//redis中的key值 String[] key(); }
然后再新建一个自定义缓存切面具体实现类CacheEnableAspect
存放位置
package com.ruoyi.system.redisAop; import com.ruoyi.system.domain.BusStudent; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit;/* * @Description 自定义缓存切面具体实现类 **/@Aspect @Componentpublic class CacheEnableAspect { @Autowiredpublic RedisTemplate redisCache;/** * Mapper层切点 使用到了我们定义的 AopCacheEnable 作为切点表达式。 */@Pointcut("@annotation(com.ruoyi.system.redisAop.AopCacheEnable)")public void queryCache() { }/** * Mapper层切点 使用到了我们定义的 AopCacheEvict 作为切点表达式。 */@Pointcut("@annotation(com.ruoyi.system.redisAop.AopCacheEvict)")public void ClearCache() { } @Around("queryCache()")public Object Interceptor(ProceedingJoinPoint pjp) { Object result = null;//注解中是否有#标识boolean spelFlg = false;//判断是否需要走数据库查询boolean selectDb = false;//redis中缓存的keyString redisKey = "";//获取当前被切注解的方法名Method method = getMethod(pjp);//获取当前被切方法的注解AopCacheEnable aopCacheEnable = method.getAnnotation(AopCacheEnable.class);//获取方法参数值Object[] arguments = pjp.getArgs();//从注解中获取字符串String[] spels = aopCacheEnable.key();for (String spe1l : spels) {if (spe1l.contains("#")) {//注解中包含#标识,则需要拼接spel字符串,返回redis的存储redisKeyredisKey = spe1l.substring(1) + arguments[0].toString(); } else {//没有参数或者参数是List的方法,在缓存中的keyredisKey = spe1l; }//取出缓存中的数据result = redisCache.opsForValue().get(redisKey);//缓存是空的,则需要重新查询数据库if (result == null || selectDb) {try { result = pjp.proceed();//从数据库查询到的结果不是空的if (result != null && result instanceof ArrayList) {//将redis中缓存的结果转换成对象listList<BusStudent> students = (List<BusStudent>) result;//判断方法里面的参数是不是BusStudentif (arguments[0] instanceof BusStudent) {//将rediskey-students 存入到redisredisCache.opsForValue().set(redisKey, students, aopCacheEnable.expireTime(), TimeUnit.SECONDS); } } } catch (Throwable e) { e.printStackTrace(); } } }return result; }/*** 定义清除缓存逻辑,先操作数据库,后清除缓存*/@Around(value = "ClearCache()")public Object evict(ProceedingJoinPoint pjp) throws Throwable {//redis中缓存的keyMethod method = getMethod(pjp);// 获取方法的注解AopCacheEvict cacheEvict = method.getAnnotation(AopCacheEvict.class);//先操作dbObject result = pjp.proceed();// 获取注解的key值String[] fieldKeys = cacheEvict.key();for (String spe1l : fieldKeys) {//根据key从缓存中删除 redisCache.delete(spe1l); }return result; }/** * 获取被拦截方法对象 */public Method getMethod(ProceedingJoinPoint pjp) { Signature signature = pjp.getSignature(); MethodSignature methodSignature = (MethodSignature) signature; Method targetMethod = methodSignature.getMethod();return targetMethod; } }
注意这里的queryCache和ClearCache,里面切点表达式
分别对应上面自定义的两个AopCacheEnable和AopCacheEvict。
然后在环绕通知的queryCache方法执行前后时
获取被切方法的参数,参数中的key,然后根据key去redis中去查询,
如果查不到,就把方法的返回结果转换成对象List,并存入到redis中,
如果能查到,则将结果返回。
然后找到这个表的查询方法,mapper层,比如要将查询的返回结果存储进redis
@AopCacheEnable(key = "BusStudent",expireTime = 40)public List<BusStudent> selectBusStudentList(BusStudent busStudent);
然后在这个表的新增、编辑、删除的mapper方法上添加
/** * 新增学生 * * @param busStudent 学生 * @return 结果 */@AopCacheEvict(key = "BusStudent")public int insertBusStudent(BusStudent busStudent);/** * 修改学生 * * @param busStudent 学生 * @return 结果 */@AopCacheEvict(key = "BusStudent")public int updateBusStudent(BusStudent busStudent);/** * 删除学生 * * @param id 学生ID * @return 结果 */@AopCacheEvict(key = "BusStudent")public int deleteBusStudentById(Integer id);
注意这里的注解上的key要和上面的查询的注解的key一致。
然后启动项目,如果启动时提示:
Consider marking one of the beans as @Primary, updating
the consumer to acce
因为sringboot通过@Autowired注入接口的实现类时发现有多个,也就是有多个类继承了这个接口,spring容器不知道使用哪一个。
找到redis的配置类,在RedisTemplate上添加@Primary注解
验证注解的使用
debug启动项目,在CacheEnableAspect中查询注解中打断点,然后调用查询方法,
就可以看到能进断点,然后就可以根据自己想要的逻辑和效果进行修改注解。
以上是SpringBoot中怎么通过自定义缓存注解实现数据库数据缓存到Redis的详细内容。更多信息请关注PHP中文网其他相关文章!

Redis的关键特性包括速度、灵活性和丰富的数据结构支持。1)速度:Redis作为内存数据库,读写操作几乎瞬时,适用于缓存和会话管理。2)灵活性:支持多种数据结构,如字符串、列表、集合等,适用于复杂数据处理。3)数据结构支持:提供字符串、列表、集合、哈希表等,适合不同业务需求。

Redis的核心功能是高性能的内存数据存储和处理系统。1)高速数据访问:Redis将数据存储在内存中,提供微秒级别的读写速度。2)丰富的数据结构:支持字符串、列表、集合等,适应多种应用场景。3)持久化:通过RDB和AOF方式将数据持久化到磁盘。4)发布订阅:可用于消息队列或实时通信系统。

Redis支持多种数据结构,具体包括:1.字符串(String),适合存储单一值数据;2.列表(List),适用于队列和栈;3.集合(Set),用于存储不重复数据;4.有序集合(SortedSet),适用于排行榜和优先级队列;5.哈希表(Hash),适合存储对象或结构化数据。

Redis计数器是一种使用Redis键值对存储来实现计数操作的机制,包含以下步骤:创建计数器键、增加计数、减少计数、重置计数和获取计数。Redis计数器的优势包括速度快、高并发、持久性和简单易用。它可用于用户访问计数、实时指标跟踪、游戏分数和排名以及订单处理计数等场景。

使用 Redis 命令行工具 (redis-cli) 可通过以下步骤管理和操作 Redis:连接到服务器,指定地址和端口。使用命令名称和参数向服务器发送命令。使用 HELP 命令查看特定命令的帮助信息。使用 QUIT 命令退出命令行工具。

Redis集群模式通过分片将Redis实例部署到多个服务器,提高可扩展性和可用性。搭建步骤如下:创建奇数个Redis实例,端口不同;创建3个sentinel实例,监控Redis实例并进行故障转移;配置sentinel配置文件,添加监控Redis实例信息和故障转移设置;配置Redis实例配置文件,启用集群模式并指定集群信息文件路径;创建nodes.conf文件,包含各Redis实例的信息;启动集群,执行create命令创建集群并指定副本数量;登录集群执行CLUSTER INFO命令验证集群状态;使

要从 Redis 读取队列,需要获取队列名称、使用 LPOP 命令读取元素,并处理空队列。具体步骤如下:获取队列名称:以 "queue:" 前缀命名,如 "queue:my-queue"。使用 LPOP 命令:从队列头部弹出元素并返回其值,如 LPOP queue:my-queue。处理空队列:如果队列为空,LPOP 返回 nil,可先检查队列是否存在再读取元素。

Redis 集群中使用 zset:zset 是一种有序集合,将元素与评分关联。分片策略: a. 哈希分片:根据 zset 键的哈希值分布。 b. 范围分片:根据元素评分划分为范围,并将每个范围分配给不同的节点。读写操作: a. 读操作:如果 zset 键属于当前节点的分片,则在本地处理;否则,路由到相应的分片。 b. 写入操作:始终路由到持有 zset 键的分片。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

记事本++7.3.1
好用且免费的代码编辑器

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

SublimeText3汉化版
中文版,非常好用