Java Code Examples for org.ehcache.expiry.ExpiryPolicy#getExpiryForCreation()

The following examples show how to use org.ehcache.expiry.ExpiryPolicy#getExpiryForCreation() . 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
public CacheType unparseConfiguration(CacheConfiguration<?, ?> cacheConfiguration, CacheType cacheType) {
  ExpiryPolicy<?, ?> expiryPolicy = cacheConfiguration.getExpiryPolicy();
  if (expiryPolicy != null) {
    Duration expiry = expiryPolicy.getExpiryForCreation(null, null);
    ExpiryType expiryType = new ExpiryType();
    if (expiryPolicy.equals(ExpiryPolicy.NO_EXPIRY)) {
      expiryType.withNone(new ExpiryType.None());
    } else if (expiryPolicy.equals(ExpiryPolicyBuilder.timeToLiveExpiration(expiry))) {
      expiryType.withTtl(convertToTimeType(expiry));
    } else if (expiryPolicy.equals(ExpiryPolicyBuilder.timeToIdleExpiration(expiry))) {
      expiryType.withTti(convertToTimeType(expiry));
    } else {
      throw new XmlConfigurationException("XML translation of custom expiry policy is not supported");
    }
    cacheType.withExpiry(expiryType);
  }

  EvictionAdvisor<?, ?> evictionAdvisor = cacheConfiguration.getEvictionAdvisor();
  if (evictionAdvisor != null) {
    throw new XmlConfigurationException("XML translation of eviction advisor is not supported");
  }

  return cacheType;
}
 
Example 2
Source File: LocalLoaderWriterStore.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
private static <K, V> boolean newValueAlreadyExpired(Logger logger, ExpiryPolicy<? super K, ? super V> expiry, K key, V oldValue, V newValue) {
  if (newValue == null) {
    return false;
  }

  Duration duration;
  try {
    if (oldValue == null) {
      duration = expiry.getExpiryForCreation(key, newValue);
    } else {
      duration = expiry.getExpiryForUpdate(key, () -> oldValue, newValue);
    }
  } catch (RuntimeException re) {
    logger.error("Expiry computation caused an exception - Expiry duration will be 0 ", re);
    return true;
  }

  return Duration.ZERO.equals(duration);
}
 
Example 3
Source File: ExpiryUtils.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the expiry for creation duration returned by the provided {@link ExpiryPolicy} but checks for immediate
 * expiry, null expiry and exceptions. In all those cases, {@code null} will be returned.
 *
 * @param key key to pass to {@link ExpiryPolicy#getExpiryForCreation(Object, Object)}
 * @param value value to pass to to pass to {@link ExpiryPolicy#getExpiryForCreation(Object, Object)}
 * @param expiry expiry queried
 * @param <K> type of key
 * @param <V> type of value
 * @return the duration returned by to pass to {@link ExpiryPolicy#getExpiryForCreation(Object, Object)}, {@code null}
 * if the call throws an exception, if the returned duration is {@code null} or if it is lower or equal to 0
 */
public static <K, V> Duration getExpiryForCreation(K key, V value, ExpiryPolicy<? super K, ? super V> expiry) {
  Duration duration;
  try {
    duration = expiry.getExpiryForCreation(key, value);
  } catch (RuntimeException e) {
    LOG.error("Expiry computation caused an exception - Expiry duration will be 0", e);
    return Duration.ZERO;
  }

  if (duration == null) {
    LOG.error("Expiry for creation can't be null - Expiry duration will be 0");
    return Duration.ZERO;
  }

  if (Duration.ZERO.compareTo(duration) >= 0) {
    return Duration.ZERO;
  }

  return duration;
}
 
Example 4
Source File: EhcacheBase.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
protected static <K, V> boolean newValueAlreadyExpired(Logger logger, ExpiryPolicy<? super K, ? super V> expiry, K key, V oldValue, V newValue) {
  if (newValue == null) {
    return false;
  }

  Duration duration;
  try {
    if (oldValue == null) {
      duration = expiry.getExpiryForCreation(key, newValue);
    } else {
      duration = expiry.getExpiryForUpdate(key, () -> oldValue, newValue);
    }
  } catch (RuntimeException re) {
    logger.error("Expiry computation caused an exception - Expiry duration will be 0 ", re);
    return true;
  }

  return Duration.ZERO.equals(duration);
}