ehcache-spring-annotations

简介: ehcache-spring-annotations

google提供了一个更简易的ehcache配置。

jar下载地址 http://code.google.com/p/ehcache-spring-annotations/

 

你只需在spring配置文件中配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
     xsi:schemaLocation="
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
     http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">

    <ehcache:annotation-driven cache-manager="ehCacheManager" />
    
    <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"/>
 
</beans>

配置ehcache

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false">
    <!--
     | Please see http://ehcache.sourceforge.net/documentation/configuration.html for
     | detailed information on how to configurigure caches in this file
     +-->
    <!-- Location of persistent caches on disk -->
    <diskStore path="java.io.tmpdir/EhCacheSpringAnnotationsExampleApp" />

    <defaultCache eternal="false" maxElementsInMemory="1000"
        overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
        timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU"/>

    <cache name="testCache" eternal="false"
        maxElementsInMemory="100" overflowToDisk="false" diskPersistent="false"
        timeToIdleSeconds="0" timeToLiveSeconds="300"
        memoryStoreEvictionPolicy="LRU" />
 
</ehcache>

编写测试类

package com.dzf.cache;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.googlecode.ehcache.annotations.Cacheable;
import com.googlecode.ehcache.annotations.TriggersRemove;
@Service
@Transactional
public class CacheService{

    @Cacheable(cacheName = "testCache")
    public String getName(int i){
        System.out.println("Processing testCache");
        return "nihao:"+i;
    }
   
    @TriggersRemove(cacheName="testCache")
    public void flush(){
        System.out.println("Processing testFlushing");
    }
}

很容易就把cache给管理起来了。相当的方便。

目录
相关文章
|
9月前
|
XML Java 数据格式
spring Resources
spring Resources
77 0
|
9月前
|
缓存 NoSQL Java
SpringBoot整合Spring Cache
SpringBoot整合Spring Cache
36 0
|
9月前
|
XML Java 数据格式
spring之资源操作:Resources
【1月更文挑战第17天】 一、Spring Resources概述 二、Resource接口 三、Resource的实现类 1、UrlResource访问网络资源 2、ClassPathResource 访问类路径下资源 3、FileSystemResource 访问文件系统资源 4、ServletContextResource 5、InputStreamResource 6、ByteArrayResource 四、Resource类图 五、ResourceLoader 接口 1、ResourceLoader 概述 2、使用演示 3、ResourceLoader 总结 六、ResourceLo
136 1
|
9月前
|
XML Java 数据格式
Spring Resources资源操作
Spring Resources资源操作
|
缓存 Java 数据库连接
Spring Boot 2.X(七):Spring Cache 使用
Spring Cache 简介 在 Spring 3.1 中引入了多 Cache 的支持,在 spring-context 包中定义了org.springframework.cache.Cache 和 org.springframework.cache.CacheManager 两个接口来统一不同的缓存技术。
1240 0
|
XML Java 数据格式
Spring之 @Configuration
Spring 提供了丰富的特性和功能,包括依赖注入、面向切面编程、事务管理、数据访问、Web应用程序开发等。其中,@Configuration 是 Spring 中的一个注解,它用于标记一个类为配置类,通过配置类可以定义和组装 Spring Bean,并且支持高度灵活的配置方式。在本文中,我们将深入探讨 @Configuration 注解的底层原理,包括其与 Spring IoC 容器的集成、其对 Bean 的定义和装配的实现,以及其在 Spring 框架中的作用和用法。
196 0
|
存储 缓存 开发框架
SpringBoot系列:整合Spring Security
Spring 是一个非常流行和成功的 Java 应用开发框架。Spring Security 基于 Spring 框架,提供了一套 Web 应用安全性的完整解决方案。一般来说,Web 应用的安全性包括用户认证(Authentication*)和用户授权(Authorization) 两个部分。
223 0
SpringBoot系列:整合Spring Security
|
Java Spring 容器
【Spring】Spring常用配置-Bean的Scope
【Spring】Spring常用配置-Bean的Scope
249 0
【Spring】Spring常用配置-Bean的Scope
|
存储 缓存 NoSQL
Spring Boot中的缓存支持(一)注解配置与EhCache使用
Spring Boot中的缓存支持(一)注解配置与EhCache使用
283 0

热门文章

最新文章