1. 程式人生 > >Spring boot配置cache(ehcache)

Spring boot配置cache(ehcache)

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 @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; } }
  • 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
@CacheConfig
public 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