Spring cache

ehcache.xml 配置

<?xml version="1.0" encoding="UTF-8"?>

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"

monitoring="autodetect">

<!-- <diskStore path="java.io.tmpdir" /> -->

<diskStore path="/cachetmpdir" />

<defaultCache maxElementsInMemory="10000" eternal="false"

timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"

maxElementsOnDisk="10000000" diskPersistent="false"

diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" />

<cache name="andCache" maxElementsInMemory="10000"

maxElementsOnDisk="1000" eternal="false" overflowToDisk="true"

diskSpoolBufferSizeMB="20" timeToIdleSeconds="300" timeToLiveSeconds="600"

memoryStoreEvictionPolicy="LFU" />

</ehcache>  

 

applicationContext.xml

<?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:p="http://www.springframework.org/schema/p"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"

xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:tool="http://www.springframework.org/schema/tool" xmlns:cache="http://www.springframework.org/schema/cache"

xsi:schemaLocation="

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd

http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd

http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool-3.1.xsd

http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd"

default-lazy-init="true">

<cache:annotation-driven cache-manager="cacheManager" />

<!-- cacheManager工厂类 -->

<bean id="cacheManagerFactory"

class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"

p:configLocation="classpath:ehcache.xml" />

 

<!-- 声明cacheManager -->

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"

p:cacheManager-ref="cacheManagerFactory" />

</beans>

 

CacheDemoService.java

package com.piaoyi.function.demo.service;

 

import org.springframework.cache.annotation.CacheEvict;

import org.springframework.cache.annotation.Cacheable;

import org.springframework.stereotype.Service;



@Service

public class CacheDemoService{

  @Cacheable(value="andCache",key="#userId + 'CacheDemoService.userId'")

public void save(String userId){

System.out.println("save cache |" +userId +"| CacheDemoService.userId");

}

@CacheEvict(value="andCache",key="#userId + 'CacheDemoService.userId'")

public void flush(String userId){

System.out.println("flush cache |" +userId +"| CacheDemoService.userId");

}

@CacheEvict(value="andCache",allEntries=true)

public void flushAll(){

System.out.println("flush cache all");

}

}

 

DemoTest.java

package com.piaoyi.function.demo;

 

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

import com.piaoyi.function.demo.service.DemoService;

 

public class DemoTest {

 

static String context = null;

static ApplicationContext applicationContext;

static{  

        context = "applicationContext.xml";//ehcache简单配置(命名空间)   

 

        applicationContext = new ClassPathXmlApplicationContext(context);  

    }  

 

/**

* 描述 : <描述函数实现的功能>. <br>

* <p>

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

CacheDemoService service =(DemoService)applicationContext.getBean("cacheDemoService");  

 

service.save("123");//打印

service.save("aaa");//打印

service.save("123");//不打印

service.save("aaa");//不打印

service.flush("123");//打印

service.flush("aaa");//打印

service.flush("aaa");//不打印

service.flushAll();//打印

}

 

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值