Java Code Examples for org.ehcache.config.builders.ExpiryPolicyBuilder#timeToIdleExpiration()

The following examples show how to use org.ehcache.config.builders.ExpiryPolicyBuilder#timeToIdleExpiration() . 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: CoreCacheConfigurationParser.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"unchecked", "deprecation"})
private static ExpiryPolicy<? super Object, ? super Object> getExpiry(ClassLoader cacheClassLoader, Expiry parsedExpiry)
  throws ClassNotFoundException, InstantiationException, IllegalAccessException {
  if (parsedExpiry.isUserDef()) {
    try {
      return getInstanceOfName(parsedExpiry.type(), cacheClassLoader, ExpiryPolicy.class);
    } catch (ClassCastException e) {
      return ExpiryUtils.convertToExpiryPolicy(getInstanceOfName(parsedExpiry.type(), cacheClassLoader, org.ehcache.expiry.Expiry.class));
    }
  } else if (parsedExpiry.isTTL()) {
    return ExpiryPolicyBuilder.timeToLiveExpiration(Duration.of(parsedExpiry.value(), parsedExpiry.unit()));
  } else if (parsedExpiry.isTTI()) {
    return ExpiryPolicyBuilder.timeToIdleExpiration(Duration.of(parsedExpiry.value(), parsedExpiry.unit()));
  } else {
    return ExpiryPolicyBuilder.noExpiration();
  }
}
 
Example 2
Source File: AbstractOffHeapStoreTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testFlushUpdatesAccessStats() throws StoreAccessException {
  ExpiryPolicy<Object, Object> expiry = ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofMillis(15L));
  offHeapStore = createAndInitStore(timeSource, expiry);
  try {
    final String key = "foo";
    final String value = "bar";
    offHeapStore.put(key, value);
    final Store.ValueHolder<String> firstValueHolder = offHeapStore.getAndFault(key);
    offHeapStore.put(key, value);
    final Store.ValueHolder<String> secondValueHolder = offHeapStore.getAndFault(key);
    timeSource.advanceTime(10);
    ((AbstractValueHolder) firstValueHolder).accessed(timeSource.getTimeMillis(), expiry.getExpiryForAccess(key, () -> value));
    timeSource.advanceTime(10);
    ((AbstractValueHolder) secondValueHolder).accessed(timeSource.getTimeMillis(), expiry.getExpiryForAccess(key, () -> value));
    assertThat(offHeapStore.flush(key, new DelegatingValueHolder<>(firstValueHolder)), is(false));
    assertThat(offHeapStore.flush(key, new DelegatingValueHolder<>(secondValueHolder)), is(true));
    timeSource.advanceTime(10); // this should NOT affect
    assertThat(offHeapStore.getAndFault(key).lastAccessTime(), is(secondValueHolder.creationTime() + 20));
  } finally {
    destroyStore(offHeapStore);
  }
}
 
Example 3
Source File: CacheManagerProvider.java    From TweetwallFX with MIT License 5 votes vote down vote up
private static ExpiryPolicy<Object, Object> createExpiryPolicy(final CacheSettings.CacheExpiry cacheExpiry) {
    switch (cacheExpiry.getType()) {
        case NONE:
            return ExpiryPolicyBuilder.noExpiration();
        case TIME_TO_IDLE:
            return ExpiryPolicyBuilder.timeToIdleExpiration(cacheExpiry.produceDuration());
        case TIME_TO_LIVE:
            return ExpiryPolicyBuilder.timeToLiveExpiration(cacheExpiry.produceDuration());
        default:
            throw new IllegalStateException(CacheExpiryType.class.getSimpleName() + " '" + cacheExpiry.getType() + "' is not supported!");
    }
}
 
Example 4
Source File: XmlConfigurationTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testExpiryIsParsed() throws Exception {
  URL resource = XmlConfigurationTest.class.getResource("/configs/expiry-caches.xml");
  final XmlConfiguration xmlConfiguration = new XmlConfiguration(resource);

  ExpiryPolicy<?, ?> expiry = xmlConfiguration.getCacheConfigurations().get("none").getExpiryPolicy();
  ExpiryPolicy<?, ?> value = ExpiryPolicyBuilder.noExpiration();
  assertThat(expiry, is(value));

  expiry = xmlConfiguration.getCacheConfigurations().get("notSet").getExpiryPolicy();
  value = ExpiryPolicyBuilder.noExpiration();
  assertThat(expiry, is(value));

  expiry = xmlConfiguration.getCacheConfigurations().get("class").getExpiryPolicy();
  assertThat(expiry, CoreMatchers.instanceOf(com.pany.ehcache.MyExpiry.class));

  expiry = xmlConfiguration.getCacheConfigurations().get("deprecatedClass").getExpiryPolicy();
  assertThat(expiry.getExpiryForCreation(null, null), is(Duration.ofSeconds(42)));
  assertThat(expiry.getExpiryForAccess(null, () -> null), is(Duration.ofSeconds(42)));
  assertThat(expiry.getExpiryForUpdate(null, () -> null, null), is(Duration.ofSeconds(42)));

  expiry = xmlConfiguration.getCacheConfigurations().get("tti").getExpiryPolicy();
  value = ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofMillis(500));
  assertThat(expiry, equalTo(value));

  expiry = xmlConfiguration.getCacheConfigurations().get("ttl").getExpiryPolicy();
  value = ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(30));
  assertThat(expiry, equalTo(value));
}
 
Example 5
Source File: CoreCacheConfigurationParserTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void parseConfigurationExpiryPolicy() throws Exception {
  Configuration configuration = new XmlConfiguration(getClass().getResource("/configs/expiry-caches.xml"));

  ExpiryPolicy<?, ?> expiry = configuration.getCacheConfigurations().get("none").getExpiryPolicy();
  ExpiryPolicy<?, ?> value = ExpiryPolicyBuilder.noExpiration();
  assertThat(expiry, is(value));

  expiry = configuration.getCacheConfigurations().get("notSet").getExpiryPolicy();
  value = ExpiryPolicyBuilder.noExpiration();
  assertThat(expiry, is(value));

  expiry = configuration.getCacheConfigurations().get("class").getExpiryPolicy();
  assertThat(expiry, CoreMatchers.instanceOf(com.pany.ehcache.MyExpiry.class));

  expiry = configuration.getCacheConfigurations().get("deprecatedClass").getExpiryPolicy();
  assertThat(expiry.getExpiryForCreation(null, null), is(Duration.ofSeconds(42)));
  assertThat(expiry.getExpiryForAccess(null, () -> null), is(Duration.ofSeconds(42)));
  assertThat(expiry.getExpiryForUpdate(null, () -> null, null), is(Duration.ofSeconds(42)));

  expiry = configuration.getCacheConfigurations().get("tti").getExpiryPolicy();
  value = ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofMillis(500));
  assertThat(expiry, equalTo(value));

  expiry = configuration.getCacheConfigurations().get("ttl").getExpiryPolicy();
  value = ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(30));
  assertThat(expiry, equalTo(value));
}
 
Example 6
Source File: AbstractOffHeapStoreTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testEvictionAdvisor() throws StoreAccessException {
  ExpiryPolicy<Object, Object> expiry = ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofMillis(15L));
  EvictionAdvisor<String, byte[]> evictionAdvisor = (key, value) -> true;

  performEvictionTest(timeSource, expiry, evictionAdvisor);
}
 
Example 7
Source File: AbstractOffHeapStoreTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testBrokenEvictionAdvisor() throws StoreAccessException {
  ExpiryPolicy<Object, Object> expiry = ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofMillis(15L));
  EvictionAdvisor<String, byte[]> evictionAdvisor = (key, value) -> {
    throw new UnsupportedOperationException("Broken advisor!");
  };

  performEvictionTest(timeSource, expiry, evictionAdvisor);
}