spring配置ehcache

spring配置文件

<cache:annotation-driven cache-manager="cacheManager" />
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager" ref="ehcache"></property>
    </bean>
    <bean id="ehcache"
        class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:ehcache.xml"></property>
    </bean>

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">
    <diskStore path="java.io.tmpdir" />
    <defaultCache eternal="false" maxElementsInMemory="1000"
        overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
        timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU" />

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

</ehcache>

实现类

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class CacheServiceImpl implements CacheService{
    @Cacheable(value="departCache",key="#i")
    public String findUser(int i) {
        StringBuffer sb=new StringBuffer();
        int j=0;
        for(;j<i;j++){
            sb.append("hello"+i);
        }
        return sb.toString();

    }

}

测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/application.xml")
public class TestCache {   

   @Resource
   CacheServiceImpl cacheServiceImpl;

    @Test
    public void testSend() {
     System.out.println(System.currentTimeMillis());
       cacheServiceImpl.findUser(1000);
       System.out.println(System.currentTimeMillis());

       System.out.println(System.currentTimeMillis());
       cacheServiceImpl.findUser(1000);
       System.out.println(System.currentTimeMillis());

    }   

}  
时间: 2024-07-31 14:35:18

spring配置ehcache的相关文章

转Spring+Hibernate+EHcache配置(二)

Spring AOP+EHCache简单缓存系统解决方案 需要使用Spring来实现一个Cache简单的解决方案,具体需求如下:使用任意一个现有开源Cache Framework,要求可以Cache系统中Service或则DAO层的get/find等方法返回结果,如果数据更新(使用Create/update/delete方法),则刷新cache中相应的内容. MethodCacheInterceptor.java Java代码 package com.co.cache.ehcache;     

Spring配置错误记录

更多Spring问题由于发生时未记录而遗忘了~~~~~~~ 现在动动手 解决方案由于不是源头分析因而仅供参考!!! 严重: Exception sending context destroyed event to listener instance of class org.springframework.web.context.ContextLoaderListener java.lang.IllegalStateException: BeanFactory not initialized o

Spring整合Ehcache管理缓存

Ehcache 是一个成熟的缓存框架,你可以直接使用它来管理你的缓存. Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案(如Ehcache),但本身不直接提供缓存功能的实现.它支持注解方式使用缓存,非常方便.本文先通过Ehcache独立应用的范例来介绍它的基本使用方法,然后再介绍与Spring整合的方法. 概述 Ehcache是什么?EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点.它是Hibernate中的默认缓存框架.Ehcache已经发布了3.1版本

Spring搭配Ehcache实例解析

转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/50538085 1 Ehcache简介 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. Ehcache是一种广泛使用的开源Java分布式缓存.主要面向通用缓存,Java EE和轻量级容器.它具有内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序,一个gzip缓存servlet过滤器,支持REST

缓存插件 Spring支持EHCache缓存

Spring仅仅是提供了对缓存的支持,但它并没有任何的缓存功能的实现,spring使用的是第三方的缓存框架来实现缓存的功能.其中,spring对EHCache提供了很好的支持. 在介绍Spring的缓存配置之前,我们先看一下EHCache是如何配置. <?xml version="1.0" encoding="UTF-8" ?> <ehcache> <!-- 定义默认的缓存区,如果在未指定缓存区时,默认使用该缓存区 --> <

spring 配置

SpringMVC配置 引入占位符<context:property-placeholder location="classpath:config.properties" /><util:properties id="properties" location="classpath:config.properties"/> <!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 --&

缓存初解(三)---Spring3.0基于注解的缓存配置+Ehcache和OScache

本文将构建一个普通工程来说明spring注解缓存的使用方式,关于如何在web应用中使用注解缓存,请参见: Spring基于注解的缓存配置--web应用实例 一.简介 在spring的modules包中提供对许多第三方缓存方案的支持,包括: EHCache OSCache(OpenSymphony) JCS GigaSpaces JBoss Cache 等等. 将这些第三方缓存方案配置在spring中很简单,网上有许多介绍,这里只重点介绍如何配置基于注解的缓存配置. 本文将通过例举EHCache和

Spring AOP +EHcache为Service层方法增加缓存

在铁科院做了一个关于医保报销的项目,在这个个系统中大量使用了下拉列表框,系统主要是给机关单位使用而且都是一些干部退休了啥的,年龄都比较大不愿意自己输入东西,因此界面上的很多值都是下拉列表框从数据字典表里面加载出来. 如此以来字典表的数据量变的越来越大,在一个界面上往往需要频繁的与字典表交互,觉的很影响性能于是我们增加了缓存,即为service层中的指定方法缓存功能,具体实现是利用Spring AOP+EHcache来做. 第一次执行某个方法的时候会去数据库里面查询,当第二次执行该方法时就会去从缓

spring使用ehcache缓存

1.首先去官方下载spring jar包,我用的是sring 4.1.6已经集成了ehcache. 2.写spring配置文件,注意需要添加 cache约束文件. xmlns:cache="http://www.springframework.org/schema/cache" http://www.springframework.org/schema/cache     http://www.springframework.org/schema/cache/spring-cache.