net.jodah.expiringmap.ExpirationPolicy Java Examples

The following examples show how to use net.jodah.expiringmap.ExpirationPolicy. 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: InMemorySlidingWindowRequestRateLimiter.java    From ratelimitj with Apache License 2.0 5 votes vote down vote up
private ConcurrentMap<String, Long> getMap(String key, int longestDuration) {

        // Currently unable to putIfAbsent when using jodah's expiry map so will wrap in a lock
        return lockManager.executeLocked(key, () -> {
            ConcurrentMap<String, Long> keyMap = expiringKeyMap.get(key);
            if (keyMap == null) {
                keyMap = new ConcurrentHashMap<>();
                expiringKeyMap.put(key, keyMap, ExpirationPolicy.CREATED, longestDuration, TimeUnit.SECONDS);
            }
            return keyMap;
        });
    }
 
Example #2
Source File: PoolStrategyCache.java    From kanela with Apache License 2.0 5 votes vote down vote up
private PoolStrategyCache() {
    super(TypePool.Default.ReaderMode.FAST);
    ExpiringMap.setThreadFactory(NamedThreadFactory.instance("strategy-cache-listener"));
    this.cache = ExpiringMap
            .builder()
            .entryLoader((key) -> TypePool.CacheProvider.Simple.withObjectType())
            .expiration(1, TimeUnit.MINUTES)
            .expirationPolicy(ExpirationPolicy.ACCESSED)
            .asyncExpirationListener(LogExpirationListener())
            .build();
}
 
Example #3
Source File: MemoryQueryCache.java    From heroic with Apache License 2.0 5 votes vote down vote up
@Override
public AsyncFuture<QueryResult> load(
    FullQuery.Request request, Supplier<AsyncFuture<QueryResult>> loader
) {
    final AggregationInstance aggregation = request.aggregation();

    /* can't be cached :( */
    if (aggregation.cadence() <= 0) {
        return loader.get();
    }

    final AsyncFuture<QueryResult> result = cache.get(request);

    if (result != null) {
        return result;
    }

    synchronized (lock) {
        final AsyncFuture<QueryResult> candidate = cache.get(request);

        if (candidate != null) {
            return candidate;
        }

        final AsyncFuture<QueryResult> next = loader.get();
        cache.put(request, next, ExpirationPolicy.ACCESSED, aggregation.cadence(),
            TimeUnit.MILLISECONDS);
        return next;
    }
}
 
Example #4
Source File: ExpirationNotificatorTimer.java    From oxAuth with MIT License 5 votes vote down vote up
public void initTimer() {
    log.debug("Initializing ExpirationNotificatorTimer");
    this.isActive = new AtomicBoolean(false);

    expiringMap = ExpiringMap.builder()
            .expirationPolicy(ExpirationPolicy.CREATED)
            .maxSize(appConfiguration.getExpirationNotificatorMapSizeLimit())
            .variableExpiration()
            .build();
    expiringMap.addExpirationListener(this);

    timerEvent.fire(new TimerEvent(new TimerSchedule(DEFAULT_INTERVAL, DEFAULT_INTERVAL), new ExpirationEvent(), Scheduled.Literal.INSTANCE));

    this.lastFinishedTime = System.currentTimeMillis();
}
 
Example #5
Source File: LocalCache.java    From gcp-token-broker with Apache License 2.0 4 votes vote down vote up
public static void set(String key, Object value, int expireIn) {
    cache.put(key, value, ExpirationPolicy.CREATED, expireIn, TimeUnit.SECONDS);
}
 
Example #6
Source File: ExpiringMapCache.java    From caffeine with Apache License 2.0 4 votes vote down vote up
public ExpiringMapCache(int maximumSize, ExpirationPolicy policy) {
  cache = ExpiringMap.builder()
      .expirationPolicy(policy)
      .maxSize(maximumSize)
      .build();
}