springboot本地缓存ehcache存取数据,cacheable缓存

直接上代码,原来自己看

1、在resource下新建ehcache.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"
         updateCheck="false">
    <diskStore path="java.io.tmpdir/Tmp_EhCache" />

    <!-- defaultCache,是默认的缓存策略 -->
    <!-- 如果你指定的缓存策略没有找到,那么就用这个默认的缓存策略 -->
    <!-- external:缓存对象是否一直存在,如果设置为true的话,那么timeout就没有效果,缓存就会一直存在,一般默认就是false -->
    <!-- maxElementsInMemory:内存中可以缓存多少个缓存条目 -->
    <!-- overflowToDisk:如果内存不够的时候,是否溢出到磁盘 -->
    <!-- diskPersistent:是否启用磁盘持久化的机制,在jvm崩溃的时候和重启之间 -->
    <!-- timeToIdleSeconds:对象最大的闲置的时间,如果超出闲置的时间,可能就会过期  单位:秒 当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大-->
    <!-- timeToLiveSeconds:对象最多存活的时间  单位:秒 当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是存活时间无穷大-->
    <!-- memoryStoreEvictionPolicy:当缓存数量达到了最大的指定条目数的时候,需要采用一定的算法,从缓存中清除一批数据,LRU,最近最少使用算法,最近一段时间内,最少使用的那些数据,就被干掉了 -->
    <defaultCache
            eternal="false"
            maxElementsInMemory="1000"
            overflowToDisk="false"
            diskPersistent="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            memoryStoreEvictionPolicy="LRU" />

    <!-- 手动指定的缓存策略 -->
    <!-- 对不同的数据,缓存策略可以在这里配置多种 -->
    <cache
            name="institutionNo"
            eternal="false"
            maxElementsInMemory="1000"
            overflowToDisk="false"
            diskPersistent="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            memoryStoreEvictionPolicy="LRU" />
</ehcache>

2、新建EhcacheUtil.java通用方法类

package com.goldpac.ocs.business.merchantservice.config;
import java.net.URL;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class EhcacheUtil {
    private static final String path = "/ehcache.xml";

    private URL url;

    private CacheManager manager;

    private static EhcacheUtil ehCache;

    private EhcacheUtil(String path) {
    url = getClass().getResource(path);
    manager = CacheManager.create(url);
    }

    public static EhcacheUtil getInstance() {
        if (ehCache== null) {
        ehCache= new EhcacheUtil(path);
        }
        return ehCache;
}

    public void put(String cacheName, String key, Object value) {
        Cache cache = manager.getCache(cacheName);
        Element element = new Element(key, value);
        cache.put(element);
    }

    public Object get(String cacheName, String key) {
        Cache cache = manager.getCache(cacheName);
        Element element = cache.get(key);
    return element == null ? null : element.getObjectValue();
    }

    public Cache get(String cacheName) {
    return manager.getCache(cacheName);
}

    public void remove(String cacheName, String key) {
        Cache cache = manager.getCache(cacheName);
        cache.remove(key);
    }
}

3、导入jar包

       <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache-core</artifactId>
            <version>2.4.3</version>
            <scope>compile</scope>
        </dependency>

4、service中调用

//获取缓存数据institutionNo为ehcache.xml中配置的name,no为获取缓存的数据key(我的no为变量)

Object institutionNo = EhcacheUtil.getInstance().get("institutionNo", no);

//添加缓存数据institutionNo为ehcache.xml中配置的name,no为缓存的数据的key,no2为缓存的value(我的no为变量)

EhcacheUtil.getInstance().put("institutionNo", no, no2);


另附一篇cacheable缓存,我的理解是,没有就执行方法并缓存结果,有就直接返回缓存,,

https://blog.csdn.net/zhuyu19911016520/article/details/81946202

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot提供了对本地缓存的支持,可以使用不同的缓存实现来提高应用程序的性能和响应速度。以下是使用Spring Boot进行本地缓存的步骤和示例代码: 1. 在pom.xml文件添加Spring Boot缓存依赖项[^1]: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> ``` 2. 在应用程序的配置文件(例如application.yml)配置缓存: ```yaml spring: cache: type: <cache-provider> ``` 其,`<cache-provider>`可以是以下之一: - `simple`:使用SimpleCacheManager作为缓存提供程序,适用于开发和测试环境。 - `caffeine`:使用Caffeine作为缓存提供程序,适用于高性能和低延迟的应用程序。 - `ehcache`:使用Ehcache作为缓存提供程序,适用于分布式和高可用性的应用程序。 - `redis`:使用Redis作为缓存提供程序,适用于分布式和高可用性的应用程序。 3. 在需要缓存的方法上添加`@Cacheable`注解,指定缓存的名称和缓存的键。例如: ```java @Service public class MyService { @Cacheable("myCache") public String getData(String key) { // 从数据库或其他数据源获取数据 return data; } } ``` 4. 运行应用程序并调用带有缓存注解的方法,第一次调用会执行方法体内的逻辑并将结果缓存起来,后续调用相同的方法会直接从缓存获取结果。 这样,你就可以使用Spring Boot进行本地缓存了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值