/** * Retrieves the default cache settings from the configuration resource. * * @param config the configuration resource * @param <K> the type of keys maintained the cache * @param <V> the type of cached values * @return the default configuration for a cache */ public static <K, V> CaffeineConfiguration<K, V> defaults(Config config) { return new Configurator<K, V>(config, "default").configure(); }
/** Determines if the cache should operate in read through mode. */ private boolean isReadThrough() { return config.isReadThrough() && (config.getCacheLoaderFactory() != null); }
/** @return if the cache writer factory is specified. */ public boolean hasCacheWriter() { return getCacheWriterFactory() != null; }
/** Returns a configuration built from the external settings. */ CaffeineConfiguration<K, V> configure() { addKeyValueTypes(); addStoreByValue(); addListeners(); addReadThrough(); addWriteThrough(); addMonitoring(); addLazyExpiration(); addEagerExpiration(); addRefresh(); addMaximum(); return configuration; }
static void checkTestCache(CaffeineConfiguration<?, ?> config) { checkStoreByValue(config); checkListener(config); assertThat(config.getKeyType(), is(Object.class)); assertThat(config.getValueType(), is(Object.class)); assertThat(config.getCacheLoaderFactory().create(), instanceOf(TestCacheLoader.class)); assertThat(config.getCacheWriter(), instanceOf(TestCacheWriter.class)); assertThat(config.isStatisticsEnabled(), is(true)); assertThat(config.isManagementEnabled(), is(true)); checkSize(config); checkRefresh(config); checkLazyExpiration(config); checkEagerExpiration(config); }
/** Adds the read through settings. */ private void addReadThrough() { configuration.setReadThrough(merged.getBoolean("read-through.enabled")); if (isSet("read-through.loader")) { configuration.setCacheLoaderFactory(factoryCreator.factoryOf( merged.getString("read-through.loader"))); } }
/** Adds the write through settings. */ private void addWriteThrough() { configuration.setWriteThrough(merged.getBoolean("write-through.enabled")); if (isSet("write-through.writer")) { configuration.setCacheWriterFactory(factoryCreator.factoryOf( merged.getString("write-through.writer"))); } }
/** Adds the maximum size and weight bounding settings. */ private void addMaximum() { if (isSet("policy.maximum.size")) { configuration.setMaximumSize(OptionalLong.of(merged.getLong("policy.maximum.size"))); } if (isSet("policy.maximum.weight")) { configuration.setMaximumWeight(OptionalLong.of(merged.getLong("policy.maximum.weight"))); } if (isSet("policy.maximum.weigher")) { configuration.setWeigherFactory(Optional.of( FactoryBuilder.factoryOf(merged.getString("policy.maximum.weigher")))); } }
/** Adds the store-by-value settings. */ private void addStoreByValue() { configuration.setStoreByValue(merged.getBoolean("store-by-value.enabled")); if (isSet("store-by-value.strategy")) { configuration.setCopierFactory(factoryCreator.factoryOf( merged.getString("store-by-value.strategy"))); } }
@Override protected CaffeineConfiguration<Integer, Integer> getConfiguration() { CaffeineConfiguration<Integer, Integer> configuration = new CaffeineConfiguration<>(); configuration.setExpiryFactory(Optional.of(() -> expiry)); configuration.setTickerFactory(() -> ticker::read); return configuration; }
@Override protected CaffeineConfiguration<Integer, Integer> getConfiguration() { CaffeineConfiguration<Integer, Integer> configuration = new CaffeineConfiguration<>(); configuration.setExpiryPolicyFactory(() -> new ModifiedExpiryPolicy( new Duration(TimeUnit.MILLISECONDS, EXPIRY_DURATION))); configuration.setTickerFactory(() -> ticker::read); return configuration; }
/** The loading configuration used by the test. */ protected CaffeineConfiguration<Integer, Integer> getLoadingConfiguration() { CaffeineConfiguration<Integer, Integer> configuration = getConfiguration(); configuration.setCacheLoaderFactory(this::getCacheLoader); configuration.setReadThrough(true); return configuration; }
/** Adds the entry listeners settings. */ private void addListeners() { for (String path : merged.getStringList("listeners")) { Config listener = root.getConfig(path); Factory<? extends CacheEntryListener<? super K, ? super V>> listenerFactory = factoryCreator.factoryOf(listener.getString("class")); Factory<? extends CacheEntryEventFilter<? super K, ? super V>> filterFactory = null; if (listener.hasPath("filter")) { filterFactory = factoryCreator.factoryOf(listener.getString("filter")); } boolean oldValueRequired = listener.getBoolean("old-value-required"); boolean synchronous = listener.getBoolean("synchronous"); configuration.addCacheEntryListenerConfiguration( new MutableCacheEntryListenerConfiguration<>( listenerFactory, filterFactory, oldValueRequired, synchronous)); } }
/** Adds the JMX monitoring settings. */ private void addMonitoring() { configuration.setStatisticsEnabled(merged.getBoolean("monitoring.statistics")); configuration.setManagementEnabled(merged.getBoolean("monitoring.management")); }
/** Adds the Caffeine refresh settings. */ public void addRefresh() { if (isSet("policy.refresh.after-write")) { long nanos = merged.getDuration("policy.refresh.after-write", NANOSECONDS); configuration.setRefreshAfterWrite(OptionalLong.of(nanos)); } }
/** @return a writer created by the configured factory or null if not set. */ public @Nullable CacheWriter<K , V> getCacheWriter() { if (hasCacheWriter()) { @SuppressWarnings("unchecked") CacheWriter<K , V> writer = (CacheWriter<K, V>) getCacheWriterFactory().create(); return writer; } return null; }
@Override protected CaffeineConfiguration<Integer, Integer> getConfiguration() { return new CaffeineConfiguration<>(); }
/** * Returns a if the cache definition is found in the external settings file. * * @param cacheName the name of the cache * @return {@code true} if a definition exists */ public boolean isDefinedExternally(String cacheName) { return TypesafeConfigurator.cacheNames(rootConfig).contains(cacheName); }
@AfterClass public void afterClass() { TypesafeConfigurator.setFactoryCreator(FactoryBuilder::factoryOf); }
@Override protected CaffeineConfiguration<Integer, Integer> getConfiguration() { return new CaffeineConfiguration<>(); }