spring boot配置缓存
第一次配置缓存,记录一下
1. 添加cache依赖
<dependency>
<groupId>net.sf.ehcache
</groupId>
<artifactId>ehcache
</artifactId>
<version>2.10.1
</version>
</dependency>
12345678
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
@EnableCaching
public 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
}
}
123456789101112131415161718192021222324252627
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>
123456789101112131415161718
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
@CacheConfig
public class CacheUtil {
@Cacheable(
"random")
public String getRandomString() {
return UUID
.randomUUID()
.toString()
}
}
1234567891011121314151617181920212223
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())
}
}
1234567891011121314151617181920212223242526272829303132
cacheable注解当中的名称要与xml配置的名称一致 configuration当中的xml名称要与xml的名字一致
6 参考
http://www.tuicool.com/articles/m2qAfqn