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

The following examples show how to use javax.cache.Cache#replace() . 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: PolicyCacheManagerImpl.java    From carbon-device-mgt with Apache License 2.0 6 votes vote down vote up
@Override
public void updatePolicy(Policy policy) {

    Cache<Integer, List<Policy>> lCache = getPolicyListCache();
    if (lCache.containsKey(1)) {
        List<Policy> cachedPolicy = lCache.get(1);
        Iterator iterator = cachedPolicy.iterator();
        while (iterator.hasNext()) {
            Policy pol = (Policy) iterator.next();
            if (pol.getId() == policy.getId()) {
                iterator.remove();
                break;
            }
        }
        cachedPolicy.add(policy);
        lCache.replace(1, cachedPolicy);
    }

}
 
Example 2
Source File: PolicyCacheManagerImpl.java    From carbon-device-mgt with Apache License 2.0 6 votes vote down vote up
@Override
public void removePolicy(int policyId) {

    Cache<Integer, List<Policy>> lCache = getPolicyListCache();
    if (lCache.containsKey(1)) {
        List<Policy> cachedPolicy = lCache.get(1);
        Iterator iterator = cachedPolicy.iterator();
        while (iterator.hasNext()) {
            Policy pol = (Policy) iterator.next();
            if (pol.getId() == policyId) {
                iterator.remove();
                break;
            }
        }
        lCache.replace(1, cachedPolicy);
    }
}
 
Example 3
Source File: DeviceCacheManagerImpl.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
@Override
public void updateDeviceInCache(DeviceIdentifier deviceIdentifier, Device device, int tenantId) {
    Cache<DeviceCacheKey, Device> lCache = DeviceManagerUtil.getDeviceCache();
    if (lCache != null) {
        DeviceCacheKey cacheKey = getCacheKey(deviceIdentifier, tenantId);
        if (lCache.containsKey(cacheKey)) {
            lCache.replace(cacheKey, device);
        }
    }
}
 
Example 4
Source File: PlatformDotNetEntityFrameworkCacheExtension.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Tries to set the global cleanup node id to current node.
 *
 * @param grid Grid.
 * @param metaCache Meta cache.
 *
 * @return True if successfully set the flag indicating that current node performs the cleanup; otherwise false.
 */
private boolean trySetGlobalCleanupFlag(Ignite grid, final Cache<CleanupNodeId, UUID> metaCache) {
    final UUID localNodeId = grid.cluster().localNode().id();

    while (true) {
        // Get the node performing cleanup.
        UUID nodeId = metaCache.get(CLEANUP_NODE_ID);

        if (nodeId == null) {
            if (metaCache.putIfAbsent(CLEANUP_NODE_ID, localNodeId))
                return true;  // Successfully reserved cleanup to local node.

            // Failed putIfAbsent: someone else may have started cleanup. Retry the check.
            continue;
        }

        if (nodeId.equals(localNodeId))
            return false;  // Current node already performs cleanup.

        if (grid.cluster().node(nodeId) != null)
            return false;  // Another node already performs cleanup and is alive.

        // Node that performs cleanup has disconnected.
        if (metaCache.replace(CLEANUP_NODE_ID, nodeId, localNodeId))
            return true;  // Successfully replaced disconnected node id with our id.

        // Replace failed: someone else started cleanup.
        return false;
    }
}
 
Example 5
Source File: IteratorTest.java    From blazingcache with Apache License 2.0 5 votes vote down vote up
@Test
public void testConditionalReplace() throws Exception {
    CachingProvider cachingProvider = Caching.getCachingProvider();
    Properties p = new Properties();
    try (CacheManager cacheManager = cachingProvider.getCacheManager(cachingProvider.getDefaultURI(), cachingProvider.getDefaultClassLoader(), p)) {
        Cache<Long, String> cache = cacheManager.createCache("test", new MutableConfiguration().setTypes(Long.class, String.class));
        BlazingCacheCache<Long, String> blazingCache = cache.unwrap(BlazingCacheCache.class);
        long hitCount = 0;
        long missCount = 0;
        long putCount = 0;

        boolean result = cache.replace(1L, "MissingNoReplace", "NewValue");
        missCount++;
        assertFalse(result);
        assertEquals((int) missCount, (int) blazingCache.getStatisticsMXBean().getCacheMisses());
        assertEquals((int) hitCount, (int) blazingCache.getStatisticsMXBean().getCacheHits());
        assertEquals((int) putCount, (int) blazingCache.getStatisticsMXBean().getCachePuts());
        assertFalse(cache.containsKey(1L));

        cache.put(1l, "Sooty");
        putCount++;
        assertEquals((int) putCount, (int) blazingCache.getStatisticsMXBean().getCachePuts());

        assertTrue(cache.containsKey(1L));
        result = cache.replace(1L, "Sooty", "Replaced");
        hitCount++;
        putCount++;
        assertTrue(result);
        assertEquals((int) missCount, (int) blazingCache.getStatisticsMXBean().getCacheMisses());
        assertEquals((int) hitCount, (int) blazingCache.getStatisticsMXBean().getCacheHits());
        assertEquals((int) putCount, (int) blazingCache.getStatisticsMXBean().getCachePuts());

        result = cache.replace(1L, "Sooty", "InvalidReplace");
        hitCount++;
        assertFalse(result);
        assertEquals((int) missCount, (int) blazingCache.getStatisticsMXBean().getCacheMisses());
        assertEquals((int) hitCount, (int) blazingCache.getStatisticsMXBean().getCacheHits());
        assertEquals((int) putCount, (int) blazingCache.getStatisticsMXBean().getCachePuts());
    }
}
 
Example 6
Source File: CacheExpiryTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void replaceShouldCallGetExpiryForModifiedEntry() {
  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);

  cache.containsKey(1);

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

  // verify case that replace does not occur so no expiry policy called
  boolean result = cache.replace(1, 1);

  assertFalse(result);

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

  cache.put(1, 1);

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

  result = cache.replace(1, 2);

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

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

    cache.containsKey(1);

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

    boolean result = cache.replace(1, 1, 2);

    assertFalse(result);
    assertThat(expiryPolicy.getCreationCount(), is(0));
    assertThat(expiryPolicy.getAccessCount(), is(0));
    assertThat(expiryPolicy.getUpdatedCount(), is(0));

    cache.put(1, 1);
    assertTrue(expiryPolicy.getCreationCount() >= 1);
    assertThat(expiryPolicy.getAccessCount(), is(0));
    assertThat(expiryPolicy.getUpdatedCount(), is(0));
    expiryPolicy.resetCount();

    // verify case when entry exist for key, but oldValue is incorrect. So replacement does not happen.
    // this counts as an access of entry referred to by key.
    result = cache.replace(1, 2, 5);

    assertFalse(result);
    assertThat(expiryPolicy.getCreationCount(), is(0));
    assertTrue(expiryPolicy.getAccessCount() >= 1);
    assertThat(expiryPolicy.getUpdatedCount(), is(0));
    expiryPolicy.resetCount();

    // verify the modify case when replace does succeed.
    result = cache.replace(1, 1, 2);

    assertTrue(result);
    assertThat(expiryPolicy.getCreationCount(), is(0));
    assertThat(expiryPolicy.getAccessCount(), is(0));
    assertTrue(expiryPolicy.getUpdatedCount() >= 1);
    expiryPolicy.resetCount();
}
 
Example 8
Source File: CacheExpiryTest.java    From cache2k with Apache License 2.0 4 votes vote down vote up
@Test
public void replaceSpecificShouldCallGetExpiry() {
  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);

  cache.containsKey(1);

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

  boolean result = cache.replace(1, 1, 2);

  assertFalse(result);
  assertThat(expiryPolicy.getCreationCount(), is(0));
  assertThat(expiryPolicy.getAccessCount(), is(0));
  assertThat(expiryPolicy.getUpdatedCount(), is(0));

  cache.put(1, 1);
  assertThat(expiryPolicy.getCreationCount(), greaterThanOrEqualTo(1));
  assertThat(expiryPolicy.getAccessCount(), is(0));
  assertThat(expiryPolicy.getUpdatedCount(), is(0));
  expiryPolicy.resetCount();

  // verify case when entry exist for key, but oldValue is incorrect. So replacement does not happen.
  // this counts as an access of entry referred to by key.
  result = cache.replace(1, 2, 5);

  assertFalse(result);
  assertThat(expiryPolicy.getCreationCount(), is(0));
  assertThat(expiryPolicy.getAccessCount(), greaterThanOrEqualTo(1));
  assertThat(expiryPolicy.getUpdatedCount(), is(0));
  expiryPolicy.resetCount();

  // verify the modify case when replace does succeed.
  result = cache.replace(1, 1, 2);

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