欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot整合單機緩存ehcache的實現(xiàn)

 更新時間:2023年02月13日 09:37:35   作者:chenxianchon  
本文主要介紹了springboot整合單機緩存ehcache的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

區(qū)別于redis的分布式緩存,ehcache是純java進程內(nèi)的單機緩存,根據(jù)不同的場景可選擇使用,以下內(nèi)容主要為springboot整合ehcache以及注意事項

添加pom引用

<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache</artifactId>
    <version>2.10.9.2</version>
</dependency>

啟動類添加開啟緩存注解:@EnableCaching

添加xml配置,注意,ehcache需要單獨的配置文件

<?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">
 
    <!--默認(rèn)緩存策略 -->
    <!-- external:是否永久存在,設(shè)置為true則不會被清除,此時與timeout沖突,通常設(shè)置為false-->
    <!-- diskPersistent:是否啟用磁盤持久化-->
    <!-- maxElementsInMemory:最大緩存數(shù)量-->
    <!-- overflowToDisk:超過最大緩存數(shù)量是否持久化到磁盤-->
    <!-- timeToIdleSeconds:最大不活動間隔,設(shè)置過長緩存容易溢出,設(shè)置過短無效果,可用于記錄時效性數(shù)據(jù),例如驗證碼-->
    <!-- timeToLiveSeconds:最大存活時間-->
    <!-- memoryStoreEvictionPolicy:緩存清除策略-->
    <defaultCache
            eternal="false"
            diskPersistent="false"
            maxElementsInMemory="1000"
            overflowToDisk="false"
            timeToIdleSeconds="60"
            timeToLiveSeconds="60"
            memoryStoreEvictionPolicy="LRU" />
 
 
    <cache name="cache1"
           eternal="false"
           diskPersistent="false"
           maxElementsInMemory="1000"
           overflowToDisk="false"
           timeToIdleSeconds="2"
           timeToLiveSeconds="2"
           memoryStoreEvictionPolicy="LRU" />
</ehcache>

這里我定義了一個緩存名字為cache1

修改項目的配置文件application.properties,添加spring緩存類型以及緩存配置文件路徑

spring.cache.ehcache.config=classpath:ehcache.xml
spring.cache.type=ehcache

上面的步驟做好之后,就可以使用了

給你需要加緩存的方法添加注解

@Configuration
public class TestConfig {
 
    @Cacheable(value = "cache1",key = "#id")
    public TestController.Person create(String id) {
        return new TestController.Person();
    }
}

這里的value跟xml配置文件里的一致即可

我們調(diào)用一下測試看看

    @GetMapping("/testCache1")
    public void testCache1(@Param("id") String id) throws InterruptedException {
        Person obj1 = testConfig.create(id);
        Person obj2 = testConfig.create(id);
        Thread.sleep(3000);
        Person obj3 = testConfig.create(id);
        Person obj4 = testConfig.create(id);
 
        log.info("test1:"+obj1.toString());
        log.info("test2:"+obj2.toString());
        log.info("test3:"+obj3.toString());
        log.info("test4:"+obj4.toString());
        System.out.println(obj1.equals(obj2));
    }

執(zhí)行一下結(jié)果看

可以看到,obj1跟obj2是同一個對象,當(dāng)程序睡眠了三秒之后,再次調(diào)用方法,就會重新創(chuàng)建對象,緩存生效

注意事項:

@Cacheable修飾的方法必須是public并且不能是static,原理是因為使用了動態(tài)代理,需要重寫方法

xml里面的配置要寫全,要不然項目啟動報錯,就是下圖這些

xml里面配置的defaultCache沒看出有啥用,我也沒刪了試試

使用緩存的方法不能在@RestController修飾的類中,即不能在controller層,要不然緩存失效,可以在@Service、@Configuratin、@Component等類下面

到此這篇關(guān)于springboot整合單機緩存ehcache的實現(xiàn)的文章就介紹到這了,更多相關(guān)springboot單機緩存ehcache內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論