springehcache:当缓存了另一个值时,如何逐出一个缓存值?

6ju8rftf  于 2021-07-23  发布在  Java
关注(0)|答案(0)|浏览(327)

我的层次结构是account->portfolios->regions->offices->entries,所以当我更新缓存的regions和regions时,我需要逐出所有account和portfolio缓存的值,因为它们现在已经过时了。
这是我的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());
    }

我是新来的-如果解释得不好,我道歉。
提前谢谢!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题