Java Code Examples for javax.cache.Cache#invoke()

The following examples show how to use javax.cache.Cache#invoke() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CacheWriterTest.java    From blazingcache with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldWriteThroughUsingInvoke_remove_nonExistingEntry() {
    RecordingCacheWriter<Integer, String> cacheWriter = new RecordingCacheWriter<>();
    CachingProvider cachingProvider = Caching.getCachingProvider();
    Properties p = new Properties();
    try (CacheManager cacheManager = cachingProvider.getCacheManager(cachingProvider.getDefaultURI(), cachingProvider.getDefaultClassLoader(), p)) {
        MutableConfiguration<Integer, String> config
                = new MutableConfiguration<Integer, String>()
                .setTypes(Integer.class, String.class)
                .setCacheWriterFactory(new FactoryBuilder.SingletonFactory<>(cacheWriter))
                .setWriteThrough(true);

        Cache<Integer, String> cache = cacheManager.createCache("simpleCache", config);
        assertEquals(0, cacheWriter.getWriteCount());
        assertEquals(0, cacheWriter.getDeleteCount());

        cache.invoke(1, new RemoveEntryProcessor<Integer, String, Object>());
        assertEquals(0, cacheWriter.getWriteCount());
        assertEquals(1, cacheWriter.getDeleteCount());
        assertFalse(cacheWriter.hasWritten(1));
    }
}
 
Example 2
Source File: CacheWriterTest.java    From blazingcache with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldWriteThroughUsingInvoke_setValue_CreateEntryThenRemove() {
    RecordingCacheWriter<Integer, String> cacheWriter = new RecordingCacheWriter<>();
    CachingProvider cachingProvider = Caching.getCachingProvider();
    Properties p = new Properties();
    try (CacheManager cacheManager = cachingProvider.getCacheManager(cachingProvider.getDefaultURI(), cachingProvider.getDefaultClassLoader(), p)) {
        MutableConfiguration<Integer, String> config
                = new MutableConfiguration<Integer, String>()
                .setTypes(Integer.class, String.class)
                .setCacheWriterFactory(new FactoryBuilder.SingletonFactory<>(cacheWriter))
                .setWriteThrough(true);

        Cache<Integer, String> cache = cacheManager.createCache("simpleCache", config);
        assertEquals(0, cacheWriter.getWriteCount());
        assertEquals(0, cacheWriter.getDeleteCount());

        EntryProcessor processors[]
                = new EntryProcessor[]{
                    new AssertNotPresentEntryProcessor(null),
                    new SetEntryProcessor<Integer, String>("Gudday World"),
                    new RemoveEntryProcessor<Integer, String, Object>(true)
                };
        cache.invoke(1, new CombineEntryProcessor<Integer, String>(processors));
        assertEquals(0, cacheWriter.getWriteCount());
        assertEquals(0, cacheWriter.getDeleteCount());
        assertTrue(!cacheWriter.hasWritten(1));
        assertTrue(cache.get(1) == null);
        assertFalse(cache.containsKey(1));
    }
}
 
Example 3
Source File: TCKCacheManagerTest.java    From blazingcache with Apache License 2.0 5 votes vote down vote up
@Test
public void invokeSetValueShouldCallGetExpiry() {

    CountingExpiryPolicy expiryPolicy = new CountingExpiryPolicy();

    MutableConfiguration<Integer, Integer> config = new MutableConfiguration<>();
    config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(expiryPolicy));
    Cache<Integer, Integer> cache = getCacheManager().createCache("test-4", config);

    final Integer key = 123;
    final Integer setValue = 456;
    final Integer modifySetValue = 789;

    // verify create
    EntryProcessor processors[]
            = new EntryProcessor[]{
                new AssertNotPresentEntryProcessor(null),
                new SetEntryProcessor<Integer, Integer>(setValue),
                new GetEntryProcessor<Integer, Integer>()
            };
    Object[] result = (Object[]) cache.invoke(key, new CombineEntryProcessor(processors));

    assertEquals(result[1], setValue);
    assertEquals(result[2], setValue);

    // expiry called should be for create, not for the get or modify.
    // Operations get combined in entry processor and only net result should be expiryPolicy method called.
    assertTrue(expiryPolicy.getCreationCount() >= 1);
    assertThat(expiryPolicy.getAccessCount(), is(0));
    assertThat(expiryPolicy.getUpdatedCount(), is(0));
    expiryPolicy.resetCount();

    // verify modify
    Integer resultValue = cache.invoke(key, new SetEntryProcessor<Integer, Integer>(modifySetValue));
    assertEquals(modifySetValue, resultValue);

    assertThat(expiryPolicy.getCreationCount(), is(0));
    assertThat(expiryPolicy.getAccessCount(), is(0));
    assertTrue(expiryPolicy.getUpdatedCount() >= 1);
}
 
Example 4
Source File: TCKCacheManagerTest.java    From blazingcache with Apache License 2.0 5 votes vote down vote up
@Test
public void invokeGetValueShouldCallGetExpiry() {
    CountingExpiryPolicy expiryPolicy = new CountingExpiryPolicy();

    MutableConfiguration<Integer, Integer> config = new MutableConfiguration<>();
    config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(expiryPolicy));
    Cache<Integer, Integer> cache = getCacheManager().createCache("test-dfd", config);

    final Integer key = 123;
    final Integer setValue = 456;

    // verify non-access to non-existent entry does not call getExpiryForAccessedEntry. no read-through scenario.
    Integer resultValue = cache.invoke(key, new GetEntryProcessor<Integer, Integer>());

    assertEquals(null, resultValue);
    assertThat(expiryPolicy.getCreationCount(), is(0));
    assertThat(expiryPolicy.getAccessCount(), is(0));
    assertThat(expiryPolicy.getUpdatedCount(), is(0));

    // verify access to existing entry.
    resultValue = cache.invoke(key, new SetEntryProcessor<Integer, Integer>(setValue));

    assertEquals(resultValue, setValue);
    assertTrue(expiryPolicy.getCreationCount() >= 1);
    assertThat(expiryPolicy.getAccessCount(), is(0));
    assertThat(expiryPolicy.getUpdatedCount(), is(0));
    expiryPolicy.resetCount();

    resultValue = cache.invoke(key, new GetEntryProcessor<Integer, Integer>());

    assertEquals(setValue, resultValue);
    assertThat(expiryPolicy.getCreationCount(), is(0));
    assertTrue(expiryPolicy.getAccessCount() >= 1);
    assertThat(expiryPolicy.getUpdatedCount(), is(0));
}
 
Example 5
Source File: CacheExpiryTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void invokeMultiSetValueShouldCallGetExpiry() {

  CountingExpiryPolicy expiryPolicy = new CountingExpiryPolicy();
  expiryPolicyServer.setExpiryPolicy(expiryPolicy);

  MutableConfiguration<Integer, Integer> config = new MutableConfiguration<>();
  config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(expiryPolicyClient));
  Cache<Integer, Integer> cache = getCacheManager().createCache(getTestCacheName(), config);

  final Integer key = 123;
  final Integer setValue = 456;
  final Integer modifySetValue = 789;

  // verify create
  EntryProcessor processors[] =
      new EntryProcessor[]{
          new AssertNotPresentEntryProcessor(null),
          new SetEntryProcessor<Integer, Integer>(111),
          new SetEntryProcessor<Integer, Integer>(setValue),
          new GetEntryProcessor<Integer, Integer>()
      };
  Object[] result = (Object[]) cache.invoke(key, new CombineEntryProcessor(processors));

  assertEquals(result[1], 111);
  assertEquals(result[2], setValue);
  assertEquals(result[3], setValue);

  // expiry called should be for create, not for the get or modify.
  // Operations get combined in entry processor and only net result should be expiryPolicy method called.
  assertThat(expiryPolicy.getCreationCount(), greaterThanOrEqualTo(1));
  assertThat(expiryPolicy.getAccessCount(), greaterThanOrEqualTo(0));
  assertThat(expiryPolicy.getUpdatedCount(), greaterThanOrEqualTo(0));
}
 
Example 6
Source File: CacheExpiryTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void invokeGetValueShouldCallGetExpiry() {
  CountingExpiryPolicy expiryPolicy = new CountingExpiryPolicy();
  expiryPolicyServer.setExpiryPolicy(expiryPolicy);

  MutableConfiguration<Integer, Integer> config = new MutableConfiguration<>();
  config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(expiryPolicyClient));
  Cache<Integer, Integer> cache = getCacheManager().createCache(getTestCacheName(), config);

  final Integer key = 123;
  final Integer setValue = 456;

  // verify non-access to non-existent entry does not call getExpiryForAccessedEntry. no read-through scenario.
  Integer resultValue = cache.invoke(key, new GetEntryProcessor<Integer, Integer>());

  assertEquals(null, resultValue);
  assertThat(expiryPolicy.getCreationCount(), is(0));
  assertThat(expiryPolicy.getAccessCount(), is(0));
  assertThat(expiryPolicy.getUpdatedCount(), is(0));

  // verify access to existing entry.
  resultValue = cache.invoke(key, new SetEntryProcessor<Integer, Integer>(setValue));

  assertEquals(resultValue, setValue);
  assertThat(expiryPolicy.getCreationCount(), greaterThanOrEqualTo(1));
  assertThat(expiryPolicy.getAccessCount(), is(0));
  assertThat(expiryPolicy.getUpdatedCount(), is(0));
  expiryPolicy.resetCount();

  resultValue = cache.invoke(key, new GetEntryProcessor<Integer, Integer>());

  assertEquals(setValue, resultValue);
  assertThat(expiryPolicy.getCreationCount(), is(0));
  assertThat(expiryPolicy.getAccessCount(), greaterThanOrEqualTo(1));
  assertThat(expiryPolicy.getUpdatedCount(), is(0));
}
 
Example 7
Source File: CacheExpiryTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void invokeGetValueWithReadThroughForNonExistentEntryShouldCallGetExpiryForCreatedEntry() 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();
    expiryPolicyServer.setExpiryPolicy(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 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();
  }
}
 
Example 8
Source File: CacheExpiryTest.java    From cache2k with Apache License 2.0 4 votes vote down vote up
@Test
public void invokeSetValueShouldCallGetExpiry() {

  CountingExpiryPolicy expiryPolicy = new CountingExpiryPolicy();
  expiryPolicyServer.setExpiryPolicy(expiryPolicy);

  MutableConfiguration<Integer, Integer> config = new MutableConfiguration<>();
  config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(expiryPolicyClient));
  Cache<Integer, Integer> cache = getCacheManager().createCache(getTestCacheName(), config);

  final Integer key = 123;
  final Integer setValue = 456;
  final Integer modifySetValue = 789;

  // verify create
  EntryProcessor processors[] =
      new EntryProcessor[]{
          new AssertNotPresentEntryProcessor(null),
          new SetEntryProcessor<Integer, Integer>(setValue),
          new GetEntryProcessor<Integer, Integer>()
      };
  Object[] result = (Object[]) cache.invoke(key, new CombineEntryProcessor(processors));

  assertEquals(result[1], setValue);
  assertEquals(result[2], setValue);

  // expiry called should be for create, not for the get or modify.
  // Operations get combined in entry processor and only net result should be expiryPolicy method called.
  assertThat(expiryPolicy.getCreationCount(), greaterThanOrEqualTo(1));
  assertThat(expiryPolicy.getAccessCount(), is(0));
  assertThat(expiryPolicy.getUpdatedCount(), is(0));
  expiryPolicy.resetCount();

  // verify modify
  Integer resultValue = cache.invoke(key, new SetEntryProcessor<Integer, Integer>(modifySetValue));
  assertEquals(modifySetValue, resultValue);

  assertThat(expiryPolicy.getCreationCount(), is(0));
  assertThat(expiryPolicy.getAccessCount(), is(0));
  assertThat(expiryPolicy.getUpdatedCount(), greaterThanOrEqualTo(1));
}