Java Code Examples for javax.cache.expiry.Duration#isEternal()

The following examples show how to use javax.cache.expiry.Duration#isEternal() . 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: CacheProxy.java    From caffeine with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the access expiration time.
 *
 * @param expirable the entry that was operated on
 * @param currentTimeMS the current time, or 0 if not read yet
 */
protected final void setAccessExpirationTime(Expirable<?> expirable, long currentTimeMS) {
  try {
    Duration duration = expiry.getExpiryForAccess();
    if (duration == null) {
      return;
    } else if (duration.isZero()) {
      expirable.setExpireTimeMS(0L);
    } else if (duration.isEternal()) {
      expirable.setExpireTimeMS(Long.MAX_VALUE);
    } else {
      if (currentTimeMS == 0L) {
        currentTimeMS = currentTimeMillis();
      }
      long expireTimeMS = duration.getAdjustedTime(currentTimeMS);
      expirable.setExpireTimeMS(expireTimeMS);
    }
  } catch (Exception e) {
    logger.log(Level.WARNING, "Failed to set the entry's expiration time", e);
  }
}
 
Example 2
Source File: CacheProxy.java    From caffeine with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the time when the entry will expire.
 *
 * @param created if the write is an insert or update
 * @return the time when the entry will expire, zero if it should expire immediately,
 *         Long.MIN_VALUE if it should not be changed, or Long.MAX_VALUE if eternal
 */
protected final long getWriteExpireTimeMS(boolean created) {
  try {
    Duration duration = created ? expiry.getExpiryForCreation() : expiry.getExpiryForUpdate();
    if (duration == null) {
      return Long.MIN_VALUE;
    } else if (duration.isZero()) {
      return 0L;
    } else if (duration.isEternal()) {
      return Long.MAX_VALUE;
    }
    return duration.getAdjustedTime(currentTimeMillis());
  } catch (Exception e) {
    logger.log(Level.WARNING, "Failed to get the policy's expiration time", e);
    return Long.MIN_VALUE;
  }
}
 
Example 3
Source File: JCSCache.java    From commons-jcs with Apache License 2.0 6 votes vote down vote up
private ICacheElement<K, V> updateElement(final K key, final V v, final Duration duration, final IElementAttributes attrs)
{
    final ICacheElement<K, V> element = new CacheElement<>(name, key, v);
    if (duration != null)
    {
        attrs.setTimeFactorForMilliseconds(1);
        final boolean eternal = duration.isEternal();
        attrs.setIsEternal(eternal);
        if (!eternal)
        {
            attrs.setLastAccessTimeNow();
        }
        // MaxLife = -1 to use IdleTime excepted if jcache.ccf asked for something else
    }
    element.setElementAttributes(attrs);
    return element;
}
 
Example 4
Source File: PlatformExpiryPolicy.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Convert actual duration to encoded duration for serialization.
 *
 * @param dur Actual duration.
 * @return Encoded duration.
 */
public static long convertDuration(Duration dur) {
    if (dur == null)
        return DUR_UNCHANGED;
    else if (dur.isEternal())
        return DUR_ETERNAL;
    else if (dur.isZero())
        return DUR_ZERO;
    else
        return dur.getTimeUnit().toMillis(dur.getDurationAmount());
}
 
Example 5
Source File: IgniteExternalizableExpiryPolicy.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param out Output stream.
 * @param duration Duration.
 * @throws IOException If failed.
 */
private void writeDuration(ObjectOutput out, @Nullable Duration duration) throws IOException {
    if (duration != null) {
        if (duration.getDurationAmount() == 0L) {
            if (duration.isEternal())
                out.writeLong(0);
            else
                out.writeLong(CU.TTL_ZERO);
        }
        else
            out.writeLong(duration.getTimeUnit().toMillis(duration.getDurationAmount()));
    }
}
 
Example 6
Source File: BlazingCacheCache.java    From blazingcache with Apache License 2.0 5 votes vote down vote up
private long getCreatedExpireTime() {
    if (policy == null) {
        return -1;
    }
    Duration res = policy.getExpiryForCreation();
    if (res == null || res.isEternal()) {
        return -1;
    }
    if (res.isZero()) {
        return 0;
    }
    return TimeUnit.MILLISECONDS.convert(res.getDurationAmount(), res.getTimeUnit());
}
 
Example 7
Source File: BlazingCacheCache.java    From blazingcache with Apache License 2.0 5 votes vote down vote up
private long getUpdatedExpireTime() {
    if (policy == null) {
        return -1;
    }
    Duration res = policy.getExpiryForUpdate();
    if (res == null || res.isEternal()) {
        return -1;
    }
    if (res.isZero()) {
        return 0;
    }
    return TimeUnit.MILLISECONDS.convert(res.getDurationAmount(), res.getTimeUnit());
}
 
Example 8
Source File: BlazingCacheCache.java    From blazingcache with Apache License 2.0 5 votes vote down vote up
private long getAccessExpireTime() {
    if (policy == null) {
        return -1;
    }
    Duration res = policy.getExpiryForAccess();
    if (res == null || res.isEternal()) {
        return -1;
    }
    if (res.isZero()) {
        return 0;
    }
    return TimeUnit.MILLISECONDS.convert(res.getDurationAmount(), res.getTimeUnit());
}
 
Example 9
Source File: JCacheLoaderAdapter.java    From caffeine with Apache License 2.0 5 votes vote down vote up
private long expireTimeMS() {
  try {
    Duration duration = expiry.getExpiryForCreation();
    if (duration.isZero()) {
      return 0;
    } else if (duration.isEternal()) {
      return Long.MAX_VALUE;
    }
    long millis = TimeUnit.NANOSECONDS.toMillis(ticker.read());
    return duration.getAdjustedTime(millis);
  } catch (Exception e) {
    return Long.MAX_VALUE;
  }
}
 
Example 10
Source File: CacheUtils.java    From hazelcast-simulator with Apache License 2.0 5 votes vote down vote up
public static void sleepDurationTwice(Logger logger, Duration duration) {
    if (duration.isEternal() || duration.isZero()) {
        return;
    }

    TimeUnit timeUnit = duration.getTimeUnit();
    long timeout = duration.getDurationAmount() * 2;
    logger.info(format("Sleeping for %d %s...", timeout, timeUnit));
    sleepTimeUnit(timeUnit, timeout);
}
 
Example 11
Source File: CacheUtils.java    From hazelcast-simulator with Apache License 2.0 5 votes vote down vote up
public static void sleepDurationTwice(Logger logger, Duration duration) {
    if (duration.isEternal() || duration.isZero()) {
        return;
    }

    TimeUnit timeUnit = duration.getTimeUnit();
    long timeout = duration.getDurationAmount() * 2;
    logger.info(format("Sleeping for %d %s...", timeout, timeUnit));
    sleepTimeUnit(timeUnit, timeout);
}
 
Example 12
Source File: ExpiryPolicyToEhcacheExpiry.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
private java.time.Duration convertDuration(Duration duration) {
  if (duration.isEternal()) {
    return org.ehcache.expiry.ExpiryPolicy.INFINITE;
  }
  return java.time.Duration.of(duration.getDurationAmount(), ExpiryUtils.jucTimeUnitToTemporalUnit(duration.getTimeUnit()));
}
 
Example 13
Source File: JCSCache.java    From commons-jcs with Apache License 2.0 4 votes vote down vote up
private V doGetControllingExpiry(final long getStart, final K key, final boolean updateAcess, final boolean forceDoLoad, final boolean skipLoad,
        final boolean propagateLoadException)
{
    final boolean statisticsEnabled = config.isStatisticsEnabled();
    final ICacheElement<K, V> elt = delegate.get(key);
    V v = elt != null ? elt.getVal() : null;
    if (v == null && (config.isReadThrough() || forceDoLoad))
    {
        if (!skipLoad)
        {
            v = doLoad(key, false, getStart, propagateLoadException);
        }
    }
    else if (statisticsEnabled)
    {
        if (v != null)
        {
            statistics.increaseHits(1);
        }
        else
        {
            statistics.increaseMisses(1);
        }
    }

    if (updateAcess && elt != null)
    {
        final Duration expiryForAccess = expiryPolicy.getExpiryForAccess();
        if (!isNotZero(expiryForAccess))
        {
            forceExpires(key);
        }
        else if (expiryForAccess != null && (!elt.getElementAttributes().getIsEternal() || !expiryForAccess.isEternal()))
        {
            try
            {
                delegate.update(updateElement(key, elt.getVal(), expiryForAccess, elt.getElementAttributes()));
            }
            catch (final IOException e)
            {
                throw new CacheException(e);
            }
        }
    }
    if (statisticsEnabled && v != null)
    {
        statistics.addGetTime(Times.now(false) - getStart);
    }
    return v;
}
 
Example 14
Source File: JCSCache.java    From commons-jcs with Apache License 2.0 4 votes vote down vote up
@Override
public boolean replace(final K key, final V oldValue, final V newValue)
{
    assertNotClosed();
    assertNotNull(key, "key");
    assertNotNull(oldValue, "oldValue");
    assertNotNull(newValue, "newValue");
    final boolean statisticsEnabled = config.isStatisticsEnabled();
    final ICacheElement<K, V> elt = delegate.get(key);
    if (elt != null)
    {
        V value = elt.getVal();
        if (value != null && statisticsEnabled)
        {
            statistics.increaseHits(1);
        }
        if (value == null && config.isReadThrough())
        {
            value = doLoad(key, false, Times.now(false), false);
        }
        if (value != null && value.equals(oldValue))
        {
            put(key, newValue);
            return true;
        }
        else if (value != null)
        {
            final Duration expiryForAccess = expiryPolicy.getExpiryForAccess();
            if (expiryForAccess != null && (!elt.getElementAttributes().getIsEternal() || !expiryForAccess.isEternal()))
            {
                try
                {
                    delegate.update(updateElement(key, elt.getVal(), expiryForAccess, elt.getElementAttributes()));
                }
                catch (final IOException e)
                {
                    throw new CacheException(e);
                }
            }
        }
    }
    else if (statisticsEnabled)
    {
        statistics.increaseMisses(1);
    }
    return false;
}