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

The following examples show how to use com.github.benmanes.caffeine.cache.CaffeineSpec. 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: ProtobufRedisLoadingCache.java    From curiostack with MIT License 6 votes vote down vote up
/**
 * Constructs a new {@link ProtobufRedisLoadingCache} that can write protobuf {@link Message}
 * keys and values to remoteCache, with an optional local cache layer.
 *
 * @param name name of this cache, will be prefixed onto all keys.
 * @param keyPrototype a prototype for the key {@link Message}, usually gotten from {@code
 *     Key.getDefaultInstance()}.
 * @param valuePrototype a prototype for the value {@link Message}, usually gotten from {@code
 *     Value.getDefaultInstance()}.
 * @param redisTtl the time until expiration of a value in the remoteCache cache. The local
 *     cache should be considered in localCacheSpec.
 * @param redisMasterOnly whether remoteCache reads should only happen from master. Best-effort,
 *     temporary persistent storage should set this to {@code true}.
 * @param localCacheSpec a {@link CaffeineSpec} to control the local cache layer. If {@code
 *     null}, local caching will be disabled.
 */
// TODO(choko): Fix this later since it breaks the API.
@SuppressWarnings("InconsistentOverloads")
public <K extends Message, V extends Message> ProtobufRedisLoadingCache<K, V> create(
    String name,
    K keyPrototype,
    V valuePrototype,
    Duration redisTtl,
    boolean redisMasterOnly,
    @Nullable CaffeineSpec localCacheSpec) {
  return new ProtobufRedisLoadingCache<>(
      keyPrototype,
      valuePrototype,
      redisTtl,
      localCacheSpec,
      config.isNoop()
          ? new NoopRemoteCache<>()
          : config.isCluster()
              ? createRedisRemoteCache(
                  name,
                  redisClusterClient.get(),
                  keyPrototype,
                  valuePrototype,
                  redisMasterOnly ? ReadFrom.MASTER : ReadFrom.NEAREST)
              : createRedisRemoteCache(name, redisClient.get(), keyPrototype, valuePrototype));
}
 
Example #2
Source File: ProtobufRedisLoadingCache.java    From curiostack with MIT License 6 votes vote down vote up
ProtobufRedisLoadingCache(
    K keyPrototype,
    V valuePrototype,
    Duration redisTtl,
    @Nullable CaffeineSpec localCacheSpec,
    RemoteCache<K, V> remoteCache) {
  checkNotNull(keyPrototype, "keyPrototype");
  checkNotNull(valuePrototype, "valuePrototype");
  checkNotNull(redisTtl, "redisTtl");
  this.remoteCache = checkNotNull(remoteCache, "remoteCache");
  final Caffeine<Object, Object> caffeineBuilder =
      localCacheSpec != null
          ? Caffeine.from(localCacheSpec)
          : Caffeine.newBuilder().maximumSize(0);
  cache =
      caffeineBuilder
          .executor(CurrentRequestContextExecutor.INSTANCE)
          .buildAsync((k, executor) -> remoteCache.get(k).toCompletableFuture());
  setArgs = SetArgs.Builder.px(redisTtl.toMillis());
}
 
Example #3
Source File: CacheConfig.java    From secrets-proxy with Apache License 2.0 6 votes vote down vote up
/**
 * Default cache spec configuration for all the caches. The default cache size is 200 and would
 * expire after a min (60sec) of write operation.
 *
 * @return {@link CaffeineSpec}
 */
@Bean
public CaffeineSpec caffeineSpec() {
  CaffeineSpec spec = CaffeineSpec.parse("maximumSize=200,expireAfterWrite=300s");
  log.info("Using CaffeineSpec " + spec.toParsableString());
  return spec;
}
 
Example #4
Source File: CaffeineCacheManagerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void changeCaffeineSpecRecreateCache() {
	CaffeineCacheManager cm = new CaffeineCacheManager("c1");
	Cache cache1 = cm.getCache("c1");

	cm.setCaffeineSpec(CaffeineSpec.parse("maximumSize=10"));
	Cache cache1x = cm.getCache("c1");
	assertTrue(cache1x != cache1);
}
 
Example #5
Source File: CaffeineCacheManagerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void changeCaffeineSpecRecreateCache() {
	CaffeineCacheManager cm = new CaffeineCacheManager("c1");
	Cache cache1 = cm.getCache("c1");

	cm.setCaffeineSpec(CaffeineSpec.parse("maximumSize=10"));
	Cache cache1x = cm.getCache("c1");
	assertTrue(cache1x != cache1);
}
 
Example #6
Source File: RepositoryCache.java    From centraldogma with Apache License 2.0 5 votes vote down vote up
@Nullable
public static String validateCacheSpec(@Nullable String cacheSpec) {
    if (cacheSpec == null) {
        return null;
    }

    try {
        CaffeineSpec.parse(cacheSpec);
        return cacheSpec;
    } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException("cacheSpec: " + cacheSpec + " (" + e.getMessage() + ')');
    }
}
 
Example #7
Source File: FileServiceConfig.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Nullable
static String validateEntryCacheSpec(@Nullable String entryCacheSpec) {
    if (entryCacheSpec == null || "off".equals(entryCacheSpec)) {
        return null;
    }
    try {
        CaffeineSpec.parse(entryCacheSpec);
    } catch (Exception e) {
        throw new IllegalArgumentException("invalid cache spec: " + entryCacheSpec, e);
    }
    return entryCacheSpec;
}
 
Example #8
Source File: Flags.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Nullable
private static String caffeineSpec(String name, String defaultValue) {
    final String spec = get(name, defaultValue, value -> {
        try {
            if (!"off".equals(value)) {
                CaffeineSpec.parse(value);
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    });
    return "off".equals(spec) ? null : spec;
}
 
Example #9
Source File: PublicApiConfig.java    From pay-publicapi with MIT License 4 votes vote down vote up
public CaffeineSpec getAuthenticationCachePolicy() {
    return authenticationCachePolicy;
}
 
Example #10
Source File: ProtobufRedisLoadingCache.java    From curiostack with MIT License 3 votes vote down vote up
/**
 * Constructs a new {@link ProtobufRedisLoadingCache} that can write protobuf {@link Message}
 * keys and values to remoteCache, with an optional local cache layer.
 *
 * @param name name of this cache, will be prefixed onto all keys.
 * @param keyPrototype a prototype for the key {@link Message}, usually gotten from {@code
 *     Key.getDefaultInstance()}.
 * @param valuePrototype a prototype for the value {@link Message}, usually gotten from {@code
 *     Value.getDefaultInstance()}.
 * @param redisTtl the time until expiration of a value in the remoteCache cache. The local
 *     cache should be considered in localCacheSpec.
 * @param localCacheSpec a {@link CaffeineSpec} to control the local cache layer. If {@code
 *     null}, local caching will be disabled.
 */
public <K extends Message, V extends Message> ProtobufRedisLoadingCache<K, V> create(
    String name,
    K keyPrototype,
    V valuePrototype,
    Duration redisTtl,
    @Nullable CaffeineSpec localCacheSpec) {
  return create(name, keyPrototype, valuePrototype, redisTtl, false, localCacheSpec);
}
 
Example #11
Source File: CaffeineCacheManager.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Set the {@link CaffeineSpec} to use for building each individual
 * {@link CaffeineCache} instance.
 * @see #createNativeCaffeineCache
 * @see com.github.benmanes.caffeine.cache.Caffeine#from(CaffeineSpec)
 */
public void setCaffeineSpec(CaffeineSpec caffeineSpec) {
	doSetCaffeine(Caffeine.from(caffeineSpec));
}
 
Example #12
Source File: CaffeineCacheManager.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Set the {@link CaffeineSpec} to use for building each individual
 * {@link CaffeineCache} instance.
 * @see #createNativeCaffeineCache
 * @see com.github.benmanes.caffeine.cache.Caffeine#from(CaffeineSpec)
 */
public void setCaffeineSpec(CaffeineSpec caffeineSpec) {
	doSetCaffeine(Caffeine.from(caffeineSpec));
}
 
Example #13
Source File: CaffeineCacheManager.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Set the {@link CaffeineSpec} to use for building each individual
 * {@link CaffeineCache} instance.
 * @see #createNativeCaffeineCache
 * @see com.github.benmanes.caffeine.cache.Caffeine#from(CaffeineSpec)
 */
public void setCaffeineSpec(CaffeineSpec caffeineSpec) {
	doSetCaffeine(Caffeine.from(caffeineSpec));
}