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

Following code examples demonstrate how to use org.ehcache.impl.internal.TimeSourceConfigurationfrom 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.TimeSourceConfigurationand various code implementation of this class.

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


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

  } 

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

  } 

  public void before() {
    CacheConfiguration<Long, String> cacheConfiguration =
      CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
        newResourcePoolsBuilder().heap(10))
        .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofMillis(TIME_TO_EXPIRATION)))
        .build();

    cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
      .withCache("aCache", cacheConfiguration)
      .using(new TimeSourceConfiguration(timeSource))
      .build(true);

    cache = cacheManager.getCache("aCache", Long.class, String.class);

    onHeap = new DefaultTierStatistics(cache, "OnHeap");
  } 

  public void setUp() throws Exception {
    manualTimeSource.setTimeMillis(0L);
    CacheManagerBuilder<CacheManager> builder = CacheManagerBuilder.newCacheManagerBuilder().using(new TimeSourceConfiguration(manualTimeSource));
    cacheManager = builder.build(true);
    CacheConfigurationBuilder<Number, CharSequence> objectObjectCacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder(Number.class, CharSequence.class, heap(10))
        .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(1)));
    testCache = cacheManager.createCache("testCache", objectObjectCacheConfigurationBuilder.build());
  } 

  private CacheManagerBuilder<PersistentCacheManager> cacheManagerBuilder(ExpiryPolicy<Object, Object> expiry) {
    return newCacheManagerBuilder()
        .using(statisticsService)
        .using(new TimeSourceConfiguration(timeSource))
        .with(cluster(CLUSTER_URI).autoCreate())
        .withCache(CLUSTERED_CACHE, newCacheConfigurationBuilder(Long.class, String.class,
            ResourcePoolsBuilder.newResourcePoolsBuilder()
                .heap(10, EntryUnit.ENTRIES)
                .offheap(10, MemoryUnit.MB)
                .with(ClusteredResourcePoolBuilder.clusteredDedicated("primary-server-resource", 32, MemoryUnit.MB)))
              .withExpiry(expiry)
            .add(ClusteredStoreConfigurationBuilder.withConsistency(Consistency.STRONG)));
  } 

  public void before() {
    cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
      .using(timeSourceConfiguration)
      .build(true);
  } 

  private CacheManager initCacheManager() {
    return CacheManagerBuilder.newCacheManagerBuilder()
      .using(new TimeSourceConfiguration(timeSource))
      .build(true);
  } 

  public void before() {
    CacheConfiguration<Long, String> cacheConfiguration =
      CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
        newResourcePoolsBuilder().heap(10))
        .withExpiry(Expirations.timeToLiveExpiration(Duration.of(TIME_TO_EXPIRATION, TimeUnit.MILLISECONDS)))
        .build();

    cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
      .withCache("aCache", cacheConfiguration)
      .using(new TimeSourceConfiguration(timeSource))
      .build(true);

    cache = (InternalCache<Long, String>) cacheManager.getCache("aCache", Long.class, String.class);

    cacheStatistics = new DefaultCacheStatistics(cache);
  } 

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

Advertisement
Javadoc
Configuration for the TimeSourceService This configuration has to be applied at the org.ehcache.Cach

eManager level.

Read More
Advertisement