Skip to content

Commit

Permalink
Fix JCache invoke triggering a load on an expired entry (fixes #187)
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-manes committed Sep 20, 2017
1 parent 5c77fae commit ac00474
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 14 deletions.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.2-rc-2-bin.zip
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void remove() {
@Override
public void setValue(V value) {
requireNonNull(value);
if (action != Action.CREATED) {
if ((action != Action.CREATED) && (action != Action.LOADED)) {
action = exists() ? Action.UPDATED : Action.CREATED;
}
this.value = value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.benmanes.caffeine.jcache;

import com.github.benmanes.caffeine.jcache.configuration.CaffeineConfiguration;
import org.testng.annotations.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.sameInstance;

import javax.cache.Cache;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.sameInstance;
import org.testng.annotations.Test;

public class CacheProxyTest extends AbstractJCacheTest {
import com.github.benmanes.caffeine.jcache.configuration.CaffeineConfiguration;

/**
* @author github.com/kdombeck (Ken Dombeck)
*/
public final class CacheProxyTest extends AbstractJCacheTest {

@Override
protected CaffeineConfiguration<Integer, Integer> getConfiguration() {
return new CaffeineConfiguration<>();
}

@Test(expectedExceptions = IllegalArgumentException.class)
public void unwrap_fail() {
Expand All @@ -19,11 +41,7 @@ public void unwrap_fail() {
public void unwrap() {
assertThat(jcache.unwrap(Cache.class), sameInstance(jcache));
assertThat(jcache.unwrap(CacheProxy.class), sameInstance(jcache));
assertThat(jcache.unwrap(com.github.benmanes.caffeine.cache.Cache.class), sameInstance(jcache.cache));
}

@Override
protected CaffeineConfiguration<Integer, Integer> getConfiguration() {
return new CaffeineConfiguration<>();
assertThat(jcache.unwrap(com.github.benmanes.caffeine.cache.Cache.class),
sameInstance(jcache.cache));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* Copyright 2017 Ben Manes. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.benmanes.caffeine.jcache.processor;

import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toMap;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.OptionalLong;
import java.util.concurrent.TimeUnit;

import javax.cache.Cache.Entry;
import javax.cache.expiry.CreatedExpiryPolicy;
import javax.cache.expiry.Duration;
import javax.cache.integration.CacheLoader;
import javax.cache.integration.CacheLoaderException;
import javax.cache.integration.CacheWriter;
import javax.cache.processor.MutableEntry;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import com.github.benmanes.caffeine.jcache.AbstractJCacheTest;
import com.github.benmanes.caffeine.jcache.configuration.CaffeineConfiguration;
import com.google.common.base.MoreObjects;
import com.google.common.collect.Streams;

/**
* @author chrisstockton (Chris Stockton)
* @author [email protected] (Ben Manes)
*/
public final class EntryProcessorTest extends AbstractJCacheTest {
private final Map<Integer, Integer> map = new HashMap<>();

private int loads;

@BeforeMethod
public void beforeMethod() {
map.clear();
loads = 0;
}

@Override
protected CaffeineConfiguration<Integer, Integer> getConfiguration() {
CaffeineConfiguration<Integer, Integer> config = new CaffeineConfiguration<>();
config.setExpiryPolicyFactory(() -> new CreatedExpiryPolicy(Duration.FIVE_MINUTES));
config.setCacheLoaderFactory(MapLoader::new);
config.setCacheWriterFactory(MapWriter::new);
config.setTickerFactory(() -> ticker::read);
config.setMaximumSize(OptionalLong.of(200));
config.setWriteThrough(true);
config.setReadThrough(true);
return config;
}

@Test
public void reload() {
jcache.invoke(KEY_1, this::process);
assertThat(loads, is(1));

ticker.advance(1, TimeUnit.MINUTES);
jcache.invoke(KEY_1, this::process);
assertThat(loads, is(1));

// Expire the entry
ticker.advance(5, TimeUnit.MINUTES);

jcache.invoke(KEY_1, this::process);
assertThat(loads, is(2));

ticker.advance(1, TimeUnit.MINUTES);
jcache.invoke(KEY_1, this::process);
assertThat(loads, is(2));
}

private Object process(MutableEntry<Integer, Integer> entry, Object... arguments) {
Integer value = MoreObjects.firstNonNull(entry.getValue(), 0);
entry.setValue(++value);
return null;
}

final class MapWriter implements CacheWriter<Integer, Integer> {

@Override
public void write(Entry<? extends Integer, ? extends Integer> entry) {
map.put(entry.getKey(), entry.getValue());
}

@Override
public void writeAll(Collection<Entry<? extends Integer, ? extends Integer>> entries) {
entries.forEach(this::write);
}

@Override
public void delete(Object key) {
map.remove(key);
}

@Override
public void deleteAll(Collection<?> keys) {
keys.forEach(this::delete);
}
}

final class MapLoader implements CacheLoader<Integer, Integer> {

@Override
public Integer load(Integer key) throws CacheLoaderException {
loads++;
return map.get(key);
}

@Override
public Map<Integer, Integer> loadAll(Iterable<? extends Integer> keys) {
return Streams.stream(keys).collect(toMap(identity(), this::load));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public final class Cache2kPolicy implements Policy {
private final PolicyStats policyStats;
private final int maximumSize;

@SuppressWarnings("deprecation")
public Cache2kPolicy(Config config) {
Logger logger = LogManager.getLogManager().getLogger("");
Level level = logger.getLevel();
Expand Down

0 comments on commit ac00474

Please sign in to comment.