【发布时间】:2022-01-05 09:44:13
【问题描述】:
我正在尝试将数据模型添加到 redis 缓存。当我取出模型时,我得到了异常
org.springframework.core.convert.ConversionFailedException: Failed to convert from type [byte[]] to type [java.util.concurrent.atomic.AtomicInteger] for value '{53}'; nested exception is java.lang.IllegalArgumentException: Cannot convert String [5] to target class [java.util.concurrent.atomic.AtomicInteger]] with root cause
模型是
@RedisHash(value = "ThottleRate", timeToLive = 5)
public class ThottleRate implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private String url;
private AtomicInteger rate;
public ThottleRate(String url, int rate) {
super();
this.url = url;
this.rate = new AtomicInteger(rate);
}
public boolean isAllowed(){
if(this.rate.decrementAndGet() <= 0) {
return false;
}
return true;
}
调用代码是
try {
option = throttleRateRepository.findById(url);
ThottleRate rate = option.get();
allow = rate.isAllowed();
} catch(NoSuchElementException e) {
throttleRateRepository.save(new ThottleRate(url, 5));
allow = true;
}
我想要做的是限制功能,使用 redis 缓存。它有一个存在的时间,并且在此期间可以访问一个 url。
但是当调用是
option = throttleRateRepository.findById(url);
这会引发 IllegalArgumentException
这似乎是使用 redis 缓存的最简单方法,具有生存时间和多个速率
【问题讨论】:
标签: java spring redis throttling spring-data-redis