SpringBootsad整合EhCache作緩存處理

輕量級的緩存框架Ehcache實現其功能。從如下幾點切入:java

  • 什麼是EhCache?
  • 它和redis、membercache比較有什麼優點?
  • 和SpringBoot怎麼整合?
  • 實現機制?
  • 有哪些坑?
    1. EhCache 是一個純Java的進程內緩存框架,具備快速、精幹等特色,是Hibernate中默認CacheProvider。Ehcache是一種普遍使用的開源Java分佈式緩存。主要面向通用緩存,Java EE和輕量級容器。它具備內存和磁盤存儲,緩存加載器,緩存擴展,緩存異常處理程序,一個gzip緩存servlet過濾器,支持REST和SOAP api等特色。
          Spring 提供了對緩存功能的抽象:即容許綁定不一樣的緩存解決方案(如Ehcache),但自己不直接提供緩存功能的實現。它支持註解方式使用緩存,很是方便。
    2.     快速
          簡單
          多種緩存策略
          緩存數據有兩級:內存和磁盤,所以無需擔憂容量問題
          緩存數據會在虛擬機重啓的過程當中寫入磁盤
          能夠經過RMI、可插入API等方式進行分佈式緩存
          具備緩存和緩存管理器的偵聽接口
          支持多緩存管理器實例,以及一個實例的多個緩存區域
          提供Hibernate的緩存實現
    3. (1)

導入jar包
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
            <!-- Ehcache 座標 -->
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
        </dependency>redis

(2)spring

新建ehcache.xml配置文件
    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
    <diskStore path="java.io.tmpdir/ehcache"/>
    <!--defaultCache:echcache的默認緩存策略  -->
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            maxElementsOnDisk="10000000"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
    </defaultCache>
    <cache name="users"
           maxElementsInMemory="10000"
           eternal="false"
           timeToIdleSeconds="120"
           timeToLiveSeconds="120"
           maxElementsOnDisk="10000000"
           diskExpiryThreadIntervalSeconds="120"
           memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
    </cache>
    </ehcache>
    而後再經過application.properties配置下
    spring.cache.ehcache.config=classpath:ehcache.xml數據庫

 

   (3)api

啓動類上添加@EnableCaching
    在寫一個能夠擴展的配置類
    /**
     * 開啓緩存配置
     *
     * @author zhangyi
     * @date 2018/12/13 15:47
     */
    @Configuration
    public class EhCacheConfig {
    }緩存

(4)app

在類上添加緩存配置,方法上添加緩存操做

    @Service
    @CacheConfig(cacheNames = "users")
    public class UserServiceImpl implements UserService{
    @Cacheable(value = "users")
    @Override
    public List<User> getAllUser(){
        List<User> list = new ArrayList<>();
        for(int i = 0; i < 5; i++) {
            User user = new User();
            user.setUserName(String.valueOf(i+Math.random()*10));
            user.setPassWord(String.valueOf(i));
            list.add(user);
        }
        System.out.println("模擬數據庫查詢... 過程");
        return list;
    }
    }框架

result:dom

第一次查詢
    模擬數據庫查詢... 過程
    8.339899184231392--0
    4.358651013143946--1
    4.244988713811452--2
    9.693692145368964--3
    8.744268864524635--4
    第二次查詢
    8.339899184231392--0
    4.358651013143946--1
    4.244988713811452--2
    9.693692145368964--3
    8.744268864524635--4
    第三次查詢
    8.339899184231392--0
    4.358651013143946--1
    4.244988713811452--2
    9.693692145368964--3
    8.744268864524635—4分佈式

簡單的三步走,後續的緩存一致性經過 CachePut CacheEvent來控制數據庫和緩存數據之間的同步性
第一次查詢是經過執行serverImpl中方法查看的,後續的緩存中有數據的時候,經過緩存讀取
坑:
    在使用SoringBoot整合shiro時候,使用的是Ehcache作緩存在shiro配置類中,配置了EhcacheManager,致使報錯,看了許多教程都是錯誤的,目前直接在    application文件中加載其配置類就行了,直接緩存信息

相關文章
相關標籤/搜索