com.github.benmanes.caffeine.cache.Policy Java Examples

The following examples show how to use com.github.benmanes.caffeine.cache.Policy. 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: GuavaCacheFromContext.java    From caffeine with Apache License 2.0 6 votes vote down vote up
@Override
public Policy<K, V> policy() {
  return new Policy<K, V>() {
    @Override public boolean isRecordingStats() {
      return isRecordingStats;
    }
    @Override public Optional<Eviction<K, V>> eviction() {
      return Optional.empty();
    }
    @Override public Optional<Expiration<K, V>> expireAfterAccess() {
      return Optional.empty();
    }
    @Override public Optional<Expiration<K, V>> expireAfterWrite() {
      return Optional.empty();
    }
    @Override public Optional<Expiration<K, V>> refreshAfterWrite() {
      return Optional.empty();
    }
  };
}
 
Example #2
Source File: CaffeineCache.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
@SuppressWarnings({"squid:S2583", "ConstantConditions"})
private Long getMaxCacheSize() {
    if (synchronousCacheView == null) {
        // This can occur if this method is called by metricStatsCounter before the cache has been initialized.
        return 0L;
    }

    return synchronousCacheView.policy().eviction().map(Policy.Eviction::getMaximum).orElse(0L);
}
 
Example #3
Source File: CacheProvider.java    From caffeine with Apache License 2.0 5 votes vote down vote up
/** Returns the fixed expiration policy for the given parameter. */
private static Policy.Expiration<Integer, Integer> expirationPolicy(
    Parameter parameter, Cache<Integer, Integer> cache) {
  if (parameter.isAnnotationPresent(ExpireAfterAccess.class)) {
    return cache.policy().expireAfterAccess().get();
  } else if (parameter.isAnnotationPresent(ExpireAfterWrite.class)) {
    return cache.policy().expireAfterWrite().get();
  } else if (parameter.isAnnotationPresent(RefreshAfterWrite.class)) {
    return cache.policy().refreshAfterWrite().get();
  }
  throw new AssertionError("Expiration parameter must have a qualifier annotation");
}
 
Example #4
Source File: MetricReportingCache.java    From cerberus with Apache License 2.0 4 votes vote down vote up
@Override
public Policy policy() {
  return delegate.policy();
}
 
Example #5
Source File: CaffeineMetricSupportTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public Policy<Object, Object> policy() {
    return policy;
}
 
Example #6
Source File: CacheProvider.java    From caffeine with Apache License 2.0 4 votes vote down vote up
/**
 * Converts each scenario into test case parameters. Supports injecting {@link LoadingCache},
 * {@link Cache}, {@link CacheContext}, the {@link ConcurrentMap} {@link Cache#asMap()} view,
 * {@link Policy.Eviction}, and {@link Policy.Expiration}.
 */
private static Iterator<Object[]> asTestCases(Method testMethod,
    Stream<Map.Entry<CacheContext, Cache<Integer, Integer>>> scenarios) {
  Parameter[] parameters = testMethod.getParameters();
  CacheContext[] stashed = new CacheContext[1];
  return scenarios.map(entry -> {
    CacheContext context = entry.getKey();
    Cache<Integer, Integer> cache = entry.getValue();

    // Retain a strong reference to the context throughout the test execution so that the
    // cache entries are not collected due to the test not accepting the context parameter
    stashed[0] = context;

    Object[] params = new Object[parameters.length];
    for (int i = 0; i < params.length; i++) {
      Class<?> clazz = parameters[i].getType();
      if (clazz.isAssignableFrom(CacheContext.class)) {
        params[i] = context;
      } else if (clazz.isAssignableFrom(Caffeine.class)) {
        params[i] = context.caffeine;
      } else if (clazz.isAssignableFrom(cache.getClass())) {
        params[i] = cache;
      } else if ((context.asyncCache != null)
          && clazz.isAssignableFrom(context.asyncCache.getClass())) {
        params[i] = context.asyncCache;
      } else if (clazz.isAssignableFrom(Map.class)) {
        params[i] = cache.asMap();
      } else if (clazz.isAssignableFrom(Policy.Eviction.class)) {
        params[i] = cache.policy().eviction().get();
      } else if (clazz.isAssignableFrom(Policy.Expiration.class)) {
        params[i] = expirationPolicy(parameters[i], cache);
      } else if (clazz.isAssignableFrom(Policy.VarExpiration.class)) {
        params[i] = cache.policy().expireVariably().get();
      }
      if (params[i] == null) {
        checkNotNull(params[i], "Unknown parameter type: %s", clazz);
      }
    }
    return params;
  }).filter(Objects::nonNull).iterator();
}