Spring Ehcache:如何在缓存另一个值时逐出一个缓存值?

如何解决Spring Ehcache:如何在缓存另一个值时逐出一个缓存值?

我的层级结构是账户 -> 投资组合 -> 区域 -> 办公室 -> 条目,所以当我更新区域和区域缓存时,我需要移除所有账户和投资组合缓存值,因为它们现在已经过时了。

这是我的 xml 文件


<?xml version="1.0" encoding="UTF-8"?>
<config
        xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
        xmlns='http://www.ehcache.org/v3'
        xsi:schemaLocation="
            http://www.ehcache.org/v3
            http://www.ehcache.org/schema/ehcache-core-3.7.xsd">

    <persistence directory="spring-boot-ehcache/cache" />

    <cache-template name="default" >
        <listeners>
            <listener>
                <class>guru.springframework.ehcache.config.CacheLogger</class>
                <event-firing-mode>ASYNCHRONOUS</event-firing-mode>
                <event-ordering-mode>UnorDERED</event-ordering-mode>
                <events-to-fire-on>CREATED</events-to-fire-on>
                <events-to-fire-on>EXPIRED</events-to-fire-on>
                <events-to-fire-on>evictED</events-to-fire-on>
            </listener>
        </listeners>

        <resources>
            <heap>1000</heap>
            <offheap unit="MB">10</offheap>
            <disk persistent="true" unit="MB">20</disk>
        </resources>
    </cache-template>

    <cache alias="accountCache" uses-template="default">
        <key-type>java.lang.String</key-type>
        <value-type>AccountDto</value-type>
    </cache>

    <cache alias="entryCache" uses-template="default">
        <key-type>java.lang.String</key-type>
        <value-type>EntryDto</value-type>
    </cache>
    <cache alias="entriesFromOfficeCache" uses-template="default">
        <key-type>java.lang.String</key-type>
        <value-type>java.util.ArrayList</value-type>
    </cache>
    <cache alias="entriesBySource" uses-template="default">
        <key-type>java.lang.String</key-type>
        <value-type>java.util.ArrayList</value-type>
    </cache>

    <cache alias="officeCache" uses-template="default">
        <key-type>java.lang.String</key-type>
        <value-type>OfficeDto</value-type>
    </cache>
    <cache alias="usersOfficesCache" uses-template="default">
        <key-type>java.lang.String</key-type>
        <value-type>java.util.ArrayList</value-type>
    </cache>

    <cache alias="portfolioCache" uses-template="default">
        <key-type>java.lang.String</key-type>
        <value-type>PortfolioDto</value-type>
    </cache>
    <cache alias="portfoliosByTagCache" uses-template="default">
        <key-type>java.lang.String</key-type>
        <value-type>java.util.ArrayList</value-type>
    </cache>
    <cache alias="portfoliosByUserId" uses-template="default">
        <key-type>java.lang.String</key-type>
        <value-type>java.util.ArrayList</value-type>
    </cache>

    <cache alias="regionCache" uses-template="default">
        <key-type>java.lang.String</key-type>
        <value-type>RegionDto</value-type>
    </cache>
    <cache alias="allRegionsForUserCache" uses-template="default">
        <key-type>java.lang.String</key-type>
        <value-type>java.util.ArrayList</value-type>
    </cache>

</config>



..这是我的配置文件

package com.cs29.api.config;

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableCaching
public class EhcacheConfig {
}


以及可缓存方法的示例:

    @Override
    @Cacheable(value = "accountCache",key = "#accountId")
    public AccountDto getAccount(String accountId) {
        Optional<Account> account = getAccountFromrepository(accountId);
        if (account.isEmpty()) {
            String errorMessage = String.format(
                    "AccountService Could not retrieve account with account id: %s",accountId);
            ApiApplication.logger.error(errorMessage);
            throw new NoSuchElementException(errorMessage);
        }
        ApiApplication.logger.info("AccountService retrieved user with account id: " + accountId);
        return AccountMapper.toAccountDto(account.get());
    }


我是新来的 - 如果解释不当,我深表歉意。

提前致谢!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?