org.infinispan.commons.api.BasicCache Java Examples

The following examples show how to use org.infinispan.commons.api.BasicCache. 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: DistributedCacheConcurrentWritesTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
        BasicCache<String, SessionEntityWrapper<UserSessionEntity>> cache1 = createCache("node1");
        BasicCache<String, SessionEntityWrapper<UserSessionEntity>> cache2 = createCache("node2");

        // NOTE: This setup requires infinispan servers to be up and running on localhost:12232 and localhost:13232
//        BasicCache<String, SessionEntityWrapper<UserSessionEntity>> cache1 = createRemoteCache("node1");
//        BasicCache<String, SessionEntityWrapper<UserSessionEntity>> cache2 = createRemoteCache("node2");

        try {
            testConcurrentPut(cache1, cache2);
        } finally {

            // Kill JVM
            cache1.stop();
            cache2.stop();
            stopMgr(cache1);
            stopMgr(cache2);

            System.out.println("Managers killed");
        }
    }
 
Example #2
Source File: InfinispanCodeToTokenStoreProvider.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public void put(UUID codeId, int lifespanSeconds, Map<String, String> codeData) {
    ActionTokenValueEntity tokenValue = new ActionTokenValueEntity(codeData);

    try {
        BasicCache<UUID, ActionTokenValueEntity> cache = codeCache.get();
        cache.put(codeId, tokenValue, lifespanSeconds, TimeUnit.SECONDS);
    } catch (HotRodClientException re) {
        // No need to retry. The hotrod (remoteCache) has some retries in itself in case of some random network error happened.
        if (logger.isDebugEnabled()) {
            logger.debugf(re, "Failed when adding code %s", codeId);
        }

        throw re;
    }
}
 
Example #3
Source File: InfinispanCodeToTokenStoreProvider.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public Map<String, String> remove(UUID codeId) {
    try {
        BasicCache<UUID, ActionTokenValueEntity> cache = codeCache.get();
        ActionTokenValueEntity existing = cache.remove(codeId);
        return existing == null ? null : existing.getNotes();
    } catch (HotRodClientException re) {
        // No need to retry. The hotrod (remoteCache) has some retries in itself in case of some random network error happened.
        // In case of lock conflict, we don't want to retry anyway as there was likely an attempt to remove the code from different place.
        if (logger.isDebugEnabled()) {
            logger.debugf(re, "Failed when removing code %s", codeId);
        }

        return null;
    }
}
 
Example #4
Source File: InfinispanSingleUseTokenStoreProvider.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public boolean putIfAbsent(String tokenId, int lifespanInSeconds) {
    ActionTokenValueEntity tokenValue = new ActionTokenValueEntity(null);

    // Rather keep the items in the cache for a bit longer
    lifespanInSeconds = lifespanInSeconds + 10;

    try {
        BasicCache<String, ActionTokenValueEntity> cache = tokenCache.get();
        ActionTokenValueEntity existing = cache.putIfAbsent(tokenId, tokenValue, lifespanInSeconds, TimeUnit.SECONDS);
        return existing == null;
    } catch (HotRodClientException re) {
        // No need to retry. The hotrod (remoteCache) has some retries in itself in case of some random network error happened.
        // In case of lock conflict, we don't want to retry anyway as there was likely an attempt to use the token from different place.
        logger.debugf(re, "Failed when adding token %s", tokenId);

        return false;
    }

}
 
Example #5
Source File: HotrodCacheTest.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void mockRemoveWithValue(final BasicCache<Object, Object> cache, final String key, final Object value,
        final boolean removeOperationResult) {
    final org.infinispan.client.hotrod.RemoteCache<Object, Object> remoteCache = (RemoteCache<Object, Object>) cache;
    if (removeOperationResult) {
        final long version = 1;
        when(remoteCache.getWithMetadataAsync(eq(key)))
                .thenReturn(CompletableFuture.completedFuture(new MetadataValueImpl<>(-1, -1, -1, -1,
                        version, value)));
        when(remoteCache.removeWithVersionAsync(eq(key), eq(version)))
                .thenReturn(CompletableFuture.completedFuture(true));
    } else {
        when(remoteCache.getWithMetadataAsync(eq(key))).thenReturn(CompletableFuture.completedFuture(null));
    }
}
 
Example #6
Source File: DistributedCacheConcurrentWritesTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
private static void printStats(BasicCache cache) {
    if (cache instanceof Cache) {
        Cache cache1 = (Cache) cache;

        JChannel channel = ((JGroupsTransport)cache1.getAdvancedCache().getRpcManager().getTransport()).getChannel();

        System.out.println("Sent MB: " + channel.getSentBytes() / 1000000 + ", sent messages: " + channel.getSentMessages() + ", received MB: " + channel.getReceivedBytes() / 1000000 +
                ", received messages: " + channel.getReceivedMessages());
    } else {
        Map<String, String> stats = ((RemoteCache) cache).stats().getStatsMap();
        System.out.println("Stats: " + stats);
    }
}
 
Example #7
Source File: DistributedCacheConcurrentWritesTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
private static void stopMgr(BasicCache cache) {
    if (cache instanceof Cache) {
        ((Cache) cache).getCacheManager().stop();
    } else {
        ((RemoteCache) cache).getRemoteCacheManager().stop();
    }
}
 
Example #8
Source File: DistributedCacheConcurrentWritesTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public static BasicCache<String, SessionEntityWrapper<UserSessionEntity>> createRemoteCache(String nodeName) {
    int port = ("node1".equals(nodeName)) ? 12232 : 13232;

    org.infinispan.client.hotrod.configuration.ConfigurationBuilder builder = new org.infinispan.client.hotrod.configuration.ConfigurationBuilder();
    org.infinispan.client.hotrod.configuration.Configuration cfg = builder
            .addServer().host("localhost").port(port)
            .version(ProtocolVersion.PROTOCOL_VERSION_26)
            .build();
    RemoteCacheManager mgr = new RemoteCacheManager(cfg);
    return mgr.getCache(InfinispanConnectionProvider.USER_SESSION_CACHE_NAME);
}
 
Example #9
Source File: HotrodCacheTest.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void verifyRemoveWithValue(final BasicCache<Object, Object> cache, final String key,
        final Object value, final boolean expectedRemoveOperationResult) {
    final org.infinispan.client.hotrod.RemoteCache<Object, Object> remoteCache = (RemoteCache<Object, Object>) cache;
    verify(remoteCache).getWithMetadataAsync(key);
    if (expectedRemoveOperationResult) {
        verify(remoteCache).removeWithVersionAsync(eq(key), anyLong());
    }
}
 
Example #10
Source File: InfinispanSingleUseTokenStoreProvider.java    From keycloak with Apache License 2.0 4 votes vote down vote up
public InfinispanSingleUseTokenStoreProvider(KeycloakSession session, Supplier<BasicCache<String, ActionTokenValueEntity>> actionKeyCache) {
    this.session = session;
    this.tokenCache = actionKeyCache;
}
 
Example #11
Source File: CrossDCAwareCacheFactory.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Override
BasicCache<String, Serializable> getCache() {
    return workCache;
}
 
Example #12
Source File: CrossDCAwareCacheFactory.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Override
BasicCache<String, Serializable> getCache() {
    // Flags are per-invocation!
    return remoteCache.withFlags(Flag.FORCE_RETURN_VALUE);
}
 
Example #13
Source File: InfinispanCodeToTokenStoreProvider.java    From keycloak with Apache License 2.0 4 votes vote down vote up
public InfinispanCodeToTokenStoreProvider(KeycloakSession session, Supplier<BasicCache<UUID, ActionTokenValueEntity>> actionKeyCache) {
    this.session = session;
    this.codeCache = actionKeyCache;
}
 
Example #14
Source File: DistributedCacheConcurrentWritesTest.java    From keycloak with Apache License 2.0 4 votes vote down vote up
private static void testConcurrentPut(BasicCache<String, SessionEntityWrapper<UserSessionEntity>> cache1,
                                      BasicCache<String, SessionEntityWrapper<UserSessionEntity>> cache2) throws InterruptedException {

    // Create workers for concurrent write and start them
    Worker worker1 = new Worker(1, cache1);
    Worker worker2 = new Worker(2, cache2);

    long start = System.currentTimeMillis();

    System.out.println("Started clustering test");

    worker1.start();
    //worker1.join();
    worker2.start();

    worker1.join();
    worker2.join();

    long took = System.currentTimeMillis() - start;

    System.out.println("Test finished. Took: " + took + " ms. Cache size: " + cache1.size());

    // JGroups statistics
    printStats(cache1);
}
 
Example #15
Source File: DistributedCacheConcurrentWritesTest.java    From keycloak with Apache License 2.0 4 votes vote down vote up
public Worker(int threadId, BasicCache<String, SessionEntityWrapper<UserSessionEntity>> cache) {
    this.cache = cache;
    this.startIndex = (threadId - 1) * (ITEMS_IN_BATCH * BATCHES_PER_WORKER);
    setName("th-" + threadId);
}
 
Example #16
Source File: DistributedCacheConcurrentWritesTest.java    From keycloak with Apache License 2.0 4 votes vote down vote up
public static BasicCache<String, SessionEntityWrapper<UserSessionEntity>> createCache(String nodeName) {
    EmbeddedCacheManager mgr = createManager(nodeName);
    Cache<String, SessionEntityWrapper<UserSessionEntity>> cache = mgr.getCache(InfinispanConnectionProvider.USER_SESSION_CACHE_NAME);
    return cache;
}
 
Example #17
Source File: ShiroCache.java    From java-platform with Apache License 2.0 4 votes vote down vote up
public ShiroCache(final BasicCache<K, V> nativeCache) {
	Assert.notNull(nativeCache, "A non-null Infinispan cache implementation is required");
	this.nativeCache = nativeCache;
}
 
Example #18
Source File: SubstituteInfinispanManager.java    From camel-quarkus with Apache License 2.0 4 votes vote down vote up
@Substitute
public <K, V> BasicCache<K, V> getCache(String cacheName) {
    return ObjectHelper.isEmpty(cacheName) ? cacheContainer.getCache() : cacheContainer.getCache(cacheName);
}
 
Example #19
Source File: CrossDCAwareCacheFactory.java    From keycloak with Apache License 2.0 votes vote down vote up
abstract BasicCache<String, Serializable> getCache();