spingboot第三方util形式整合ehcache

本文详细介绍了如何在Spring Boot项目中集成Ehcache缓存。从添加依赖到配置文件,再到具体代码实现,包括使用注解进行缓存操作和自定义Ehcache工具类。同时,解释了配置项的作用,如内存缓存大小、过期时间等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ehcache的各种原理,应用都不介绍了,官网里有,其他博客一大堆。您可以快速查阅,这篇博客纯属笔记。

开始使用要在application.java的开始代码中添加@EnableCaching注解

@SpringBootApplication
@EnableCaching
public class Application

在application.yml中添加cache内容

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

pom引入依赖

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

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

注解形式使用cache

//查询缓存
@Cacheable(value = "album_search", key = "#type")
public List<String>countStory(String type)

//刷新清除所有缓存
@CacheEvict(value = "album_search", allEntries = true)
public void clearHotSongCache() {}

代码中的value是ehchache.xml中的name

<cache name= "album_search"
           maxElementsInMemory= "1000"
           eternal= "false"
           timeToIdleSeconds= "180"
           timeToLiveSeconds= "86400"
           overflowToDisk= "false" />

整个的ehcache.xml配置
xml中的各种属性,不会的可以百度;着重注意一下是eternal,如果是false就是这个内存是定时的,timeToIdleSeconds, timeToLiveSeconds才会有用。

<ehcache>
    <diskStore path ="java.io.tmpdir"/>
    <defaultCache maxElementsInMemory= "10000"
                   eternal= "false"
                   timeToIdleSeconds= "120"
                   timeToLiveSeconds= "120"
                   overflowToDisk= "true"
                   diskSpoolBufferSizeMB= "30"
                   maxElementsOnDisk= "1000000"
                   diskPersistent= "false"
                   diskExpiryThreadIntervalSeconds="120"
                   memoryStoreEvictionPolicy= "LRU"  />

    <cache name= "album_search"
           maxElementsInMemory= "1000"
           eternal= "false"
           timeToIdleSeconds= "180"
           timeToLiveSeconds= "86400"
           overflowToDisk= "false" />
</ehcache>

第三方cacheutil代码

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

/**
 * <p>description: ehcache缓存工具类
 *    ALBUM_SEARCH是配置在ehcache.xml中的cacheName
 * </p>
 *
 * @author chenrui
 * @since 2018-07-17
 */
public class QQAlbumEhcacheUtil {

    private static CacheManager cacheManager = CacheManager.create();

    private static final String ALBUM_SEARCH = "album_search";

    public static Object get(Object key) {
        Cache cache = cacheManager.getCache(ALBUM_SEARCH);
        if(cache!= null) {
            Element element = cache.get(key);
            if(element != null) {
                return element.getObjectValue();
            }
        }
        return null;
    }

    public static void put(Object key, Object value) {
        Cache cache = cacheManager.getCache(ALBUM_SEARCH);
        if (cache != null) {
            cache.put(new Element(key, value));
        }
    }

    public static boolean remove(Object key) {
        Cache cache = cacheManager.getCache(ALBUM_SEARCH);
        if (cache != null) {
            return cache.remove(key);
        }
        return false;
    }

    public static void main(String[] args) {
        String key = "key";
        String value = "hello";
        QQAlbumEhcacheUtil.put(key, value);
        QQAlbumEhcacheUtil.get(key);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值