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

The following examples show how to use javax.cache.Cache#containsKey() . 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: JDBCTokenStore.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
@Override
public Token getToken(String id) throws TrustException {

    String tokenId = getTokenId(id);

    Cache<String, SerializableToken> tokenCache = getTokenCache();
    if (tokenCache != null && tokenCache.containsKey(tokenId)) {
        try {
            return STSStoreUtils.getToken((SerializableToken) tokenCache.get(tokenId));
        } catch (XMLStreamException e) {
            throw new TrustException("Failed to get Token from cache", e);
        }
    }
    initDao();
    Token token = dbStsDAO.getToken(tokenId);

    if (token == null) {
        log.debug("Token is not present in cache or database");
    }

    if (tokenCache != null && token != null) {
        tokenCache.put(tokenId, STSStoreUtils.getSerializableToken(token));
    }
    return token;
}
 
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: PolicyCacheManagerImpl.java    From carbon-device-mgt with Apache License 2.0 6 votes vote down vote up
@Override
public void addPolicy(Policy policy) {

    Cache<Integer, List<Policy>> lCache = getPolicyListCache();
    if (lCache.containsKey(1)) {
        List<Policy> cachedPolicy = lCache.get(1);

        for (Policy pol : cachedPolicy) {
            if (pol.getId() == policy.getId()) {
                return;
            }
        }
        cachedPolicy.add(policy);
    }

}
 
Example 4
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 5
Source File: EntitlementBaseCache.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * Clears a cache entry.
 *
 * @param key Key to clear cache.
 */
public void clearCacheEntry(K key) {
    Cache<K, V> cache = getEntitlementCache();
    if (cache != null) {
        if (cache.containsKey(key)) {
            cache.remove(key);
            if (log.isDebugEnabled()) {
                String tenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain();
                log.debug("Cache : " + Entitlement_CACHE_NAME + " entry is removed " + "in tenant domain : " +
                          tenantDomain);
            }
        }
    }
}
 
Example 6
Source File: CacheExpiryTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void putIfAbsentShouldCallGetExpiry() {
  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.putIfAbsent(1, 1);

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

  result = cache.putIfAbsent(1, 2);

  assertFalse(result);
  assertThat(expiryPolicy.getCreationCount(), is(0));
  assertThat(expiryPolicy.getAccessCount(), is(0));
  assertThat(expiryPolicy.getUpdatedCount(), is(0));
}
 
Example 7
Source File: OpenIDBaseCache.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves a cache entry.
 *
 * @param key CacheKey
 * @return Cached entry.
 */
public V getValueFromCache(K key) {
    Cache<K, V> cache = getOpenIDCache();
    if (cache != null && cache.containsKey(key)) {
        return (V) cache.get(key);
    }
    return null;
}
 
Example 8
Source File: CacheInvalidationServiceImpl.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Override
public void invalidateResourceCache(String apiContext, String apiVersion,
                                    ResourceCacheInvalidationDto[] uriTemplates) {

    boolean isTenantFlowStarted = false;
    int tenantDomainIndex = apiContext.indexOf("/t/");
    String tenantDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
    if (tenantDomainIndex != -1) {
        String temp = apiContext.substring(tenantDomainIndex + 3, apiContext.length());
        tenantDomain = temp.substring(0, temp.indexOf('/'));
    }

    try {
        isTenantFlowStarted = startTenantFlow(tenantDomain);
        Cache cache = CacheProvider.getResourceCache();
        if (apiContext.contains(APIConstants.POLICY_CACHE_CONTEXT)) {
            if (log.isDebugEnabled()) {
                log.debug("Cleaning cache for policy update for tenant " + tenantDomain);
            }
            cache.removeAll();
        } else {
            String apiCacheKey = APIUtil.getAPIInfoDTOCacheKey(apiContext, apiVersion);
            if (cache.containsKey(apiCacheKey)) {
                cache.remove(apiCacheKey);
            }
            for (ResourceCacheInvalidationDto uriTemplate : uriTemplates) {
                String resourceVerbCacheKey = APIUtil.getResourceInfoDTOCacheKey(apiContext, apiVersion,
                        uriTemplate.getResourceURLContext(), uriTemplate.getHttpVerb());
                if (cache.containsKey(resourceVerbCacheKey)) {
                    cache.remove(resourceVerbCacheKey);
                }
            }
        }

    } finally {
        if (isTenantFlowStarted) {
            endTenantFlow();
        }
    }
}
 
Example 9
Source File: CacheExpiryTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void containsKeyShouldNotCallExpiryPolicyMethods() {

  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));

  cache.put(1, 1);

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

  cache.containsKey(1);

  assertThat(expiryPolicy.getCreationCount(), is(0));
  assertThat(expiryPolicy.getAccessCount(), is(0));
  assertThat(expiryPolicy.getUpdatedCount(), is(0));
  closeTestCache();
}
 
Example 10
Source File: CacheExpiryTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void getAndRemoveShouldNotCallExpiryPolicyMethods() {
  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);

  // verify case when entry is non-existent
  cache.containsKey(1);

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

  cache.getAndRemove(1);

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

  // verify case when entry exist
  cache.put(1, 1);

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

  int value = cache.getAndRemove(1);
  assertThat(value, is(1));

  assertThat(expiryPolicy.getCreationCount(), is(0));
  assertThat(expiryPolicy.getAccessCount(), is(0));
  assertThat(expiryPolicy.getUpdatedCount(), is(0));
}
 
Example 11
Source File: OpenIDBaseCache.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * Clears a cache entry.
 *
 * @param key Key to clear cache.
 */
public void clearCacheEntry(K key) {
    Cache<K, V> cache = getOpenIDCache();
    if (cache != null && cache.containsKey(key)) {
        cache.remove(key);
    }
}
 
Example 12
Source File: CacheExpiryTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void getAndReplaceShouldCallGetExpiryForModifiedEntry() {
  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));

  cache.getAndReplace(1, 1);

  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();

  int oldValue = cache.getAndReplace(1, 2);

  assertEquals(1, oldValue);
  assertThat(expiryPolicy.getCreationCount(), is(0));
  assertThat(expiryPolicy.getAccessCount(), is(0));
  assertThat(expiryPolicy.getUpdatedCount(), greaterThanOrEqualTo(1));
  expiryPolicy.resetCount();
}
 
Example 13
Source File: DeviceCacheManagerImpl.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
@Override
public void removeDevicesFromCache(List<DeviceCacheKey> deviceList) {
    Cache<DeviceCacheKey, Device> lCache = DeviceManagerUtil.getDeviceCache();
    if (lCache != null) {
        for (DeviceCacheKey cacheKey : deviceList) {
            if (lCache.containsKey(cacheKey)) {
                lCache.remove(cacheKey);
            }
        }
    }
}
 
Example 14
Source File: DeviceCacheManagerImpl.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
@Override
public void removeDeviceFromCache(DeviceIdentifier deviceIdentifier, int tenantId) {
    Cache<DeviceCacheKey, Device> lCache = DeviceManagerUtil.getDeviceCache();
    if (lCache != null) {
        DeviceCacheKey cacheKey = getCacheKey(deviceIdentifier, tenantId);
        if (lCache.containsKey(cacheKey)) {
            lCache.remove(cacheKey);
        }
    }
}
 
Example 15
Source File: DeviceCacheManagerImpl.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
@Override
public void addDeviceToCache(DeviceIdentifier deviceIdentifier, Device device, int tenantId) {
    Cache<DeviceCacheKey, Device> lCache = DeviceManagerUtil.getDeviceCache();
    if (lCache != null) {
        DeviceCacheKey cacheKey = getCacheKey(deviceIdentifier, tenantId);
        if (lCache.containsKey(cacheKey)) {
            this.updateDeviceInCache(deviceIdentifier, device, tenantId);
        } else {
            lCache.put(cacheKey, device);
        }
    }
}
 
Example 16
Source File: CacheExpiryTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void getAndPutShouldCallEitherCreatedOrModifiedExpiryPolicy() {
  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));

  cache.getAndPut(1, 1);

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

  cache.getAndPut(1, 2);

  assertThat(expiryPolicy.getCreationCount(), is(0));
  assertThat(expiryPolicy.getAccessCount(), is(0));
  assertThat(expiryPolicy.getUpdatedCount(), greaterThanOrEqualTo(1));
  expiryPolicy.resetCount();
}
 
Example 17
Source File: RealmCache.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
/**
 * Clears a cache entry.
 *
 * @param key Key to clear cache.
 */
public void clearCacheEntry(RealmCacheKey key) {

    Cache<RealmCacheKey, RealmCacheEntry> cache = getRealmCache();
    if (cache.containsKey(key)) {
        cache.remove(key);
    }
}
 
Example 18
Source File: RealmCache.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves a cache entry.
 *
 * @param key CacheKey
 * @return Cached entry.
 */
public RealmCacheEntry getValueFromCache(RealmCacheKey key) {

    Cache<RealmCacheKey, RealmCacheEntry> cache = getRealmCache();
    if (cache.containsKey(key)) {
        return cache.get(key);
    }
    return null;
}
 
Example 19
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 20
Source File: JDBCTokenStore.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
@Override
public void update(Token token) throws TrustException {
    initDao();
    dbStsDAO.updateToken(token);
    //update the cache is that token present in cache
    Cache<String, SerializableToken> tokenCache = getTokenCache();

    if (tokenCache != null && tokenCache.containsKey(getTokenId(token))) {
        tokenCache.put(getTokenId(token), STSStoreUtils.getSerializableToken(token));
    }
}