Skip to content

Commit

Permalink
remove force argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Roiocam committed Sep 4, 2024
1 parent fb305d6 commit e226421
Showing 1 changed file with 8 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public MultiLevelCache(Cache... caches) throws CacheConfigException {
CacheConfig lastConfig = caches[caches.length - 1].config();
config = new MultiLevelCacheConfig<>();
config.setCaches(Arrays.asList(caches));
config.setExpireAfterWriteInMillis(lastConfig.getExpireAfterWriteInMillis());
config.setCacheNullValue(lastConfig.isCacheNullValue());
}

Expand Down Expand Up @@ -60,14 +61,12 @@ public MultiLevelCacheConfig<K, V> config() {

@Override
public CacheResult PUT(K key, V value) {
// let each level cache decide the expiration time
return PUT(key, value, 0, null);
return PUT(key, value, config().getExpireAfterWriteInMillis(), TimeUnit.MILLISECONDS);
}

@Override
public CacheResult PUT_ALL(Map<? extends K, ? extends V> map) {
// let each level cache decide the expiration time
return PUT_ALL(map, 0, null);
return PUT_ALL(map, config().getExpireAfterWriteInMillis(), TimeUnit.MILLISECONDS);
}

@Override
Expand Down Expand Up @@ -111,7 +110,7 @@ private void checkResultAndFillUpperCache(K key, int i, CacheValueHolder<V> h) {
*/
long restTtl = currentExpire - now;
if (restTtl > 0) {
PUT_caches(i, key, h.getValue(), restTtl, TimeUnit.MILLISECONDS,false);
PUT_caches(i, key, h.getValue(), restTtl, TimeUnit.MILLISECONDS);
}
}
}
Expand Down Expand Up @@ -147,7 +146,7 @@ protected MultiGetResult<K, V> do_GET_ALL(Set<? extends K> keys) {

@Override
protected CacheResult do_PUT(K key, V value, long expireAfterWrite, TimeUnit timeUnit) {
return PUT_caches(caches.length, key, value, expireAfterWrite, timeUnit, true);
return PUT_caches(caches.length, key, value, expireAfterWrite, timeUnit);
}

@Override
Expand All @@ -165,20 +164,16 @@ protected CacheResult do_PUT_ALL(Map<? extends K, ? extends V> map, long expireA
return new CacheResult(future);
}

private CacheResult PUT_caches(int lastIndex, K key, V value, long expire, TimeUnit timeUnit, boolean isForce) {
private CacheResult PUT_caches(int lastIndex, K key, V value, long expire, TimeUnit timeUnit) {
CompletableFuture<ResultData> future = CompletableFuture.completedFuture(null);
for (int i = 0; i < lastIndex; i++) {
Cache cache = caches[i];
CacheResult r;
if (timeUnit == null) {
r = cache.PUT(key, value);
} else {
// only use argument expiration time when user force and expiration minus than configuration
if (isForce || expire <= cache.config().getExpireAfterWriteInMillis()) {
r = cache.PUT(key, value, expire, timeUnit);
} else {
r = cache.PUT(key, value);
}
long useExpireTime = Math.min(expire, cache.config().getExpireAfterWriteInMillis());
r = cache.PUT(key, value, useExpireTime, timeUnit);
}
future = combine(future, r);
}
Expand Down

0 comments on commit e226421

Please sign in to comment.