Spring Cache 配置及一些问题的解决

配置

1. applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:cache="http://www.springframework.org/schema/cache"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"><cache:annotation-driven /><!-- 定义缓存管理 --><bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager"><property name="caches"><set><bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"p:name="default"/><bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"p:name="activityCache"/><bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"p:name="awardsCache"/></set></property></bean>

Spring内部默认使用 ConcurrentHashMap 来存储, 配置中的 activityCache awardsCache 就是一个一个的 ConcurrentHashMap 对象的名字. 另外 spring还支持使用 EHCache 来存储缓存.

 

2. 在service的实现类上加上 @Cacheable

//@Service
//public class LotteryActivityServiceImpl implements LotteryActivityService@Cacheable(value = "activityCache", key = "#shortName")
public LotteryActivity findByShortName(String shortName) {log.info("query activity : {} from database.", shortName);return activityRepository.findByShortName(shortName);
}

 @Cacheable参数

value  缓存的名称,在 spring 配置文件中定义,必须指定至少一个 例如:@Cacheable(value=”mycache”) 或者 
@Cacheable(value={”cache1”,”cache2”}
key  缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合 例如:
@Cacheable(value=”testcache”,key=”#userName”)
condition缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存 例如:
@Cacheable(value=”testcache”,condition=”#userName.length()>2”)

在需要清除缓存的方法上加上@CacheEvict 

@CacheEvict(value="activityCache", allEntries = true, beforeInvocation = true)
public void cleanActivityCache(String shortName) {log.info("cleaned cache activity : {}", shortName);
}

@CacheEvict 参数

value缓存的名称,在 spring 配置文件中定义,必须指定至少一个 例如:
@CachEvict(value=”mycache”) 或者 
@CachEvict(value={”cache1”,”cache2”}
key缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合 例如:
@CachEvict(value=”testcache”,key=”#userName”
condition缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才清空缓存 例如:
@CachEvict(value=”testcache”,
condition=”#userName.length()>2”)
allEntries是否清空所有缓存内容,缺省为 false,如果指定为 true,则方法调用后将立即清空所有缓存 例如:
@CachEvict(value=”testcache”,allEntries=true)
beforeInvocation是否在方法执行前就清空,缺省为 false,如果指定为 true,则在方法还没有执行的时候就清空缓存,缺省情况下,如果方法执行抛出异常,则不会清空缓存  例如:
@CachEvict(value=”testcache”,beforeInvocation=true)

当需要保证方法被调用,又希望结果被缓存, 可以使用@CachePut 

@CachePut(value="accountCache",key="#account.getName()")// 更新 accountCache 缓存public Account updateAccount(Account account) { return updateDB(account); } 

@CachePut 参数

value缓存的名称,在 spring 配置文件中定义,必须指定至少一个  例如:
@Cacheable(value=”mycache”) 或者 
@Cacheable(value={”cache1”,”cache2”}
key缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合  例如:
@Cacheable(value=”testcache”,key=”#userName”)
condition缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存  例如:
@Cacheable(value=”testcache”,condition=”#userName.length()>2”)

 

 注解最好加在实现类而不是接口的方法上

Spring recommends that you only annotate concrete classes (and methods of concrete classes) with the @Cache* annotation, as opposed to annotating interfaces. You certainly can place the @Cache* annotation on an interface (or an interface method), but this works only as you would expect it to if you are using interface-based proxies. The fact that Java annotations are not inherited from interfaces means that if you are using class-based proxies (proxy-target-class="true") or the weaving-based aspect (mode="aspectj"), then the caching settings are not recognized by the proxying and weaving infrastructure, and the object will not be wrapped in a caching proxy, which would be decidedly bad.

带有@Cache* 注解的方法不能被定义在调用该方法的类里, 比如 UserController要调用 findUserByName(String name), 且该方法有 @Cacheabele注解, 那么该方法就不能被定义在 UserController中

In proxy mode (which is the default), only external method calls coming in through the proxy are intercepted. This means that self-invocation, in effect, a method within the target object calling another method of the target object, will not lead to an actual caching at runtime even if the invoked method is marked with @Cacheable - considering using the aspectj mode in this case.

@Cache*注解要加在 public 方法上

When using proxies, you should apply the @Cache* annotations only to methods with public visibility. If you do annotate protected, private or package-visible methods with these annotations, no error is raised, but the annotated method does not exhibit the configured caching settings. Consider the use of AspectJ (see below) if you need to annotate non-public methods as it changes the bytecode itself.

 

 

转载于:https://www.cnblogs.com/ykt8465279130/p/3521557.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/493512.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

泊松融合——用了拉普拉斯但没有金字塔

图像融合的方式有alpha融合&#xff0c;拉普拉斯金字塔融合。 同样是基于拉普拉斯算子&#xff0c;我们可以直接用求解的方式得到融合后的图像。因为人眼对二阶导是更敏感的&#xff0c;所以只要我们指定了融合区域内部的梯度值&#xff0c;并且知道融合边界处的值&#xff0c…

三层神经网络实现手写字母的识别(基于tensorflow)

数据集的制作参考这篇文章&#xff1a; https://blog.csdn.net/fanzonghao/article/details/81229409 一&#xff0c;读取数据集 import tensorflow as tf import numpy as np import pickle import matplotlib.pyplot as plt #对于x变成(samles,pixs),y变成one_hot (sample…

(转)Kinect背景移除支持多人

原文&#xff1a;http://blogs.msdn.com/b/k4wdev/archive/2013/10/22/using-kinect-background-removal-with-multiple-users.aspx?utm_sourcetuicool Introduction: Background Removal in Kinect for Windows The 1.8 release of the Kinect for Windows Developer Toolkit…

德国汉堡科学院院士张建伟:信息物理系统驱动智能未来

来源&#xff1a;OFweek工控网随着第四次工业革命的到来&#xff0c;信息技术&#xff08;IT&#xff09;和运营技术&#xff08;OT&#xff09;的融合成为新趋势&#xff0c;工厂开始进入数字化转型阶段&#xff0c;而德国“工业4.0”战略给全球制造业发展带来启示&#xff0c…

两层卷积网络实现手写字母的识别(基于tensorflow)

可和这篇文章对比&#xff0c;https://blog.csdn.net/fanzonghao/article/details/81489049&#xff0c;数据集来源代码和链接一样。 import tensorflow as tf import numpy as np import matplotlib.pyplot as plt import read_pickle_datasettrain_dataset,train_label,vali…

焦李成教授谈深度神经网络发展历程

来源&#xff1a;西电人工智能学院摘要&#xff1a;焦李成教授谈深度神经网络发展历程2018年11月18日下午&#xff0c;计算机科学与技术学部主任、人工智能学院焦李成教授在成都参加了由中国人工智能学会主办的人工智能大讲堂并做特邀报告&#xff0c;焦李成教授在报告中回顾了…

SGU 187 - Twist and whirl -- want to cheat

原题地址&#xff1a;http://acm.sgu.ru/problem.php?contest0&problem187 太开心啦&#xff01;&#xff01;&#xff01;&#xff01;这道题从2013年开始困扰我&#xff01;&#xff01;今天晚上第四次下定决心把它写一写&#xff0c;之前写了三次&#xff08;事实上是五…

KNN实现CIFAR-10数据集识别

cs231n链接&#xff1a;http://cs231n.github.io/linear-classify/&#xff0c; 训练集链接&#xff1a;https://download.csdn.net/download/fanzonghao/10592049 KNN缺点&#xff1a;每个测试样本都要循环一遍训练样本。 该数据集由5个data_batch和一个test_batch构成&…

近期苹果、Facebook等科技巨头股价缘何不断下跌?

来源&#xff1a;资本实验室近期&#xff0c;FAANG(Facebook、亚马逊、苹果、Netflix、谷歌)等科技巨头股价都出现了不同程度的下跌&#xff0c;而美国科技股整体的持续大跌&#xff0c;更是引发了全球股市振荡。其中&#xff0c;亚马逊在今年9月初达曾达到1万亿美元市值&#…

ZOJ 3300 Mahjong DFS暴力解决。。

ZJU 3300&#xff1a; 题目描述 就看成1—9的数字&#xff0c;DFS暴力搜索下就过了。。。。。要求输入13 个数字&#xff0c;声明个数组记录数字的个数&#xff0c;DFS 里各种回溯&#xff1b;#include<stdio.h> #include<string.h> int p[12],flag; int dfs(int x…

概率论基础知识各种分布

离散分布&#xff1a;伯努力分布&#xff0c;二项分布&#xff0c;possion分布 一&#xff0c;伯努力分布 #执硬币 x_arrnp.array([0,1]) #x为1的概率 p0.7 #0 1分布 #由PMF生成对应的概率 离散事件 pr_arrstats.bernoulli.pmf(x_arr,p) plt.plot(x_arr,pr_arr,markero,lines…

AI 芯片和传统芯片的区别

来源&#xff1a;内容来自「知乎汪鹏 」所谓的AI芯片&#xff0c;一般是指针对AI算法的ASIC&#xff08;专用芯片&#xff09;。传统的CPU、GPU都可以拿来执行AI算法&#xff0c;但是速度慢&#xff0c;性能低&#xff0c;无法实际商用。比如&#xff0c;自动驾驶需要识别道路行…

三层神经网络实现手写数字的识别(基于tensorflow)

数据集链接&#xff1a;https://download.csdn.net/download/fanzonghao/10598333 from tensorflow.examples.tutorials.mnist import input_data mnist input_data.read_data_sets("./mnist/", one_hotTrue)import tensorflow as tf# Parameters learning_rate 0…

鼠标终将消失,未来我们有哪些人机交互方式?

来源&#xff1a;资本实验室在人类发明史上&#xff0c;诞生了无数的英雄。他们的发明往往从一项前沿技术到家喻户晓、无处不在&#xff0c;但他们自己却又鲜为人知&#xff0c;美国发明家道格拉斯恩格尔巴特就是其中的代表。20世纪60年代&#xff0c;道格拉斯恩格尔巴特发明了…

两层卷积网络实现手写数字的识别(基于tensorflow)

可和这篇文章对比&#xff1a;https://blog.csdn.net/fanzonghao/article/details/81603367 # coding: utf-8 # ## MNIST数据集from __future__ import division, print_function, absolute_importimport tensorflow as tf# Import MNIST data&#xff0c;MNIST数据集导入 fro…

光波导总结资料

1、写出光波导中的麦克斯韦方程&#xff0c;并把光场分解为纵向分量与横向分量&#xff0c;求出混合模式HE与EH模式的横向电场强度与横向磁场强度的点积&#xff08;用纵向分量表示&#xff09;&#xff08;需要有推导过程&#xff09; 解&#xff1a;在线性、各向同性且时不变…

德国再出颠覆性发明,这次要安排我们的快递

来源&#xff1a;最黑科技摘要&#xff1a;如果用一句话来形容德国的工业设计&#xff0c;我能想到的就是&#xff1a;“母牛坐电锯——锯牛逼"&#xff0c;小编已经不止一次把它吹得五光十色斗转星移~但你可能不知道&#xff0c;这个工业强国在2013年还提出了一个著名的发…

跟我一起写 Makefile

跟我一起写 Makefile【转】 陈皓 概述—— 什么是makefile&#xff1f;或许很多Winodws的程序员都不知道这个东西&#xff0c;因为那些Windows的IDE都为你做了这个工作&#xff0c;但我觉得要作一个好的和professional的程序员&#xff0c;makefile还是要懂。这就好像现在有这么…

C++中用frugally-deep调用keras的模型并进行预测

1、背景 Python语言中的Keras库搭建深度学习模型非常便捷&#xff0c;但有时需要在 C 中调用训练好的模型&#xff0c;得到测试集的结果。比如将模型部署于FPGA&#xff0c;中间的一个步骤则需要用C构建模型。但 Keras库没有提供 C API&#xff0c;其中一种解决方法是使用 Ten…

简单的线性回归实现模型的存储和读取

和这篇文章对比https://blog.csdn.net/fanzonghao/article/details/81023730 不希望重复定义图上的运算&#xff0c;也就是在模型恢复过程中&#xff0c;不想sess.run(init)首先看路径 lineRegulation_model.py定义线性回归类&#xff1a; import tensorflow as tf "&qu…