org.infinispan.AdvancedCache Java Examples

The following examples show how to use org.infinispan.AdvancedCache. 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: Bucket4jInfinispanApplication.java    From spring-cloud-zuul-ratelimit with Apache License 2.0 5 votes vote down vote up
@Bean
@Qualifier("RateLimit")
public ReadWriteMap<String, GridBucketState> map() {
    DefaultCacheManager cacheManager = new DefaultCacheManager();
    cacheManager.defineConfiguration("rateLimit", new ConfigurationBuilder().build());
    AdvancedCache<String, GridBucketState> cache = cacheManager.<String, GridBucketState>getCache("rateLimit").getAdvancedCache();
    FunctionalMapImpl<String, GridBucketState> functionalMap = FunctionalMapImpl.create(cache);
    return ReadWriteMapImpl.create(functionalMap);
}
 
Example #2
Source File: Bucket4jInfinispanRateLimiterTest.java    From spring-cloud-zuul-ratelimit with Apache License 2.0 5 votes vote down vote up
@BeforeEach
public void setUp() {
    MockitoAnnotations.initMocks(this);
    DefaultCacheManager cacheManager = new DefaultCacheManager();
    cacheManager.defineConfiguration("rateLimit", new ConfigurationBuilder().build());
    AdvancedCache<String, GridBucketState> cache = cacheManager.<String, GridBucketState>getCache("rateLimit").getAdvancedCache();
    FunctionalMapImpl<String, GridBucketState> functionalMap = FunctionalMapImpl.create(cache);
    FunctionalMap.ReadWriteMap<String, GridBucketState> readWriteMap = ReadWriteMapImpl.create(functionalMap);
    target = new Bucket4jInfinispanRateLimiter(readWriteMap);
}
 
Example #3
Source File: AbstractSessionCacheCommand.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
protected void doRunCacheCommand(KeycloakSession session, Cache<String, SessionEntityWrapper> cache) {
    String id = getArg(1);
    cache = ((AdvancedCache) cache).withFlags(Flag.CACHE_MODE_LOCAL);
    UserSessionEntity userSession = (UserSessionEntity) cache.get(id).getEntity();
    printSession(id, userSession);
}
 
Example #4
Source File: InfinispanAuthenticationSessionProvider.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public void removeExpired(RealmModel realm) {
    log.debugf("Removing expired sessions");

    int expired = Time.currentTime() - RealmInfoUtil.getDettachedClientSessionLifespan(realm);

    final AdvancedCache<String, RootAuthenticationSessionEntity> localCache = CacheDecorators.localCache(cache);
    int localCacheSizePre = localCache.size();
    // Each cluster node cleanups just local sessions, which are those owned by himself (+ few more taking l1 cache into account)
    localCache.entrySet()
            .removeIf(RootAuthenticationSessionPredicate.create(realm.getId()).expired(expired));

    log.debugf("Removed %d expired authentication sessions for realm '%s'", localCache.size() - localCacheSizePre, realm.getName());
}
 
Example #5
Source File: InfinispanFunctional.java    From infinispan-simple-tutorials with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
   DefaultCacheManager cacheManager = new DefaultCacheManager();
   cacheManager.defineConfiguration("local", new ConfigurationBuilder().build());
   AdvancedCache<String, String> cache = cacheManager.<String, String>getCache("local").getAdvancedCache();
   FunctionalMapImpl<String, String> functionalMap = FunctionalMapImpl.create(cache);
   FunctionalMap.WriteOnlyMap<String, String> writeOnlyMap = WriteOnlyMapImpl.create(functionalMap);
   FunctionalMap.ReadOnlyMap<String, String> readOnlyMap = ReadOnlyMapImpl.create(functionalMap);

   // Execute two parallel write-only operation to store key/value pairs
   CompletableFuture<Void> writeFuture1 = writeOnlyMap.eval("key1", "value1",
      (v, writeView) -> writeView.set(v));
   CompletableFuture<Void> writeFuture2 = writeOnlyMap.eval("key2", "value2",
      (v, writeView) -> writeView.set(v));

   // When each write-only operation completes, execute a read-only operation to retrieve the value
   CompletableFuture<String> readFuture1 =
      writeFuture1.thenCompose(r -> readOnlyMap.eval("key1", EntryView.ReadEntryView::get));
   CompletableFuture<String> readFuture2 =
      writeFuture2.thenCompose(r -> readOnlyMap.eval("key2", EntryView.ReadEntryView::get));

   // When the read-only operation completes, print it out
   System.out.printf("Created entries: %n");
   CompletableFuture<Void> end = readFuture1.thenAcceptBoth(readFuture2, (v1, v2) ->
      System.out.printf("key1 = %s%nkey2 = %s%n", v1, v2));

   // Wait for this read/write combination to finish
   end.get();

   // Create a read-write map
   FunctionalMap.ReadWriteMap<String, String> readWriteMap = ReadWriteMapImpl.create(functionalMap);

   // Use read-write multi-key based operation to write new values
   // together with lifespan and return previous values
   Map<String, String> data = new HashMap<>();
   data.put("key1", "newValue1");
   data.put("key2", "newValue2");
   Traversable<String> previousValues = readWriteMap.evalMany(data, (v, readWriteView) -> {
      String prev = readWriteView.find().orElse(null);
      readWriteView.set(v, new MetaLifespan(Duration.ofHours(1).toMillis()));
      return prev;
   });

   // Use read-only multi-key operation to read current values for multiple keys
   Traversable<EntryView.ReadEntryView<String, String>> entryViews =
      readOnlyMap.evalMany(data.keySet(), readOnlyView -> readOnlyView);
   System.out.printf("Updated entries: %n");
   entryViews.forEach(view -> System.out.printf("%s%n", view));

   // Finally, print out the previous entry values
   System.out.printf("Previous entry values: %n");
   previousValues.forEach(prev -> System.out.printf("%s%n", prev));
}
 
Example #6
Source File: CacheDecorators.java    From keycloak with Apache License 2.0 2 votes vote down vote up
/**
 * Adds {@link Flag#CACHE_MODE_LOCAL} flag to the cache.
 * @param cache
 * @return Cache with the flag applied.
 */
public static <K, V> AdvancedCache<K, V> localCache(Cache<K, V> cache) {
    return cache.getAdvancedCache().withFlags(Flag.CACHE_MODE_LOCAL);
}
 
Example #7
Source File: CacheDecorators.java    From keycloak with Apache License 2.0 2 votes vote down vote up
/**
 * Adds {@link Flag#SKIP_CACHE_LOAD} and {@link Flag#SKIP_CACHE_STORE} flags to the cache.
 * @param cache
 * @return Cache with the flags applied.
 */
public static <K, V> AdvancedCache<K, V> skipCacheLoaders(Cache<K, V> cache) {
    return cache.getAdvancedCache().withFlags(Flag.SKIP_CACHE_LOAD, Flag.SKIP_CACHE_STORE);
}
 
Example #8
Source File: CacheDecorators.java    From keycloak with Apache License 2.0 2 votes vote down vote up
/**
 * Adds {@link Flag#SKIP_CACHE_STORE} flag to the cache.
 * @param cache
 * @return Cache with the flags applied.
 */
public static <K, V> AdvancedCache<K, V> skipCacheStore(Cache<K, V> cache) {
    return cache.getAdvancedCache().withFlags(Flag.SKIP_CACHE_STORE);
}