Fix putIfAbsent bug in InMemoryCacheStore

pull/137/head
johnniang 2019-03-28 18:25:57 +08:00
parent 58bf3c1d2a
commit 8d04bec3b2
4 changed files with 20 additions and 6 deletions

View File

@ -111,7 +111,6 @@ public abstract class AbstractCacheStore<K, V> implements CacheStore<K, V> {
expireAt = DateUtils.addMilliseconds(now, Long.valueOf(millis).intValue()); expireAt = DateUtils.addMilliseconds(now, Long.valueOf(millis).intValue());
} }
// Build cache wrapper // Build cache wrapper
CacheWrapper<V> cacheWrapper = new CacheWrapper<>(); CacheWrapper<V> cacheWrapper = new CacheWrapper<>();
cacheWrapper.setCreateAt(now); cacheWrapper.setCreateAt(now);

View File

@ -2,6 +2,7 @@ package cc.ryanc.halo.cache;
import lombok.*; import lombok.*;
import java.io.Serializable;
import java.util.Date; import java.util.Date;
/** /**
@ -10,11 +11,11 @@ import java.util.Date;
* @author johnniang * @author johnniang
*/ */
@Data @Data
@EqualsAndHashCode
@ToString @ToString
@EqualsAndHashCode
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
public class CacheWrapper<V> { class CacheWrapper<V> implements Serializable {
/** /**
* Cache data * Cache data

View File

@ -42,10 +42,24 @@ public class InMemoryCacheStore extends StringCacheStore {
Assert.hasText(key, "Cache key must not be blank"); Assert.hasText(key, "Cache key must not be blank");
Assert.notNull(cacheWrapper, "Cache wrapper must not be null"); Assert.notNull(cacheWrapper, "Cache wrapper must not be null");
log.debug("Preparing to put key: [{}], value: [{}]", key, cacheWrapper);
// Put the cache wrapper // Put the cache wrapper
CacheWrapper<String> putCacheWrapper = cacheContainer.putIfAbsent(key, cacheWrapper); CacheWrapper<String> putCacheWrapper = cacheContainer.putIfAbsent(key, cacheWrapper);
return cacheWrapper.equals(putCacheWrapper); if (putCacheWrapper == null) {
putCacheWrapper = cacheWrapper;
}
boolean isEqual = cacheWrapper.equals(putCacheWrapper);
if (isEqual) {
log.debug("Put successfully");
} else {
log.warn("Failed to put the cache, because the key: [{}] has been present already", key);
}
return isEqual;
} }
@Override @Override

View File

@ -38,7 +38,7 @@ public class HaloConfiguration {
* @return Cors filter registration bean * @return Cors filter registration bean
*/ */
@Bean @Bean
FilterRegistrationBean<CorsFilter> corsFilter() { public FilterRegistrationBean<CorsFilter> corsFilter() {
FilterRegistrationBean<CorsFilter> corsFilter = new FilterRegistrationBean<>(); FilterRegistrationBean<CorsFilter> corsFilter = new FilterRegistrationBean<>();
corsFilter.setOrder(Ordered.HIGHEST_PRECEDENCE + 10); corsFilter.setOrder(Ordered.HIGHEST_PRECEDENCE + 10);
@ -54,7 +54,7 @@ public class HaloConfiguration {
* @return Log filter registration bean * @return Log filter registration bean
*/ */
@Bean @Bean
FilterRegistrationBean<LogFilter> logFilter() { public FilterRegistrationBean<LogFilter> logFilter() {
FilterRegistrationBean<LogFilter> logFilter = new FilterRegistrationBean<>(); FilterRegistrationBean<LogFilter> logFilter = new FilterRegistrationBean<>();
logFilter.setOrder(Ordered.HIGHEST_PRECEDENCE + 9); logFilter.setOrder(Ordered.HIGHEST_PRECEDENCE + 9);