Class Expiry
- java.lang.Object
-
- org.cache2k.expiry.Expiry
-
- All Implemented Interfaces:
ExpiryTimeValues
public class Expiry extends Object implements ExpiryTimeValues
Utility methods and constants to use inside expire policy or resilience policy.- Author:
- Jens Wilke
- See Also:
ExpiryPolicy
,ResiliencePolicy
,Cache.expireAt(K, long)
-
-
Field Summary
-
Fields inherited from interface org.cache2k.expiry.ExpiryTimeValues
ETERNAL, NEUTRAL, NOW, REFRESH
-
-
Constructor Summary
Constructors Constructor Description Expiry()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static long
earliestTime(long loadTime, long candidate1, long candidate2)
Helper to calculate the next expiry out of two expiry times that may be up next.static long
mixTimeSpanAndPointInTime(long loadTime, long refreshAfter, long pointInTime)
Combine a refresh time span and an expiry at a specified point in time.static long
toSharpTime(long millis)
Convert the time value to a time representing a sharp expiry.
-
-
-
Method Detail
-
toSharpTime
public static long toSharpTime(long millis)
Convert the time value to a time representing a sharp expiry. This essentially negates the time value and is provided for a more speaking coding style.- Parameters:
millis
- expiry time since the milliseconds since epoch orExpiryTimeValues.ETERNAL
if no expiry is requested.- Throws:
IllegalArgumentException
- if the time value is negative
-
earliestTime
public static long earliestTime(long loadTime, long candidate1, long candidate2)
Helper to calculate the next expiry out of two expiry times that may be up next. Return the time value that is closest or equal to the load time. Time values in the past are ignored. If all times are in the past, returnsExpiryTimeValues.ETERNAL
.- Parameters:
loadTime
- the current time in millis since epochcandidate1
- candidate time for next expirycandidate2
- candidate time for next expiry- Returns:
- either first or second candidate or
ExpiryTimeValues.ETERNAL
-
mixTimeSpanAndPointInTime
public static long mixTimeSpanAndPointInTime(long loadTime, long refreshAfter, long pointInTime)
Combine a refresh time span and an expiry at a specified point in time.If the expiry time is far ahead of time the refresh time span takes precedence. If the point in time is near, this time takes precedence. If the refresh time is too close to the requested point an earlier refresh time is used to keep maximum distance to the requested point in time, which is
abs(pointInTime) - refreshAfter
Rationale: Usually the expiry is allowed to lag behind. This is okay when a normal expiry interval is used. If sharp expiry is requested an old value may not be visible at and after the expiry time. Refresh ahead implies lagging expiry, since the refresh is triggered when the value would usually expire. The two concepts can be combined in the expiry policy, e.g. using an interval for triggering refresh ahead and requesting a sharp expiry only when needed. If effective expiry time for the lagging variant and the for the sharp variant are in proximity, conflicts need to be resolved.
- Parameters:
loadTime
- time when the load was startedrefreshAfter
- time span in milliseconds when the next refresh should happenpointInTime
- time in milliseconds since epoch for the next expiry. Can be negative if sharp expiry is requested, orExpiryTimeValues.ETERNAL
if no point in time expiry is needed.
-
-