java spring 使用注解来实现缓存
2013-07-12 10:57
706 查看
这里举例使用spring3.1.4+ehcache
注解的方式使用cache是在spring3.1加入的
使用方法:
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
http://www.ehcache.org/documentation/configuration/configuration
2.1首先建立一个ehcache.xml的配置文件
2.2在spring的apllication.xml加入注入的cache
这里还需要在配置文件头部引入
在mvc-servlet.xml里加入
这种情况类中方法中返回值都会被缓存,
3.2用于方法method
这个就会将age小于25的值,按name为key缓存
3.3清除
还可使用下面的清除全部
注解的方式使用cache是在spring3.1加入的
使用方法:
1.ehcache依赖+spring依赖
<!--ehcache依赖--> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.7.2</version> </dependency>
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.1.4.RELEASE</version> </dependency> <!--这里有注解类需要的jar--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.1.4.RELEASE</version> </dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
2.spring+ehcache的简单配置
Ehcache的基本配置,具体参数意思看:name | cache的唯一标识 |
maxElementsInMemory | 最大创建条数 |
eternal | 缓存是否是永久的,默认false |
timeToLiveSeconds | 缓存的存活时间 |
timeToIdleSeconds | 多长时间不访问就清楚该缓存 |
overflowToDisk | 内存不足是否写入磁盘,默认False |
maxElementsOnDisk | 持久化到硬盘最大条数 |
memoryStoreEvictionPolicy | 缓存满了后,清除缓存的规则, 自带三种:FIFO(先进先出),LFU(最少使用),LRU(最近最少使用) |
diskSpoolBufferSizeMB | 磁盘缓存的缓存区大小,默认30M |
diskExpiryThreadIntervalSeconds | 磁盘失效线程时间间隔 |
<ehcache> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> <cache name="onecache" maxElementsInMemory="10000" eternal="false" overflowToDisk="false" timeToIdleSeconds="10" timeToLiveSeconds="10"/> </ehcache>
2.2在spring的apllication.xml加入注入的cache
<cache:annotation-drivencache-manager="cacheManager"/>
<beanid="cacheManager"class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cacheManager-ref="ehcache"/>
<beanid="ehcache"class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:configLocation="classpath:ehcache.xml"
p:shared="true"/>
这里还需要在配置文件头部引入
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
...http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd"
在mvc-servlet.xml里加入
<mvc:annotation-driven/><!--挺重要的,不加注解不会被扫描-->
3.注解使用
3.1用于对象class@Cacheable(value="onecache")
classA1{}
这种情况类中方法中返回值都会被缓存,
3.2用于方法method
@Cacheable(value="onecache",key="#name",condition="#age<25")
publicxxfindEmployeeBySurname(StringfirstName,Stringname,intage){
returnxxx
}
这个就会将age小于25的值,按name为key缓存
3.3清除
@CacheEvict(value="onecache",key="#name+'haha'")
publicvoiddelete(Stringname){
System.out.println("deleteonename");
}
还可使用下面的清除全部
@CacheEvict(value="oneCache",allEntries=true)
4.代码调用
@Autowired
privateCacheManagercacheManager;
//获取当前时间
publicStringgetABCCache(){
cacheManager.getCache("ccc").put("hello",newDate().toString());
return(String)cacheManager.getCache("ccc").get("hello").get();
}
相关文章推荐
- 使用java5的注解和Sping/AspectJ的AOP 来实现Memcached的缓存
- 使用java5的注解和Sping/AspectJ的AOP 来实现Memcached的缓存
- 使用Spring AOP结合自定义Java注解实现动态数据源设置
- 使用java5的注解和Sping/AspectJ的AOP 来实现Memcached的缓存
- 深入理解Spring Redis的使用 (八)、Spring Redis实现 注解 自动缓存
- 使用java5的注解和Sping/AspectJ的AOP 来实现Memcached的缓存
- Java注解的使用,实现仿Spring的对象装载
- 深入理解Spring Redis的使用 (八)、Spring Redis实现 注解 自动缓存
- 使用spring-modules-0.9实现注解缓存
- [原创]Java使用反射及自定义注解实现对象差异性比较
- Spring中使用自定义的注解校验器的实现
- spring-使用注解实现Bean自动装配1
- Java注解机制之Spring自动装配实现原理
- 基于纯Java代码的Spring容器和Web容器零配置的思考和实现(3) - 使用配置
- Spring缓存注解@Cache,@CachePut , @CacheEvict,@CacheConfig使用
- java使用spring实现读写分离的示例代码
- SpringMVC中使用@RequestBody,@ResponseBody注解实现Java对象和XML/JSON数据自动转换(上)
- 使用spring注解方式实现AOP(一)
- 使用Spring的注解方式实现AOP入门
- 【Java EE 学习 50】【Spring学习第二天】【使用注解的DI实现】【spring中的继承】【动态代理伪hibernate实现】