ehcache 注解 key注解怎么使用在无参方法上

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
您的访问请求被拒绝 403 Forbidden - ITeye技术社区
您的访问请求被拒绝
亲爱的会员,您的IP地址所在网段被ITeye拒绝服务,这可能是以下两种情况导致:
一、您所在的网段内有网络爬虫大量抓取ITeye网页,为保证其他人流畅的访问ITeye,该网段被ITeye拒绝
二、您通过某个代理服务器访问ITeye网站,该代理服务器被网络爬虫利用,大量抓取ITeye网页
请您点击按钮解除封锁&java(53)
spring(9)
缓存注解有以下三个:
@Cacheable & & &@CacheEvict & &&@CachePut
@Cacheable(value=”accountCache”),这个注释的意思是,当调用这个方法的时候,会从一个名叫 accountCache 的缓存中查询,如果没有,则执行实际的方法(即查询数据库),并将执行的结果存入缓存中,否则返回缓存中的对象。这里的缓存中的 key 就是参数 userName,value 就是 Account 对象。“accountCache”缓存是在
spring*.xml 中定义的名称。
Java代码&&
@Cacheable(value=&accountCache&)&&
public&Account&getAccountByName(String&userName)&{&&
&&&&&System.out.println(&real&query&account.&&#43;userName);&&&
&&&&&return&getFromDB(userName);&&&
@CacheEvict 注释来标记要清空缓存的方法,当这个方法被调用后,即会清空缓存。注意其中一个 @CacheEvict(value=”accountCache”,key=”#account.getName()”),其中的 Key 是用来指定缓存的 key 的,这里因为我们保存的时候用的是 account 对象的 name 字段,所以这里还需要从参数
account 对象中获取 name 的&#20540;来作为 key,前面的 # 号代表这是一个 SpEL 表达式,此表达式可以遍历方法的参数对象,具体语法可以参考 Spring 的相关文档手册。
Java代码&&
@CacheEvict(value=&accountCache&,key=&#account.getName()&)&&
public&void&updateAccount(Account&account)&{&&
&&&&&updateDB(account);&&&
@CacheEvict(value=&accountCache&,allEntries=true)&&
public&void&reload()&{&&
&&&&&reloadAll()&&
@Cacheable(value=&accountCache&,condition=&#userName.length()&&=4&)&&
public&Account&getAccountByName(String&userName)&{&&&
&return&getFromDB(userName);&&&
@CachePut 注释,这个注释可以确保方法被执行,同时方法的返回&#20540;也被记录到缓存中,实现缓存与数据库的同步更新。
Java代码&&
@CachePut(value=&accountCache&,key=&#account.getName()&)&&
public&Account&updateAccount(Account&account)&{&&&
&&&return&updateDB(account);&&&
@Cacheable、@CachePut、@CacheEvict 注释介绍
通过上面的例子,我们可以看到 spring cache 主要使用两个注释标签,即 @Cacheable、@CachePut 和 @CacheEvict,我们总结一下其作用和配置方法。
表 1. @Cacheable 作用和配置方法
@Cacheable 的作用&主要针对方法配置,能够根据方法的请求参数对其结果进行缓存
@Cacheable 主要的参数
缓存的名称,在 spring 配置文件中定义,必须指定至少一个
@Cacheable(value=”mycache”) 或者&
@Cacheable(value={”cache1”,”cache2”}
缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合
@Cacheable(value=”testcache”,key=”#userName”)
缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存
@Cacheable(value=”testcache”,condition=”#userName.length()&2”)
表 2. @CachePut 作用和配置方法
@CachePut 的作用&主要针对方法配置,能够根据方法的请求参数对其结果进行缓存,和 @Cacheable 不同的是,它每次都会触发真实方法的调用
@CachePut 主要的参数
缓存的名称,在 spring 配置文件中定义,必须指定至少一个
@Cacheable(value=”mycache”) 或者&
@Cacheable(value={”cache1”,”cache2”}
缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合
@Cacheable(value=”testcache”,key=”#userName”)
缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存
@Cacheable(value=”testcache”,condition=”#userName.length()&2”)
表 3. @CacheEvict 作用和配置方法
@CachEvict 的作用&主要针对方法配置,能够根据一定的条件对缓存进行清空
@CacheEvict 主要的参数
缓存的名称,在 spring 配置文件中定义,必须指定至少一个
@CachEvict(value=”mycache”) 或者&
@CachEvict(value={”cache1”,”cache2”}
缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合
@CachEvict(value=”testcache”,key=”#userName”)
缓存的条件,可以为空,使用 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)
转载出处:
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:22515次
排名:千里之外
原创:45篇
转载:22篇
评论:74条
(1)(6)(6)(10)(5)(14)(3)(6)(5)(7)(2)(1)spring + ehcache + mybatis缓存控制,使用注解没有起到作用。 - 开源中国社区
当前访客身份:游客 [
当前位置:
&dependency&
&groupId&net.sf.ehcache&/groupId&
&artifactId&ehcache-core&/artifactId&
&version&2.6.11&/version&
&/dependency&
&dependency&
&groupId&com.googlecode.ehcache-spring-annotations&/groupId&
&artifactId&ehcache-spring-annotations&/artifactId&
&version&1.2.0&/version&
&/dependency&
以上是引入两个jar包
@Cacheable(cacheName = "testCache")
public User findUserById(int id) throws Exception {
return (User) dao.findForObject("UserMapper.selectByPrimaryKey", id);
我在方法上使用@Cacheable注解和不使用注解,效果是一样的。都有缓存查询结果。
比如我第一次查询一个id为2的
10:27:01,141 DEBUG [org.mybatis.spring.SqlSessionUtils] - Creating a new SqlSession
10:27:01,141 DEBUG [org.mybatis.spring.SqlSessionUtils] - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7c933b60]
10:27:01,141 DEBUG [UserMapper] - Cache Hit Ratio [UserMapper]: 0.3333
10:27:01,141 DEBUG [org.mybatis.spring.transaction.SpringManagedTransaction] - JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@292ef64] will be managed by Spring
10:27:01,141 DEBUG [UserMapper.selectByPrimaryKey] - ==&
Preparing: select id, account, password, user_name, s from user where id = ?
10:27:01,141 DEBUG [UserMapper.selectByPrimaryKey] - ==& Parameters: 2(Integer)
10:27:01,141 DEBUG [UserMapper.selectByPrimaryKey] - &==
10:27:01,141 DEBUG [org.mybatis.spring.SqlSessionUtils] - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7c933b60]
10:27:01,141 DEBUG [org.mybatis.spring.SqlSessionUtils] - Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7c933b60]
10:27:01,141 DEBUG [org.mybatis.spring.SqlSessionUtils] - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7c933b60]
10:28:28,454 DEBUG [org.mybatis.spring.SqlSessionUtils] - Creating a new SqlSession
10:28:28,454 DEBUG [org.mybatis.spring.SqlSessionUtils] - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5930a78f]
10:28:28,469 DEBUG [UserMapper] - Cache Hit Ratio [UserMapper]: 0.5
10:28:28,469 DEBUG [org.mybatis.spring.SqlSessionUtils] - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5930a78f]
10:28:28,469 DEBUG [org.mybatis.spring.SqlSessionUtils] - Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5930a78f]
10:28:28,469 DEBUG [org.mybatis.spring.SqlSessionUtils] - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5930a78f]
使不使用都是这个结果。
mybatis mapper中我就增加了一行&cache/&。没有其他了。
spring配置文件中
&bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" scope="singleton"&
&property name="configLocation" value="classpath:ehcache.xml" /&
ehcache配置文件
&?xml version="1.0" encoding="UTF-8"?&
&ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="false"&
&diskStore path="java.io.tmpdir"/&
&defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="30"
timeToLiveSeconds="30"
overflowToDisk="true"/&
&cache name="testCache"
maxElementsInMemory="10000"
eternal="true"
overflowToDisk="false"
timeToIdleSeconds="0"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU" /&
&/ehcache&
共有3个答案
<span class="a_vote_num" id="a_vote_num_
<span class="a_vote_num" id="a_vote_num_
&cache:annotation-driven cache-manager="ehCacheManager" proxy-target-class="false"/& &
<span class="a_vote_num" id="a_vote_num_
spring配置文件中添加使cache注解生效
更多开发者职位上
有什么技术问题吗?
Kylin_S...的其它问题
类似的话题& & 今天下午做了一下缓存整合在项目中,以前没有用过也没有学过。都知道spring已经对Ehcache进行了很好的支持,我的spring版本是3.2.2在spring-context-support.jar包中可以看到.以前的版本可能会不太一样。
我下的ehcache是和一个做页面的缓存一个做查询的缓存。
& & echcache的配置也不说了网上一大堆,而且解释的也很清楚。但是spring基于注解的整合好像还不太一样,好吧看官方文档进行配置好一点。
& & 官网上有这么一句话:EHCache support moved to spring-context-support 看来以前的版本还不是在上面提到的那个包中。接着看配置吧:
&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.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"&
&cache:annotation-driven /&
&/beans& 这是官方文档中的基于注解的XML配置如果加上缓存的配置的话上面的配置还要加上下面这一句:
xmlns:p="http://www.springframework.org/schema/p" 缓存的配置如下:
&bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache"/&
&!-- EhCache library setup --&
&bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:config-location="classpath:ehcache.xml" p:shared="true"/&
OK!!缓存已经完成了
说明一下官网上没有p:shared="true"在配echcahe的时候,这时启动会报一个错误:
net.sf.ehcache.CacheException:Another unnamed CacheManager already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following
我找了半天也不知道起动的时候哪还创建了一个CancheManager但加上
p:shared="true"就没事了。命名空间的p打不开我也不知道是什么意思。请高手指点。我觉得大概意思就是使用一个cache吧。
做一个简单的测试!
@Cacheable(value="sampleCache1",key="#id")
public T getById(String id) {
return (T) getSession().get(clazz, id);
} 在baseDao的get方法上进行配置在测试类中用userService服务进行测试看输出几条sql语句:
public class CancheSpring extends BaseSpringTest {
private UserService userS
public void testGetCanche(){
String id = "e060ab0000";
User user = userService.getById(id);
System.out.println(user);
User user2 = userService.getById(id);
System.out.println(user2);
} 当然id我从数据库中直接copy出来的UUID.输出结果如下:
OK。很明显可以看到两个对象是一模一样的。好吧配置成功能了可以在其他需要的地方方便使用。还有一个注解是@CacheEvict是删除操作。页面缓存明天再写,也有好多不明白的地方。 看完记的指点一下哈!
&?xml version=&1.0& encoding=&UTF-8&?&&beans xmlns=&http://www.springframework.org/schema/beans&
xmlns:xsi=&http://www.w3.org/2001/XMLSchema-instance&
xsi:schemaLocation=&http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd&&
&!--xmemcached--&
&bean name=&memcachedClientBuilder& class=&net.rubyeye.xmemcached.XMemcachedClientBuilder&&
&constructor-arg&
&bean class=&java.net.InetSocketAddress&&
&constructor-arg value=&127.0.0.1&/&
&constructor-arg value=&11211&/&
&/constructor-arg&
&!--config weight--&
&constructor-arg&
&value&1&/value&
&!--&value&2&/value&--&
&/constructor-arg&
&property name=&connectionPoolSize& value=&10&/&
&property name=&commandFactory&&
&bean class=&net.mand.TextCommandFactory&/&
&/property&
&property name=&sessionLocator&&
&bean class=&net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator&/&
&/property&
&property name=&transcoder&&
&bean class=&net.rubyeye.xmemcached.transcoders.SerializingTranscoder& /&
&/property&
&property name=&name& value=&landlordsServerCached&/&
&bean name=&memcachedClient& factory-bean=&memcachedClientBuilder& factory-method=&build& destroy-method=&shutdown&/&&/beans&
&?xml version=&1.0& encoding=&UTF-8&?&&beans xmlns=&http://www.springframework.org/schema/beans&
xmlns:xsi=&http://www.w3.org/2001/XMLSchema-instance&
xsi:schemaLocation=&http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd&&
&!--xmemcached--&
&bean name=&memcachedClientBuilder& class=&net.rubyeye.xmemcached.XMemcachedClientBuilder&&
&constructor-arg&
&bean class=&java.net.InetSocketAddress&&
&constructor-arg value=&127.0.0.1&/&
&constructor-arg value=&11211&/&
&/constructor-arg&
&!--config weight--&
&constructor-arg&
&value&1&/value&
&!--&value&2&/value&--&
&/constructor-arg&
&property name=&connectionPoolSize& value=&10&/&
&property name=&commandFactory&&
&bean class=&net.mand.TextCommandFactory&/&
&/property&
&property name=&sessionLocator&&
&bean class=&net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator&/&
&/property&
&property name=&transcoder&&
&bean class=&net.rubyeye.xmemcached.transcoders.SerializingTranscoder& /&
&/property&
&property name=&name& value=&landlordsServerCached&/&
&bean name=&memcachedClient& factory-bean=&memcachedClientBuilder& factory-method=&build& destroy-method=&shutdown&/&&/beans&net.rubyeye.xmemcached,是xmemcached自带的么?感觉包名不一样?
弱弱问一句,你用STS可以轻松加上,STS是什么啊?
弱弱问一句,你用STS可以轻松加上,STS是什么啊?Spring Tool Suite
弱弱问一句,你用STS可以轻松加上,STS是什么啊?Spring Tool Suite intellij idea
同样可以很轻松找到哈
弱弱问一句,你用STS可以轻松加上,STS是什么啊?SPRING TOOL SUITE
额。。。我也不清楚哈!!如果你觉得我写不好或有什么不对的地方欢迎指出来!!我入行时间不长好多东西需要学习!!谢谢!
& 开源中国(OSChina.NET) |
开源中国社区(OSChina.net)是工信部
指定的官方社区

我要回帖

更多关于 ehcache 注解不起作用 的文章

 

随机推荐