通过AOP方式实现Service计算结果的缓存

2024-05-02 02:08

本文主要是介绍通过AOP方式实现Service计算结果的缓存,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

AOP为Aspect Oriented Programming, 面向切面的编程。意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。

Spring框架的AOP基础知识详见:

Spring实现AOP的4种方式

Spring AOP 详解

AOP的应用场景很多。可以作为日志系统的记录拦截,也可以用于缓存的拦截。

以下用我自己的框架项目 springMVC 中实现的Ehcache对Service层计算结果缓存的实现为例子。介绍一下。

Service缓存实现

主要实现思路通过AOP拦截的方式插入缓存层的操作。

参考:Spring AOP+ehCache简单缓存系统解决方案

简单的配置以及实现效果

Ehcache配置

ehcache的配置文件默认在 classpath:ehcache.xml MAVEN工程对应 src/main/resources 。定义了缓存的配置信息,* 必须有一个 默认缓存defaultCache *。在管理器找不到缓存的时候,就会用默认缓存。


<?xml version="1.0" encoding="UTF-8"?>
<!-- Ehcache2.x的变化(取自https://github.com/springside/springside4/wiki/Ehcache) -->
<!-- 1)最好在ehcache.xml中声明不进行updateCheck -->
<!-- 2)为了配合BigMemory和Size Limit,原来的属性最好改名 -->
<!-- maxElementsInMemory->maxEntriesLocalHeap -->
<!-- maxElementsOnDisk->maxEntriesLocalDisk -->
<ehcache mlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"><diskStore path="java.io.tmpdir" /><defaultCache maxElementsInMemory="1000" eternal="false"timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false" /><cache name="myCache" maxElementsOnDisk="20000"maxElementsInMemory="2000" eternal="true" overflowToDisk="true"diskPersistent="true" />
</ehcache>
<!-- <diskStore>==========当内存缓存中对象数量超过maxElementsInMemory时,将缓存对象写到磁盘缓存中(需对象实现序列化接口) <diskStore path="">==用来配置磁盘缓存使用的物理路径,Ehcache磁盘缓存使用的文件后缀名是*.data和*.index name=================缓存名称,cache的唯一标识(ehcache会把这个cache放到HashMap里) maxElementsOnDisk====磁盘缓存中最多可以存放的元素数量,0表示无穷大 maxElementsInMemory==内存缓存中最多可以存放的元素数量,若放入Cache中的元素超过这个数值,则有以下两种情况 1)若overflowToDisk=true,则会将Cache中多出的元素放入磁盘文件中 2)若overflowToDisk=false,则根据memoryStoreEvictionPolicy策略替换Cache中原有的元素 eternal==============缓存中对象是否永久有效,即是否永驻内存,true时将忽略timeToIdleSeconds和timeToLiveSeconds timeToIdleSeconds====缓存数据在失效前的允许闲置时间(单位:秒),仅当eternal=false时使用,默认值是0表示可闲置时间无穷大,此为可选属性 即访问这个cache中元素的最大间隔时间,若超过这个时间没有访问此Cache中的某个元素,那么此元素将被从Cache中清除 timeToLiveSeconds====缓存数据在失效前的允许存活时间(单位:秒),仅当eternal=false时使用,默认值是0表示可存活时间无穷大 即Cache中的某元素从创建到清楚的生存时间,也就是说从创建开始计时,当超过这个时间时,此元素将从Cache中清除 overflowToDisk=======内存不足时,是否启用磁盘缓存(即内存中对象数量达到maxElementsInMemory时,Ehcache会将对象写到磁盘中) 会根据标签中path值查找对应的属性值,写入磁盘的文件会放在path文件夹下,文件的名称是cache的名称,后缀名是data diskPersistent=======是否持久化磁盘缓存,当这个属性的值为true时,系统在初始化时会在磁盘中查找文件名为cache名称,后缀名为index的文件 这个文件中存放了已经持久化在磁盘中的cache的index,找到后会把cache加载到内存 要想把cache真正持久化到磁盘,写程序时注意执行net.sf.ehcache.Cache.put(Element element)后要调用flush()方法 diskExpiryThreadIntervalSeconds==磁盘缓存的清理线程运行间隔,默认是120秒 diskSpoolBufferSizeMB============设置DiskStore(磁盘缓存)的缓存区大小,默认是30MB memoryStoreEvictionPolicy========内存存储与释放策略,即达到maxElementsInMemory限制时,Ehcache会根据指定策略清理内存 共有三种策略,分别为LRU(最近最少使用)、LFU(最常用的)、FIFO(先进先出) -->

AOP拦截

拦截器编写
  • 取缓存 MethodCacheInterceptor

package org.company.core.common.interceptor;import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.List;import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;public class MethodCacheInterceptor implements MethodInterceptor,InitializingBean {private Cache cache;/*** 设置缓存名*/public void setCache(Cache cache) {this.cache = cache;}/*** 检查是否提供必要参数。*/public void afterPropertiesSet() throws Exception {Assert.notNull(cache,"A cache is required. Use setCache(Cache) to provide one.");}/*** 主方法 如果某方法可被缓存就缓存其结果 方法结果必须是可序列化的(serializable)*/public Object invoke(MethodInvocation invocation) throws Throwable {
//      String methodName = invocation.getMethod().getName();
//      if(methodName.contains("find")){
//          return get(invocation);
//      } else {
//          return remove(invocation);
//      }String targetName = invocation.getThis().getClass().getName();String methodName = invocation.getMethod().getName();Object[] arguments = invocation.getArguments();Object result;String cacheKey = getCacheKey(targetName, methodName, arguments);System.out.println("getting from cache...");Element element = cache.get(cacheKey);System.out.println("cache Key:" + cacheKey);if (element == null) {System.out.println("miss");result = invocation.proceed();element = new Element(cacheKey, (Serializable) result);cache.put(element);return element.getValue();} else {System.out.println("hit");return element.getValue();}}//  }/*** creates cache key: targetName.methodName.argument0.argument1...*/private String getCacheKey(String targetName, String methodName,Object[] arguments) {StringBuffer sb = new StringBuffer();sb.append(targetName).append(".").append(methodName);if ((arguments != null) && (arguments.length != 0)) {for (int i = 0; i < arguments.length; i++) {sb.append(".").append(arguments[i]);}}return sb.toString();}
}
  • 删除缓存

package org.company.core.common.interceptor;import java.lang.reflect.Method;
import java.util.List;import net.sf.ehcache.Cache;import org.springframework.aop.AfterReturningAdvice;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;public class MethodCacheAfterAdvice implements AfterReturningAdvice, InitializingBean    
{    private Cache cache;    public void setCache(Cache cache) {    this.cache = cache;    }    public MethodCacheAfterAdvice() {    super();    }    @Transactionalpublic void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {    String className = arg3.getClass().getName();    List list = cache.getKeys();    System.out.println("invoking...");System.out.println("removing cache key:"+className+".*");for(int i = 0;i<list.size();i++){    String cacheKey = String.valueOf(list.get(i));    if(cacheKey.startsWith(className)){    System.out.println("remving key:"+cacheKey);cache.remove(cacheKey);    }    }    }    public void afterPropertiesSet() throws Exception {    Assert.notNull(cache, "Need a cache. Please use setCache(Cache) create it.");    }      
}   
配置文件

配置详见 applicationContext-ehcache.xml

注册拦截器类

声明了拦截器的BEAN

<!-- find/create cache拦截器 -->  <bean id="methodCacheInterceptor" class="org.company.frame.interceptor.MethodCacheInterceptor"><property name="cache"><ref local="demoCache" /></property></bean><!-- flush cache拦截器 -->  <bean id="methodCacheAfterAdvice" class="org.company.frame.interceptor.MethodCacheAfterAdvice">  <property name="cache">  <ref local="demoCache" />  </property>  </bean>  
切入点声明

将拦截器BEAN与方法匹配。

<!-- find/get cache拦截器方法匹配 --><bean id="methodCachePointCut"class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"><property name="advice"><ref local="methodCacheInterceptor" /></property><property name="patterns"><list><value>org.company.core.*.service.*find.*</value></list></property></bean><!-- flush 拦截器方法匹配  --><bean id="methodCachePointCutAdvice" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">  <property name="advice">  <ref local="methodCacheAfterAdvice"/>  </property>  <property name="patterns">  <list>  <value>org.company.core.*.service.*create.*</value>  <value>org.company.core.*.service.*update.*</value>  <value>org.company.core.*.service.*delete.*</value>  </list>  </property>  </bean>
AOP的最终声明

绑定服务BEAN与拦截器切入点。

<!-- AOP 的最终配置 --><!-- BEAN与find/get 拦截器关联 --><bean id="myService" class="org.springframework.aop.framework.ProxyFactoryBean"><property name="target"><ref local="testTableServiceBean" /></property><property name="interceptorNames"><list><value>methodCachePointCut</value><value>methodCachePointCutAdvice</value></list></property></bean> <!-- 拦截BEAN --><bean id="testTableServiceBean" class="org.company.core.moduel.service.TestTableServiceImpl"></bean>

缓存的最终实现效果如图:

这里写图片描述

这篇关于通过AOP方式实现Service计算结果的缓存的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/953178

相关文章

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi

让树莓派智能语音助手实现定时提醒功能

最初的时候是想直接在rasa 的chatbot上实现,因为rasa本身是带有remindschedule模块的。不过经过一番折腾后,忽然发现,chatbot上实现的定时,语音助手不一定会有响应。因为,我目前语音助手的代码设置了长时间无应答会结束对话,这样一来,chatbot定时提醒的触发就不会被语音助手获悉。那怎么让语音助手也具有定时提醒功能呢? 我最后选择的方法是用threading.Time

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

内核启动时减少log的方式

内核引导选项 内核引导选项大体上可以分为两类:一类与设备无关、另一类与设备有关。与设备有关的引导选项多如牛毛,需要你自己阅读内核中的相应驱动程序源码以获取其能够接受的引导选项。比如,如果你想知道可以向 AHA1542 SCSI 驱动程序传递哪些引导选项,那么就查看 drivers/scsi/aha1542.c 文件,一般在前面 100 行注释里就可以找到所接受的引导选项说明。大多数选项是通过"_

C#实战|大乐透选号器[6]:实现实时显示已选择的红蓝球数量

哈喽,你好啊,我是雷工。 关于大乐透选号器在前面已经记录了5篇笔记,这是第6篇; 接下来实现实时显示当前选中红球数量,蓝球数量; 以下为练习笔记。 01 效果演示 当选择和取消选择红球或蓝球时,在对应的位置显示实时已选择的红球、蓝球的数量; 02 标签名称 分别设置Label标签名称为:lblRedCount、lblBlueCount

缓存雪崩问题

缓存雪崩是缓存中大量key失效后当高并发到来时导致大量请求到数据库,瞬间耗尽数据库资源,导致数据库无法使用。 解决方案: 1、使用锁进行控制 2、对同一类型信息的key设置不同的过期时间 3、缓存预热 1. 什么是缓存雪崩 缓存雪崩是指在短时间内,大量缓存数据同时失效,导致所有请求直接涌向数据库,瞬间增加数据库的负载压力,可能导致数据库性能下降甚至崩溃。这种情况往往发生在缓存中大量 k

Kubernetes PodSecurityPolicy:PSP能实现的5种主要安全策略

Kubernetes PodSecurityPolicy:PSP能实现的5种主要安全策略 1. 特权模式限制2. 宿主机资源隔离3. 用户和组管理4. 权限提升控制5. SELinux配置 💖The Begin💖点点关注,收藏不迷路💖 Kubernetes的PodSecurityPolicy(PSP)是一个关键的安全特性,它在Pod创建之前实施安全策略,确保P

用命令行的方式启动.netcore webapi

用命令行的方式启动.netcore web项目 进入指定的项目文件夹,比如我发布后的代码放在下面文件夹中 在此地址栏中输入“cmd”,打开命令提示符,进入到发布代码目录 命令行启动.netcore项目的命令为:  dotnet 项目启动文件.dll --urls="http://*:对外端口" --ip="本机ip" --port=项目内部端口 例: dotnet Imagine.M