如何在 Spring-Mvc 中缓存带有两个参数的方法

How to Cache a Method with two parameters in Spring-Mvc

我想将交易的金额字段转换为另一种货币,我决定使用带有@Cacheable 注释的缓存机制。在这个可缓存的方法中,我调用了一个 rest api 方法来获取货币汇率。根据我将缓存周期设置为 60 秒并始终发送相同的参数,程序为每个即将到来的事务执行可缓存的方法范围。

我想查看每分钟的日志或调用不同的参数,因此我使用了 log.error()。

CurrencyConverterService.java

@Service
public class CurrencyConverterService {
    private Logger log = LogManager.getLogger(CurrencyConverterService.class);
    @Autowired
    private ICurrencyService currencyService;

    int counter=0;

    @Cacheable(value = "allCurrency")
    public Float getCurrencyRate(String targetCurrCode, String baseCurrCode){
        log.error("getCurrencyRate called " +targetCurrCode +" - "+baseCurrCode+" - counter "+ counter+" Times");
        counter++;
        Float rate = currencyService.calculateParity(targetCurrCode, baseCurrCode);
        return rate;
    }

    public BigDecimal currencyConverter(Object value, String targetCurrCode, String baseCurrCode){
        float rate;

        if (baseCurrCode.equals(targetCurrCode)){
            rate = 1.0f;
        }else{
            rate = getCurrencyRate(targetCurrCode, baseCurrCode);
        }
        return ((BigDecimal)value).multiply(new BigDecimal(rate));
    }
}

Demo.java

@Service
public class Demo{
    @Autowired
    public ApplicationContext appContext;
    public CurrencyConverterService currencyConverterService;

    public void getCurrencyFromAPI(Object value, String targetCurrCode, String baseCurrCode){
        ....
        currencyConverterService = (CurrencyConverterService) appContext.getBean("currencyConverterService");
        currencyConverterService.currencyConverter(value, targetCurrCode, baseCurrCode);
        ....

    }
}

ehcache.xml

<ehcache
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
        updateCheck="false" monitoring="off" dynamicConfig="true">

    <diskStore path="java.io.tmpdir"/>

    <cache
            name="allCurrency"
            eternal="false"
            maxElementsInMemory="200"
            maxElementsOnDisk="10000"
            timeToIdleSeconds="600"
            timeToLiveSeconds="600"
            overflowToDisk="true"
            diskExpiryThreadIntervalSeconds="86400"
            memoryStoreEvictionPolicy="LFU"/>
</ehcache>

如果我在四分钟内发送 1000 笔交易。我看到下面的输出;

getCurrencyRate calleddd EUR-USD - 计数器 1 次
getCurrencyRate calleddd EUR-USD - 计数器 2 次
...
getCurrencyRate calleddd EUR-USD - 计数器 999 次
getCurrencyRate calleddd EUR-USD - 计数器 1000 次

但我想看到下面的样子;

getCurrencyRate calleddd EUR-USD - 计数器 1 次
getCurrencyRate calleddd EUR-USD - 计数器 2 次
getCurrencyRate calleddd EUR-USD - 计数器 3 次
getCurrencyRate calleddd EUR-USD - 计数器 4 次

感谢您的帮助。

我解决了这个问题。如果我像上面一样在 class 中从其他方法调用缓存方法,则不起作用。如果我从其他 classes 调用直接缓存的方法,则工作正常。我应该像下面这样使用;

Demo.java

@Service
public class Demo{
    @Autowired
    public ApplicationContext appContext;
    public CurrencyConverterService currencyConverterService;

    public void getCurrencyFromAPI(Object value, String targetCurrCode, String baseCurrCode){
        ....
        currencyConverterService = (CurrencyConverterService) appContext.getBean("currencyConverterService");
        currencyConverter(value, targetCurrCode, baseCurrCode);
        ....

    }

    public BigDecimal currencyConverter(Object value, String targetCurrCode, String baseCurrCode){
        float rate;

        if (baseCurrCode.equals(targetCurrCode)){
            rate = 1.0f;
        }else{
            rate = currencyConverterService.getCurrencyRate(targetCurrCode, baseCurrCode);
        }
        return ((BigDecimal)value).multiply(new BigDecimal(rate));
    }

}

CurrencyConverterService.java

@Service
public class CurrencyConverterService {
    private Logger log = LogManager.getLogger(CurrencyConverterService.class);
    @Autowired
    private ICurrencyService currencyService;

    int counter=0;

    @Cacheable(value = "allCurrency")
    public Float getCurrencyRate(String targetCurrCode, String baseCurrCode){
        log.error("getCurrencyRate called " +targetCurrCode +" - "+baseCurrCode+" - counter "+ counter+" Times");
        counter++;
        Float rate = currencyService.calculateParity(targetCurrCode, baseCurrCode);
        return rate;
    }
}