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

The following examples show how to use javax.cache.Cache#iterator() . 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: JCacheOAuthDataProvider.java    From cxf with Apache License 2.0 6 votes vote down vote up
protected static <K, V extends ServerAccessToken> List<V> getTokens(Cache<K, V> cache,
                                                                  Client client, UserSubject sub) {
    final Set<K> toRemove = new HashSet<>();
    final List<V> tokens = new ArrayList<>();

    for (Iterator<Cache.Entry<K, V>> it = cache.iterator(); it.hasNext();) {
        Cache.Entry<K, V> entry = it.next();
        V token = entry.getValue();

        if (isExpired(token)) {
            toRemove.add(entry.getKey());
        } else if (isTokenMatched(token, client, sub)) {
            tokens.add(token);
        }
    }

    cache.removeAll(toRemove);

    return tokens;
}
 
Example 2
Source File: IteratorTest.java    From blazingcache with Apache License 2.0 5 votes vote down vote up
@Test
    public void testIterateAndRemove() 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);
            for (long i = 0; i < 100L; i++) {
                String word = "";
                word = word + " " + "Trinity";
                cache.put(i, word);
            }

            Iterator<Cache.Entry<Long, String>> iterator = cache.iterator();

            while (iterator.hasNext()) {
                iterator.next();
                iterator.remove();
            }

            assertEquals(100, (int) blazingCache.getStatisticsMXBean().getCacheHits());
            assertEquals(100, (int) blazingCache.getStatisticsMXBean().getCacheHitPercentage());
            assertEquals(0, (int) blazingCache.getStatisticsMXBean().getCacheMisses());
            assertEquals(0, (int) blazingCache.getStatisticsMXBean().getCacheMissPercentage());
            assertEquals(100, (int) blazingCache.getStatisticsMXBean().getCacheGets());
            assertEquals(100, (int) blazingCache.getStatisticsMXBean().getCachePuts());
            assertEquals(100, (int) blazingCache.getStatisticsMXBean().getCacheRemovals());
            assertEquals(0, (int) blazingCache.getStatisticsMXBean().getCacheEvictions());
//            assertTrue(, (int) blazingCache.getStatisticsMXBean().getCache
//            assertThat((Float) lookupManagementAttribute(cache, CacheStatistics, "AveragePutTime"), greaterThanOrEqualTo(0f));
//            assertThat((Float) lookupManagementAttribute(cache, CacheStatistics, "AverageRemoveTime"), greaterThanOrEqualTo(0f));
        }
    }
 
Example 3
Source File: IteratorTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testIterateExpiredIsSkipped() throws Exception {
  EhcacheCachingProvider provider = (EhcacheCachingProvider) Caching.getCachingProvider();
  TestTimeSource testTimeSource = new TestTimeSource();
  TimeSourceConfiguration timeSourceConfiguration = new TimeSourceConfiguration(testTimeSource);
  CacheManager cacheManager = provider.getCacheManager(new URI("test://testIterateExpiredReturnsNull"), new DefaultConfiguration(getClass().getClassLoader(), timeSourceConfiguration));

  Cache<Number, CharSequence> testCache = cacheManager.createCache("testCache", new MutableConfiguration<Number, CharSequence>()
      .setExpiryPolicyFactory(() -> new ExpiryPolicy() {
        @Override
        public Duration getExpiryForCreation() {
          return Duration.ETERNAL;
        }

        @Override
        public Duration getExpiryForAccess() {
          return new Duration(TimeUnit.SECONDS, 1L);
        }

        @Override
        public Duration getExpiryForUpdate() {
          return Duration.ZERO;
        }
      })
      .setTypes(Number.class, CharSequence.class));

  testCache.put(1, "one");
  testCache.get(1);

  testTimeSource.advanceTime(1000);

  Iterator<Cache.Entry<Number, CharSequence>> iterator = testCache.iterator();
  assertThat(iterator.hasNext(), is(false));

  cacheManager.close();
}
 
Example 4
Source File: CacheExpiryTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void iteratorNextShouldCallGetExpiryForAccessedEntry() {
  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);
  Set<Integer> keys = new HashSet<>();
  keys.add(1);
  keys.add(2);

  cache.put(1, 1);
  cache.put(2, 2);

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

  // when getting an existing entry, getExpiryForAccessedEntry is called.
  Iterator<Entry<Integer, Integer>> iter = cache.iterator();
  int count = 0;
  while (iter.hasNext()) {
    Entry<Integer, Integer> entry = iter.next();
    count++;

    assertThat(expiryPolicy.getCreationCount(), is(0));
    assertThat(expiryPolicy.getAccessCount(), greaterThanOrEqualTo(1));
    assertThat(expiryPolicy.getUpdatedCount(), is(0));
    expiryPolicy.resetCount();
  }
}
 
Example 5
Source File: TCKCacheManagerTest.java    From blazingcache with Apache License 2.0 4 votes vote down vote up
@Test
public void expire_whenAccessed() {
    MutableConfiguration<Integer, Integer> config = new MutableConfiguration<Integer, Integer>();
    config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(new ParameterizedExpiryPolicy(Duration.ETERNAL, Duration.ZERO, null)));

    Cache<Integer, Integer> cache = getCacheManager().createCache("test-aaa", config);

    cache.put(1, 1);

    assertTrue(cache.containsKey(1));
    assertNotNull(cache.get(1));
    assertFalse(cache.containsKey(1));
    assertNull(cache.get(1));

    cache.put(1, 1);

    assertTrue(cache.containsKey(1));
    assertNotNull(cache.get(1));
    assertFalse(cache.containsKey(1));
    assertNull(cache.getAndReplace(1, 2));

    cache.put(1, 1);

    assertTrue(cache.containsKey(1));
    assertNotNull(cache.get(1));
    assertFalse(cache.containsKey(1));
    assertNull(cache.getAndRemove(1));

    cache.put(1, 1);
    assertTrue(cache.containsKey(1));
    assertNotNull(cache.get(1));
    assertFalse(cache.remove(1));

    cache.put(1, 1);

    assertTrue(cache.containsKey(1));
    assertNotNull(cache.get(1));
    assertFalse(cache.remove(1, 1));

    cache.getAndPut(1, 1);

    assertTrue(cache.containsKey(1));
    assertNotNull(cache.get(1));
    assertFalse(cache.containsKey(1));
    assertNull(cache.get(1));

    cache.getAndPut(1, 1);

    assertTrue(cache.containsKey(1));
    assertNotNull(cache.getAndPut(1, 1));
    assertTrue(cache.containsKey(1));
    assertNotNull(cache.get(1));
    assertFalse(cache.containsKey(1));
    assertNull(cache.get(1));

    cache.putIfAbsent(1, 1);

    assertTrue(cache.containsKey(1));
    assertNotNull(cache.get(1));
    assertFalse(cache.containsKey(1));
    assertNull(cache.get(1));

    HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
    map.put(1, 1);
    cache.putAll(map);

    assertTrue(cache.containsKey(1));
    assertNotNull(cache.get(1));
    assertFalse(cache.containsKey(1));
    assertNull(cache.get(1));

    cache.put(1, 1);
    Iterator<Cache.Entry<Integer, Integer>> iterator = cache.iterator();

    assertTrue(iterator.hasNext());
    assertEquals((Integer) 1, iterator.next().getValue());
    assertFalse(cache.iterator().hasNext());
}
 
Example 6
Source File: CacheExpiryTest.java    From cache2k with Apache License 2.0 4 votes vote down vote up
/**
 * Ensure that a cache using a {@link javax.cache.expiry.ExpiryPolicy} configured to
 * return a {@link Duration#ZERO} after accessing entries will immediately
 * expire the entries.
 */
@Test
public void expire_whenAccessed() {
  MutableConfiguration<Integer, Integer> config = new MutableConfiguration<Integer, Integer>();
  config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(new ParameterizedExpiryPolicy(Duration.ETERNAL, Duration.ZERO, null)));

  Cache<Integer, Integer> cache = getCacheManager().createCache(getTestCacheName(), config);

  cache.put(1, 1);

  assertTrue(cache.containsKey(1));
  assertNotNull(cache.get(1));
  assertFalse(cache.containsKey(1));
  assertNull(cache.get(1));

  cache.put(1, 1);

  assertTrue(cache.containsKey(1));
  assertNotNull(cache.get(1));
  assertFalse(cache.containsKey(1));
  assertNull(cache.getAndReplace(1, 2));

  cache.put(1, 1);

  assertTrue(cache.containsKey(1));
  assertNotNull(cache.get(1));
  assertFalse(cache.containsKey(1));
  assertNull(cache.getAndRemove(1));

  cache.put(1, 1);
  assertTrue(cache.containsKey(1));
  assertNotNull(cache.get(1));
  assertFalse(cache.remove(1));

  cache.put(1, 1);

  assertTrue(cache.containsKey(1));
  assertNotNull(cache.get(1));
  assertFalse(cache.remove(1, 1));

  cache.getAndPut(1, 1);

  assertTrue(cache.containsKey(1));
  assertNotNull(cache.get(1));
  assertFalse(cache.containsKey(1));
  assertNull(cache.get(1));

  cache.getAndPut(1, 1);

  assertTrue(cache.containsKey(1));
  assertNotNull(cache.getAndPut(1, 1));
  assertTrue(cache.containsKey(1));
  assertNotNull(cache.get(1));
  assertFalse(cache.containsKey(1));
  assertNull(cache.get(1));

  cache.putIfAbsent(1, 1);

  assertTrue(cache.containsKey(1));
  assertNotNull(cache.get(1));
  assertFalse(cache.containsKey(1));
  assertNull(cache.get(1));

  HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
  map.put(1, 1);
  cache.putAll(map);

  assertTrue(cache.containsKey(1));
  assertNotNull(cache.get(1));
  assertFalse(cache.containsKey(1));
  assertNull(cache.get(1));

  cache.put(1, 1);
  Iterator<Entry<Integer, Integer>> iterator = cache.iterator();

  assertTrue(iterator.hasNext());
  assertEquals((Integer) 1, iterator.next().getValue());
  assertFalse(cache.iterator().hasNext());
}