use of javax.cache.configuration.MutableConfiguration in project cache2k by cache2k.
the class CacheExpiryTest method invokeAllReadThroughEnabledGetOnNonExistentEntry.
@Test
public void invokeAllReadThroughEnabledGetOnNonExistentEntry() throws IOException {
// establish and open a CacheLoaderServer to handle cache
// cache loading requests from a CacheLoaderClient
// this cacheLoader just returns the key as the value.
RecordingCacheLoader<Integer> recordingCacheLoader = new RecordingCacheLoader<>();
try (CacheLoaderServer<Integer, Integer> cacheLoaderServer = new CacheLoaderServer<>(10000, recordingCacheLoader)) {
cacheLoaderServer.open();
// establish a CacheLoaderClient that a Cache can use for loading entries
// (via the CacheLoaderServer)
CacheLoaderClient<Integer, Integer> cacheLoader = new CacheLoaderClient<>(cacheLoaderServer.getInetAddress(), cacheLoaderServer.getPort());
CountingExpiryPolicy expiryPolicy = new CountingExpiryPolicy();
setupExpiryPolicyClientServer(expiryPolicy);
MutableConfiguration<Integer, Integer> config = new MutableConfiguration<>();
config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(expiryPolicyClient));
config.setCacheLoaderFactory(FactoryBuilder.factoryOf(cacheLoader));
config.setReadThrough(true);
Cache<Integer, Integer> cache = getCacheManager().createCache(getTestCacheName(), config);
final Integer INITIAL_KEY = 123;
final Integer MAX_KEY_VALUE = INITIAL_KEY + 4;
// set keys to read through
Set<Integer> keys = new HashSet<>();
for (int key = INITIAL_KEY; key <= MAX_KEY_VALUE; key++) {
keys.add(key);
}
// verify read-through of getValue of non-existent entries
cache.invokeAll(keys, new GetEntryProcessor<Integer, Integer>());
assertThat(expiryPolicy.getCreationCount(), greaterThanOrEqualTo(keys.size()));
assertThat(expiryPolicy.getAccessCount(), is(0));
assertThat(expiryPolicy.getUpdatedCount(), is(0));
expiryPolicy.resetCount();
closeTestCache();
}
}
use of javax.cache.configuration.MutableConfiguration in project cache2k by cache2k.
the class CacheExpiryTest method invokeGetValueWithReadThroughForNonExistentEntryShouldCallGetExpiryForCreatedEntry.
@Test
public void invokeGetValueWithReadThroughForNonExistentEntryShouldCallGetExpiryForCreatedEntry() throws IOException {
CountingExpiryPolicy expiryPolicy = new CountingExpiryPolicy();
setupExpiryPolicyClientServer(expiryPolicy);
// establish and open a CacheLoaderServer to handle cache
// cache loading requests from a CacheLoaderClient
// this cacheLoader just returns the key as the value.
RecordingCacheLoader<Integer> recordingCacheLoader = new RecordingCacheLoader<>();
try (CacheLoaderServer<Integer, Integer> cacheLoaderServer = new CacheLoaderServer<>(10000, recordingCacheLoader)) {
cacheLoaderServer.open();
// establish a CacheLoaderClient that a Cache can use for loading entries
// (via the CacheLoaderServer)
CacheLoaderClient<Integer, Integer> cacheLoader = new CacheLoaderClient<>(cacheLoaderServer.getInetAddress(), cacheLoaderServer.getPort());
MutableConfiguration<Integer, Integer> config = new MutableConfiguration<>();
config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(expiryPolicyClient));
config.setCacheLoaderFactory(FactoryBuilder.factoryOf(cacheLoader));
config.setReadThrough(true);
Cache<Integer, Integer> cache = getCacheManager().createCache(getTestCacheName(), config);
final Integer key = 123;
final Integer recordingCacheLoaderValue = key;
// verify create when read through is enabled and entry was non-existent in cache.
Integer resultValue = cache.invoke(key, new GetEntryProcessor<Integer, Integer>());
assertEquals(recordingCacheLoaderValue, resultValue);
assertTrue(recordingCacheLoader.hasLoaded(key));
assertThat(expiryPolicy.getCreationCount(), greaterThanOrEqualTo(1));
assertThat(expiryPolicy.getAccessCount(), is(0));
assertThat(expiryPolicy.getUpdatedCount(), is(0));
closeTestCache();
}
}
use of javax.cache.configuration.MutableConfiguration in project cache2k by cache2k.
the class CacheExpiryTest method invokeAllSetValueShouldCallGetExpiry.
@Test
public void invokeAllSetValueShouldCallGetExpiry() {
CountingExpiryPolicy expiryPolicy = new CountingExpiryPolicy();
setupExpiryPolicyClientServer(expiryPolicy);
MutableConfiguration<Integer, Integer> config = new MutableConfiguration<>();
config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(expiryPolicyClient));
Cache<Integer, Integer> cache = getCacheManager().createCache(getTestCacheName(), config);
final Integer INITIAL_KEY = 123;
final Integer MAX_KEY_VALUE = INITIAL_KEY + 4;
final Integer setValue = 456;
final Integer modifySetValue = 789;
// set half of the keys so half of invokeAll will be modify and rest will be create.
Set<Integer> keys = new HashSet<>();
int createdCount = 0;
for (int key = INITIAL_KEY; key <= MAX_KEY_VALUE; key++) {
keys.add(key);
if (key <= MAX_KEY_VALUE - 2) {
cache.put(key, setValue);
createdCount++;
}
}
assertThat(expiryPolicy.getCreationCount(), greaterThanOrEqualTo(createdCount));
assertThat(expiryPolicy.getAccessCount(), is(0));
assertThat(expiryPolicy.getUpdatedCount(), is(0));
expiryPolicy.resetCount();
// verify modify or create
cache.invokeAll(keys, new SetEntryProcessor<Integer, Integer>(setValue));
assertThat(expiryPolicy.getCreationCount(), greaterThanOrEqualTo(keys.size() - createdCount));
assertThat(expiryPolicy.getAccessCount(), is(0));
assertThat(expiryPolicy.getUpdatedCount(), greaterThanOrEqualTo(createdCount));
expiryPolicy.resetCount();
// verify accessed
cache.invokeAll(keys, new GetEntryProcessor<Integer, Integer>());
assertThat(expiryPolicy.getCreationCount(), is(0));
assertThat(expiryPolicy.getAccessCount(), greaterThanOrEqualTo(keys.size()));
assertThat(expiryPolicy.getUpdatedCount(), is(0));
}
use of javax.cache.configuration.MutableConfiguration in project cache2k by cache2k.
the class TypesTest method genericsEnforcementAndStricterTypeEnforcement.
/**
* What happens when you:
*
* 1) declare using generics with a super class
* 2) declare using configuration with a sub class
*
* Set generics to Identifier and Dog
* Bypass generics with a raw MutableConfiguration but set runtime to Identifier and Hound
*
* The configuration checking gets done on put.
*/
@Test
public void genericsEnforcementAndStricterTypeEnforcement() {
// configure the cache
MutableConfiguration config = new MutableConfiguration<>();
config.setTypes(Identifier.class, Hound.class);
Cache<Identifier, Dog> cache = cacheManager.createCache(cacheName, config);
// Types are restricted and types are enforced
// Cannot put in wrong types
// cache.put(1, "something");
// can put in
cache.put(pistachio.getName(), pistachio);
// can put in with generics but possibly not with configuration as not a hound
try {
cache.put(tonto.getName(), tonto);
} catch (ClassCastException e) {
// expected but not mandatory. The RI throws these.
}
// cannot get out wrong key types
// assertNotNull(cache.get(1));
assertNotNull(cache.get(pistachio.getName()));
// not necessarily
// assertNotNull(cache.get(tonto.getName()));
// cannot remove wrong key types
// assertTrue(cache.remove(1));
assertTrue(cache.remove(pistachio.getName()));
// not necessarily
// assertTrue(cache.remove(tonto.getName()));
}
use of javax.cache.configuration.MutableConfiguration in project cache2k by cache2k.
the class TypesTest method simpleAPINoGenericsAndNoTypeEnforcementStoreByReference.
/**
* What happens when you:
*
* 1) don't declare using generics and
* 2) don't specify types during configuration.
*/
@Test
public void simpleAPINoGenericsAndNoTypeEnforcementStoreByReference() {
MutableConfiguration config = new MutableConfiguration().setStoreByValue(false);
Cache cache = cacheManager.createCache(cacheName, config);
Identifier2 one = new Identifier2("1");
// can put different things in
cache.put(one, "something");
cache.put(pistachio.getName(), pistachio);
cache.put(tonto.getName(), tonto);
cache.put(bonzo.getName(), bonzo);
cache.put(juno.getName(), juno);
cache.put(talker.getName(), talker);
try {
cache.put(skinny.getName(), skinny);
} catch (Exception e) {
// not serializable expected
}
// can get them out
Identifier2 one_ = new Identifier2("1");
Assert.assertEquals(one, one_);
Assert.assertEquals(one.hashCode(), one_.hashCode());
assertNotNull(cache.get(one_));
assertNotNull(cache.get(one));
assertNotNull(cache.get(pistachio.getName()));
// can remove them
assertTrue(cache.remove(one));
assertTrue(cache.remove(pistachio.getName()));
}
Aggregations