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

The following examples show how to use javax.cache.Cache#remove() . 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: PermissionTree.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
/**
 * Clears all permission information in current node.
 */
void clear() {
    Cache<PermissionTreeCacheKey, GhostResource<TreeNode>> permissionCache = this.getPermissionTreeCache();
    if (permissionCache != null) {
        write.lock();
        try {
            this.root.clearNodes();
            this.hashValueOfRootNode = -1;
            PermissionTreeCacheKey cacheKey = new PermissionTreeCacheKey(cacheIdentifier, tenantId);
            // TODO Is this clear all?
            permissionCache.remove(cacheKey);
        } finally {
            write.unlock();
        }
    }

}
 
Example 2
Source File: JSRExamplesTest.java    From blazingcache with Apache License 2.0 6 votes vote down vote up
@Test
public void testJSRExample1() {

    CachingProvider cachingProvider = Caching.getCachingProvider();
    Properties p = new Properties();
    try (CacheManager cacheManager = cachingProvider.getCacheManager(cachingProvider.getDefaultURI(), cachingProvider.getDefaultClassLoader(), p)) {
        MutableConfiguration<String, Integer> config
                = new MutableConfiguration<String, Integer>()
                .setTypes(String.class, Integer.class)
                .setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(ONE_HOUR))
                .setStatisticsEnabled(true);

        Cache<String, Integer> cache = cacheManager.createCache("simpleCache", config);

        String key = "key";
        Integer value1 = 1;
        cache.put("key", value1);
        Integer value2 = cache.get(key);
        assertEquals(value1, value2);
        cache.remove(key);
        assertNull(cache.get(key));
    }
}
 
Example 3
Source File: ParametersInURITest.java    From blazingcache with Apache License 2.0 6 votes vote down vote up
@Test
public void testParametersInURI() throws URISyntaxException {

    CachingProvider cachingProvider = Caching.getCachingProvider();
    Properties p = new Properties();
    try (CacheManager cacheManager = cachingProvider.getCacheManager(new URI(cachingProvider.getDefaultURI() + "?secret=testsecret"), cachingProvider.getDefaultClassLoader(), p)) {
        MutableConfiguration<String, Integer> config
                = new MutableConfiguration<String, Integer>()
                .setTypes(String.class, Integer.class)
                .setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(ONE_HOUR))
                .setStatisticsEnabled(true);

        Cache<String, Integer> cache = cacheManager.createCache("simpleCache", config);
        BlazingCacheManager manager = cacheManager.unwrap(BlazingCacheManager.class);
        assertEquals("testsecret", manager.getProperties().get("blazingcache.secret"));

        String key = "key";
        Integer value1 = 1;
        cache.put("key", value1);
        Integer value2 = cache.get(key);
        assertEquals(value1, value2);
        cache.remove(key);
        assertNull(cache.get(key));
    }
}
 
Example 4
Source File: BaseCache.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
/**
 * Clears a cache entry.
 *
 * @param key Key to clear cache.
 */
public void clearCacheEntry(K key) {

    if (!isEnabled()) {
        return;
    }

    try {
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext
                .getThreadLocalCarbonContext();
        carbonContext.setTenantId(MultitenantConstants.SUPER_TENANT_ID);
        carbonContext.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
        Cache<K, V> cache = getBaseCache();
        if (cache != null) {
            cache.remove(key);
        }
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
}
 
Example 5
Source File: ReadOnlyLDAPUserStoreManager.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
/**
 * Removes the cache entry given the user name.
 *
 * @param userName the User name to remove.
 * @return true if removal was successful.
 */
protected boolean removeFromUserCache(String userName) {
    try {
        Cache<String, LdapName> userDnCache = createOrGetUserDnCache();
        if (userDnCache == null) {
            // User cache may be null while initializing.
            // Return true as removal result is successful when there is no cache. Nothing was held.
            return true;
        }
        return userDnCache.remove(userName);
    } catch (IllegalStateException e) {
        // There is no harm ignoring the removal, as the cache(local) is already is of no use.
        log.error("Error occurred while removing User DN from cache having search base : " + userSearchBase, e);
        return true;
    }
}
 
Example 6
Source File: AuthorizationCache.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
/**
 * To clear cache when resource authorization is cleared.
 *
 * @param serverId
 * @param tenantID
 * @param resourceID
 */
public void clearCacheByResource(String serverId, int tenantID,
                                 String resourceID) {
    Cache<AuthorizationKey, AuthorizeCacheEntry> cache = this.getAuthorizationCache();
    // check for null
    if (isCacheNull(cache)) {
        return;
    }

    for (Cache.Entry<AuthorizationKey, AuthorizeCacheEntry> entry : cache) {
        AuthorizationKey authorizationKey = entry.getKey();
        if ((tenantID == (authorizationKey.getTenantId()))
                && (resourceID.equals(authorizationKey.getResourceId()))
                && (serverId == null || serverId.equals(authorizationKey
                .getServerId()))) {
            cache.remove(authorizationKey);
        }
    }

}
 
Example 7
Source File: BaseCache.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
/**
 * Clears a cache entry.
 *
 * @param key Key to clear cache.
 */
public void clearCacheEntry(K key) {
    if (!isEnabled()) {
        return;
    }

    try {
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext
                .getThreadLocalCarbonContext();
        carbonContext.setTenantId(MultitenantConstants.SUPER_TENANT_ID);
        carbonContext.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
        Cache<K, V> cache = getBaseCache();
        if (cache != null) {
            cache.remove(key);
        }
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
}
 
Example 8
Source File: AuthorizationCache.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
/**
 * Clears a given cache entry.
 *
 * @param serverId   unique identifier for carbon server instance
 * @param tenantId   tenant id
 * @param userName   User name to construct the cache key.
 * @param resourceId Resource id to construct the cache key.
 * @param action     Action to construct the cache key.
 */
public void clearCacheEntry(String serverId, int tenantId, String userName,
                            String resourceId, String action) {
    Cache<AuthorizationKey, AuthorizeCacheEntry> cache = this.getAuthorizationCache();
    // check for null
    if (isCacheNull(cache)) {
        return;
    }
    if (!isCaseSensitiveUsername(userName, tenantId)) {
        userName = userName.toLowerCase();
    }

    AuthorizationKey key = new AuthorizationKey(serverId, tenantId,
            userName, resourceId, action);
    if (cache.containsKey(key)) {
        cache.remove(key);
    }

}
 
Example 9
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 10
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 11
Source File: CacheTest.java    From commons-jcs with Apache License 2.0 5 votes vote down vote up
@Test
public void getPut()
{
    final CachingProvider cachingProvider = Caching.getCachingProvider();
    final CacheManager cacheManager = cachingProvider.getCacheManager();
    final Cache<String, String> cache = cacheManager.createCache("default", new MutableConfiguration<String, String>());
    assertFalse(cache.containsKey("foo"));
    cache.put("foo", "bar");
    assertTrue(cache.containsKey("foo"));
    assertEquals("bar", cache.get("foo"));
    cache.remove("foo");
    assertFalse(cache.containsKey("foo"));
    cachingProvider.close();
}
 
Example 12
Source File: APIKeyMgtUtil.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * Remove APIKeyValidationInfoDTO from Key Manager Cache
 *
 * @param cacheKey Key for the Cache Entry to be removed
 */
public static void removeFromKeyManagerCache(String cacheKey) {

    boolean cacheEnabledKeyMgt = APIKeyMgtDataHolder.getKeyCacheEnabledKeyMgt();

    if (cacheKey != null && cacheEnabledKeyMgt) {

        Cache cache = getKeyManagerCache();
        cache.remove(cacheKey);
        log.debug("KeyValidationInfoDTO removed for key : " + cacheKey);
    }
}
 
Example 13
Source File: JDBCTokenStore.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
@Override
public void removeToken(String id) throws TrustException {
    String tokenId = getTokenId(id);
    initDao();
    dbStsDAO.removeToken(tokenId);
    //remove token from cache and send cache invalidation msg
    Cache<String, SerializableToken> tokenCache = getTokenCache();
    if (tokenCache != null && tokenCache.containsKey(tokenId)) {
        tokenCache.remove(tokenId);
    }
}
 
Example 14
Source File: TCKCacheManagerTest.java    From blazingcache with Apache License 2.0 5 votes vote down vote up
@Test
public void removeSpecifiedEntryShouldNotCallExpiryPolicyMethods() {
    CountingExpiryPolicy expiryPolicy = new CountingExpiryPolicy();

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

    boolean result = cache.remove(1, 1);

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

    result = cache.remove(1, 2);

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

    result = cache.remove(1, 1);

    assertTrue(result);
    assertThat(expiryPolicy.getCreationCount(), is(0));
    assertThat(expiryPolicy.getAccessCount(), is(0));
    assertThat(expiryPolicy.getUpdatedCount(), is(0));
}
 
Example 15
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 16
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 17
Source File: InMemoryIdentityDataStore.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
@Override
public void remove(String userName, UserStoreManager userStoreManager) throws IdentityException {

    try {
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID);

        Cache<String, UserIdentityClaimsDO> cache = getCache();
        if (userName == null) {
            return;
        }
        if (userStoreManager instanceof org.wso2.carbon.user.core.UserStoreManager) {
            if (!IdentityUtil.isUserStoreCaseSensitive((org.wso2.carbon.user.core.UserStoreManager) userStoreManager)) {
                if (log.isDebugEnabled()) {
                    log.debug("Case insensitive user store found. Changing username from : " + userName + " to : " +
                            userName.toLowerCase());
                }
                userName = userName.toLowerCase();
            }
        }
        org.wso2.carbon.user.core.UserStoreManager store = (org.wso2.carbon.user.core.UserStoreManager)
                userStoreManager;
        String domainName = store.getRealmConfiguration().getUserStoreProperty(UserCoreConstants.RealmConfig
                .PROPERTY_DOMAIN_NAME);

        cache.remove(domainName + userStoreManager.getTenantId() + userName);
    } catch (UserStoreException e) {
        log.error("Error while obtaining tenant ID from user store manager");
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
}
 
Example 18
Source File: SerializableEntityCache.java    From requery with Apache License 2.0 5 votes vote down vote up
@Override
public void invalidate(Class<?> type, Object key) {
    Cache cache = getCache(type);
    if (cache != null && !cache.isClosed()) {
        cache.remove(key);
    }
}
 
Example 19
Source File: JCacheOAuthDataProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected static <V extends ServerAccessToken> V getToken(Cache<String, V> cache, String key) {
    V token = cache.get(key);
    if (token != null && isExpired(token)) {
        cache.remove(key);
        token = null;
    }
    return token;
}
 
Example 20
Source File: ClaimInvalidationCache.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.
 */
private void clearCacheEntry(String key) {
    Cache<String, Integer> cache = getClaimCache();
    if (cache != null) {
        cache.remove(key);
    }
}