spring配置ehcache

1.引入需要的jar
2.配置ehcache,ehcache.xml内容如下:

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

    <defaultCache maxElementsInMemory="10000" eternal="false"
        timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false"
        diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"
        diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="LRU" />

    <cache name="userCache" maxElementsInMemory="1000" eternal="false"
        timeToIdleSeconds="100000" timeToLiveSeconds="100000" overflowToDisk="false" />

</ehcache>

3.配置spring管理ehcache:

<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:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
            http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">

    <!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->
    <cache:annotation-driven cache-manager="cacheManager" />

    <!-- cacheManager工厂类,指定ehcache.xml的位置 -->
    <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>

4.编写测试代码:
public class EhcacheSpringTest {

private CacheManager cacheManager;

private Cache cache;

@Before
public void init() {
    ApplicationContext ctx = new ClassPathXmlApplicationContext(
            "applicationContext.xml");
    EhCacheCacheManager ehCacheManager = (EhCacheCacheManager) ctx
            .getBean("cacheManager");
    cacheManager = ehCacheManager.getCacheManager();
    cache = cacheManager.getCache("userCache");
}

@After
public void destory() {
    cacheManager.shutdown();
}

@Test
public void testEhcacheString() {
    String key = "hello";
    String value = "world";
    cache.put(new Element(key, value));
    Element element = cache.get(key);
    Object obj = element.getObjectValue();
    Assert.assertEquals(value, obj);
    System.out.println(obj);
}

@Test
public void testEhcacheObj() {
    String key = "user";
    User value = new User("zhangsan", "lisi");
    cache.put(new Element(key, value));
    Element element = cache.get(key);
    Object obj = element.getObjectValue();
    Assert.assertEquals(value, obj);
    System.out.println(obj);
    Object objSer = element.getValue();
    Assert.assertEquals(value, objSer);
    System.out.println(objSer);
}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值