springboot配置緩存:echcache

1.pom.xml

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
        </dependency>

2.配置文件echache.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">
    <diskStore path="java.io.tmpdir" />
    <defaultCache eternal="false" maxElementsInMemory="1000"
                  overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
                  timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU" />
    <cache
            name="userCache"
            maxElementsInMemory="1000"
            eternal="false"
            timeToIdleSeconds="300"
            timeToLiveSeconds="300"
            overflowToDisk="false"
            memoryStoreEvictionPolicy="LRU">
        <!-- 配置緩存事件監聽器 replicateAsynchronously 操作是否異步,默認值爲true. replicatePuts 添加操作是否同步到集羣內的其他緩存,默認爲true.
            replicateUpdates 更新操作是否同步到集羣內的其他緩存,默認爲true. replicateUpdatesViaCopy 更新之後的對象是否複製到集羣中的其他緩存(true);
            replicateRemovals 刪除操作是否同步到集羣內的其他緩存,默認爲true. -->
        <cacheEventListenerFactory
                class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
                properties="
                    replicateAsynchronously=true,
                    replicatePuts=true,
                    replicateUpdates=true,
                    replicateUpdatesViaCopy=true,
                    replicateRemovals=true " />


        <!-- 初始化緩存,以及自動設置 -->
        <bootstrapCacheLoaderFactory
                class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"
                properties="bootstrapAsynchronously=true" />
    </cache>

    <cache
            name="listGo"
            maxElementsInMemory="1000"
            eternal="false"
            timeToIdleSeconds="300"
            timeToLiveSeconds="300"
            overflowToDisk="false"
            memoryStoreEvictionPolicy="LRU">
        <!-- 配置緩存事件監聽器 replicateAsynchronously 操作是否異步,默認值爲true. replicatePuts 添加操作是否同步到集羣內的其他緩存,默認爲true.
            replicateUpdates 更新操作是否同步到集羣內的其他緩存,默認爲true. replicateUpdatesViaCopy 更新之後的對象是否複製到集羣中的其他緩存(true);
            replicateRemovals 刪除操作是否同步到集羣內的其他緩存,默認爲true. -->
        <cacheEventListenerFactory
                class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
                properties="
                    replicateAsynchronously=true,
                    replicatePuts=true,
                    replicateUpdates=true,
                    replicateUpdatesViaCopy=true,
                    replicateRemovals=true " />


        <!-- 初始化緩存,以及自動設置 -->
        <bootstrapCacheLoaderFactory
                class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"
                properties="bootstrapAsynchronously=true" />
    </cache>

</ehcache>

一個cache代表一個Ecache對象的定義,當然可以定義多個cache對象,針對不同的cache進行配置就行了。

3.開啓


@SpringBootApplication
@EnableCaching  
public class PrintshopwebApplication {

    public static void main(String[] args) {
        SpringApplication.run(PrintshopwebApplication.class, args);
    }

}

4.service層

//查詢方法,值注入名爲userCache的echache對象中
    @Cacheable(key = "1" , value = "userCache")
    public List<Product> selList() {
        return productDao.selList();
    }


//當執行刪除方法是,清空userCache中的緩存內容
    @CacheEvict(value="userCache",allEntries=true,beforeInvocation=true)
    public int delId(int productId) {
        return productDao.delId(productId);
    }

//根據id把緩存存到listGo中,緩存是以鍵值對的形式存在的
    @Cacheable(key = "#productId" , value = "listGo")
    public Product selId(int productId) {
        return productDao.selId(productId);
    }

這部分簡單來說,你爲了提升用戶體驗,查詢所有產品信息,放到緩存中,這樣用戶每次刷新頁面就不用反覆去數據庫找數據,但是如果後臺刪除、新增、修改產品信息呢?CacheEvict就是在刪除產品的時候,清空了userCache中的所有緩存內容,這樣當用戶再次刷新頁面時,selList()方法就會去數據庫查詢了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章