springboot 配置Ehcache


Ehcache的基本配置說明我就不說了.小編記錄一下在springboot中使用Ehcache的使用方法.

第一步:在classpath下引入配置文件ehcache.xml

代碼如下:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:noNamespaceSchemaLocation="ehcache.xsd">  
    <cache name="demo"  
           maxEntriesLocalHeap="200"  
           timeToLiveSeconds="600">  
    </cache>  
</ehcache> 

 

第二步springboot開啟對緩存的支持,你需要在springboot啟動的main方法上配置@EnableCaching注解即可

 

第三步就是代碼使用demo了.代碼如下:

首先我們建一個實體類:

public class Thing {

    private Long id;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
    
    
}

然后我們注入一個service,模擬數據crud

@Service
public class CacheDemoServiceImpl {

    private static Map<Long, Thing> data = new HashMap<>();//用與緩存模擬數據 /**
     * 緩存的key
     */
    public static final String THING_ALL_KEY = "\"thing_all\"";
    /**
     * value屬性表示使用哪個緩存策略,緩存策略在ehcache.xml
     */
    public static final String DEMO_CACHE_NAME = "demo";

    @CacheEvict(value = DEMO_CACHE_NAME, key = THING_ALL_KEY)
    public void create(Thing thing) {
        thing.setId(thing.getId());
        data.put(thing.getId(), thing);
    }

    @Cacheable(value = DEMO_CACHE_NAME, key = "#thing.id")
    public Thing findById(Thing thing) {
        Long id=thing.getId();
        System.err.println("沒有走緩存!" + id);
        return data.get(id);
    }

    @Cacheable(value = DEMO_CACHE_NAME, key = THING_ALL_KEY)
    public List<Thing> findAll() {
        return Lists.newArrayList(data.values());
    }

    @CachePut(value = DEMO_CACHE_NAME, key = "#thing.id")
    @CacheEvict(value = DEMO_CACHE_NAME, key = THING_ALL_KEY)
    public Thing update(Thing thing) {
        System.out.println(thing);
        data.put(thing.getId(), thing);
        return thing;
    }

    @CacheEvict(value = DEMO_CACHE_NAME)
    public void delete(Long id) {
        data.remove(id);
    }

}

最后我們建立一個控制層來訪問數據做測試:

    @Autowired
    private CacheDemoServiceImpl cacheDemoServiceImpl;
    
    @RequestMapping("/test/add")
    public void test(@NotNull Long id) {
        Thing t=new Thing();
        t.setId(id);
        cacheDemoServiceImpl.create(t);
    
    }
    
    @RequestMapping("/test/list")
    public JsonResult testlist() {
        List<Thing> list=cacheDemoServiceImpl.findAll();
        return result(200,"",list);
    }
    
    @RequestMapping("/test/one")
    public JsonResult testfind(@NotNull Long id) {
        Thing t=new Thing();
        t.setId(id);
        Thing tt=cacheDemoServiceImpl.findById(t);
        return result(200,"測試緩存",tt);
    
    }
    
    @RequestMapping("/test/delete")
    public void testdelete(@NotNull Long id) {
        cacheDemoServiceImpl.delete(id);
    
    }

 

先執行/test/add, 然后/test/list,其次/test/one,你最后會發現的/test/one 當參數傳入相同的時候時,數據是從緩存中拿了.

 

付:下面是springboot不要Ehcache配置文件的注入方法:

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.cache.interceptor.CacheResolver;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.cache.interceptor.SimpleKeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import net.sf.ehcache.config.CacheConfiguration;

@Configuration
@EnableCaching
public class CachingConfiguration implements CachingConfigurer {
    @Bean(destroyMethod="shutdown")
    public net.sf.ehcache.CacheManager ehCacheManager() {
        CacheConfiguration cacheConfiguration = new CacheConfiguration();
        cacheConfiguration.setName("demo");
        cacheConfiguration.setMemoryStoreEvictionPolicy("LRU");
        cacheConfiguration.setMaxEntriesLocalHeap(1000);
        net.sf.ehcache.config.Configuration config = new net.sf.ehcache.config.Configuration();
        config.addCache(cacheConfiguration);
        return net.sf.ehcache.CacheManager.newInstance(config);
    }

    @Bean
    @Override
    public CacheManager cacheManager() {
        return new EhCacheCacheManager(ehCacheManager());
    }

    @Bean
    @Override
    public KeyGenerator keyGenerator() {
        return new SimpleKeyGenerator();
    }

    @Override
    public CacheResolver cacheResolver() {
        
        return null;
    }

    @Override
    public CacheErrorHandler errorHandler() {
        return null;
    }
    
}

 

每天就進步一點點就可以了...不要想太多

 

參考:http://www.cnblogs.com/lic309/p/4072848.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM