Java Code Examples for org.ehcache.impl.internal.TimeSourceConfiguration

Following code examples demonstrate how to use org.ehcache.impl.internal.TimeSourceConfiguration from ehcache. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.ehcache.impl.internal.TimeSourceConfiguration and various code implementation of this class.
Example 1
Project : ehcache Source File : BasicClusteredCacheExpiryTest.java View Source Code on GitHub

  public void testGetExpiredTwoClients() {

    TestTimeSource timeSource = new TestTimeSource();
    TimeSourceConfiguration timeSourceConfiguration = new TimeSourceConfiguration(timeSource);

    final CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder =
        commonClusteredCacheManagerBuilder.using(timeSourceConfiguration);

    final PersistentCacheManager cacheManager1 = clusteredCacheManagerBuilder.build(true);
    final PersistentCacheManager cacheManager2 = clusteredCacheManagerBuilder.build(true);

    final Cache<Long, String> cache1 = cacheManager1.getCache("clustered-cache", Long.class, String.class);
    final Cache<Long, String> cache2 = cacheManager2.getCache("clustered-cache", Long.class, String.class);

    assertThat(cache2.get(1L), nullValue());
    cache1.put(1L, "value1");
    assertThat(cache1.get(1L), is("value1"));
    timeSource.advanceTime(1L);

    assertThat(cache2.get(1L), nullValue());
    assertThat(cache1.get(1L), nullValue());

    cacheManager2.close();
    cacheManager1.close();
  }
			
Example 2
Project : ehcache Source File : BasicClusteredCacheExpiryTest.java View Source Code on GitHub

  public void testContainsKeyExpiredTwoClients() {

    TestTimeSource timeSource = new TestTimeSource();
    TimeSourceConfiguration timeSourceConfiguration = new TimeSourceConfiguration(timeSource);

    final CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder =
        commonClusteredCacheManagerBuilder.using(timeSourceConfiguration);

    final PersistentCacheManager cacheManager1 = clusteredCacheManagerBuilder.build(true);
    final PersistentCacheManager cacheManager2 = clusteredCacheManagerBuilder.build(true);

    final Cache<Long, String> cache1 = cacheManager1.getCache("clustered-cache", Long.class, String.class);
    final Cache<Long, String> cache2 = cacheManager2.getCache("clustered-cache", Long.class, String.class);

    assertThat(cache2.get(1L), nullValue());
    cache1.put(1L, "value1");
    assertThat(cache1.containsKey(1L), is(true));
    timeSource.advanceTime(1L);

    assertThat(cache1.containsKey(1L), is(false));
    assertThat(cache2.containsKey(1L), is(false));

    cacheManager2.close();
    cacheManager1.close();

  }
			
Example 3
Project : ehcache Source File : BasicClusteredCacheExpiryTest.java View Source Code on GitHub

  public void testGetExpiredSingleClient() {

    TestTimeSource timeSource = new TestTimeSource();
    TimeSourceConfiguration timeSourceConfiguration = new TimeSourceConfiguration(timeSource);

    final CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder =
        commonClusteredCacheManagerBuilder.using(timeSourceConfiguration);

    final PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(true);

    final Cache<Long, String> cache = cacheManager.getCache("clustered-cache", Long.class, String.class);

    cache.put(1L, "value");
    assertThat(cache.get(1L), is("value"));

    timeSource.advanceTime(1);

    assertThat(cache.get(1L), nullValue());

    cacheManager.close();

  }
			
Example 4
Project : ehcache Source File : IteratorTest.java View Source Code on GitHub

  public void testIterateExpiredReturnsNull() throws Exception {
    EhcacheCachingProvider provider = (EhcacheCachingProvider) Caching.getCachingProvider();
    TestTimeSource testTimeSource = new TestTimeSource();
    TimeSourceConfiguration timeSourceConfiguration = new TimeSourceConfiguration(testTimeSource);

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

    int loopCount = 0;
    while (iterator.hasNext()) {
      Cache.Entry<Number, CharSequence> next = iterator.next();
      assertThat(next, is(nullValue()));

      loopCount++;
    }
    assertThat(loopCount, is(1));

    cacheManager.close();
  }