我的层次结构是帐户 -> 投资组合 -> 区域 -> 办公室 -> 条目,因此当我更新区域和区域被缓存时,我需要逐出所有帐户和投资组合缓存的值,因为它们现在已经过时了。
这是我的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());
}
我是新来的——如果解释得不好,我深表歉意。
提前致谢!