Spring boot配置cache(ehcache)

来源:互联网 发布:旅行社软件管理 编辑:程序博客网 时间:2024/06/05 02:10

spring boot配置缓存

第一次配置缓存,记录一下

1. 添加cache依赖

<dependency><groupId>net.sf.ehcache</groupId><artifactId>ehcache</artifactId><version>2.10.1</version></dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2. 配置cacheConfiguration类

package com.idcos.automate.biz.common.cache;import org.springframework.cache.annotation.EnableCaching;import org.springframework.cache.ehcache.EhCacheCacheManager;import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.io.ClassPathResource;@Configuration@EnableCachingpublic class EhcacheConfiguration {    @Bean(name = "ehCacheCacheManager")    public EhCacheCacheManager ehCacheCacheManager(EhCacheManagerFactoryBean bean) {        return new EhCacheCacheManager(bean.getObject());    }    @Bean    public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() {        EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean();        cacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));        cacheManagerFactoryBean.setShared(true);        return cacheManagerFactoryBean;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

3. 配置xml

<?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/Tmp_EhCache"/>    <defaultCache eternal="false" maxElementsInMemory="1000" overflowToDisk="false" diskPersistent="false"                  timeToIdleSeconds="0" timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU"/>    <cache name="random"           eternal="false"           maxElementsInMemory="100"           overflowToDisk="false"           diskPersistent="false"           timeToIdleSeconds="0"           timeToLiveSeconds="300"           memoryStoreEvictionPolicy="LRU"/></ehcache>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

4. 配置cache实现类

package com.idcos.cloud.biz.common.util;import java.util.List;import java.util.UUID;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cache.annotation.CacheConfig;import org.springframework.cache.annotation.Cacheable;import org.springframework.stereotype.Service;import com.idcos.cloud.core.dal.domain.settings.SettingPlatformDict;import com.idcos.cloud.core.dal.repository.settings.SettingPlatformDictRepository;@Service@CacheConfigpublic class CacheUtil {    @Cacheable("random")    public String getRandomString() {        return UUID.randomUUID().toString();    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

5. 测试

package pkg_manager;import org.junit.Assert;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.SpringApplicationConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.idcos.PkgManager;import com.idcos.cloud.biz.common.util.CacheUtil;@RunWith(SpringJUnit4ClassRunner.class)@SpringApplicationConfiguration(classes = PkgManager.class)public class PkgTest {    @Autowired    private CacheUtil CacheUtil;    @Test    public void test() {        testCache();    }    public void testCache() {        Assert.assertNotNull(CacheUtil);        Assert.assertEquals(CacheUtil.getRandomString(), CacheUtil.getRandomString());        System.out.println(CacheUtil.getRandomString());        System.out.println(CacheUtil.getRandomString());    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

cacheable注解当中的名称要与xml配置的名称一致 
configuration当中的xml名称要与xml的名字一致

6 参考

http://www.tuicool.com/articles/m2qAfqn

原创粉丝点击