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>
2. 配置cacheConfiguration类
package com.idcos.automate.biz.common.cacheimport org.springframework.cache.annotation.EnableCachingimport org.springframework.cache.ehcache.EhCacheCacheManagerimport org.springframework.cache.ehcache.EhCacheManagerFactoryBeanimport org.springframework.context.annotation.Beanimport org.springframework.context.annotation.Configurationimport 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.utilimport java.util.Listimport java.util.UUIDimport org.springframework.beans.factory.annotation.Autowiredimport org.springframework.cache.annotation.CacheConfigimport org.springframework.cache.annotation.Cacheableimport org.springframework.stereotype.Serviceimport com.idcos.cloud.core.dal.domain.settings.SettingPlatformDictimport 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_managerimport org.junit.Assertimport org.junit.Testimport org.junit.runner.RunWithimport org.springframework.beans.factory.annotation.Autowiredimport org.springframework.boot.test.SpringApplicationConfigurationimport org.springframework.test.context.junit4.SpringJUnit4ClassRunnerimport com.idcos.PkgManagerimport 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