Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.launchdarkly.client.utils;
import com.google.common.base.Optional;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.CacheStats;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableMap;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.launchdarkly.client.FeatureStore;
import com.launchdarkly.client.FeatureStoreCacheConfig;
import com.launchdarkly.client.VersionedData;
import com.launchdarkly.client.VersionedDataKind;
import com.launchdarkly.client.integrations.CacheMonitor;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* CachingStoreWrapper is a partial implementation of {@link FeatureStore} that delegates the basic
* functionality to an instance of {@link FeatureStoreCore}. It provides optional caching behavior and
* other logic that would otherwise be repeated in every feature store implementation. This makes it
* easier to create new database integrations by implementing only the database-specific logic.
*
* Construct instances of this class with {@link CachingStoreWrapper#builder(FeatureStoreCore)}.
*
* @since 4.6.0
* @deprecated Referencing this class directly is deprecated; it is now part of the implementation
* of {@link com.launchdarkly.client.integrations.PersistentDataStoreBuilder} and will be made
* package-private starting in version 5.0.
*/
@Deprecated
public class CachingStoreWrapper implements FeatureStore {
private static final String CACHE_REFRESH_THREAD_POOL_NAME_FORMAT = "CachingStoreWrapper-refresher-pool-%d";
private final FeatureStoreCore core;
private final FeatureStoreCacheConfig caching;
private final LoadingCache> itemCache;
private final LoadingCache, ImmutableMap> allCache;
private final LoadingCache initCache;
private final AtomicBoolean inited = new AtomicBoolean(false);
private final ListeningExecutorService executorService;
/**
* Creates a new builder.
* @param core the {@link FeatureStoreCore} instance
* @return the builder
*/
public static CachingStoreWrapper.Builder builder(FeatureStoreCore core) {
return new Builder(core);
}
protected CachingStoreWrapper(final FeatureStoreCore core, FeatureStoreCacheConfig caching, CacheMonitor cacheMonitor) {
this.core = core;
this.caching = caching;
if (!caching.isEnabled()) {
itemCache = null;
allCache = null;
initCache = null;
executorService = null;
} else {
CacheLoader> itemLoader = new CacheLoader>() {
@Override
public Optional load(CacheKey key) throws Exception {
return Optional.fromNullable(core.getInternal(key.kind, key.key));
}
};
CacheLoader, ImmutableMap> allLoader = new CacheLoader, ImmutableMap>() {
@Override
public ImmutableMap load(VersionedDataKind> kind) throws Exception {
return itemsOnlyIfNotDeleted(core.getAllInternal(kind));
}
};
CacheLoader initLoader = new CacheLoader() {
@Override
public Boolean load(String key) throws Exception {
return core.initializedInternal();
}
};
if (caching.getStaleValuesPolicy() == FeatureStoreCacheConfig.StaleValuesPolicy.REFRESH_ASYNC) {
ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat(CACHE_REFRESH_THREAD_POOL_NAME_FORMAT).setDaemon(true).build();
ExecutorService parentExecutor = Executors.newSingleThreadExecutor(threadFactory);
executorService = MoreExecutors.listeningDecorator(parentExecutor);
// Note that the REFRESH_ASYNC mode is only used for itemCache, not allCache, since retrieving all flags is
// less frequently needed and we don't want to incur the extra overhead.
itemLoader = CacheLoader.asyncReloading(itemLoader, executorService);
} else {
executorService = null;
}
itemCache = newCacheBuilder(caching, cacheMonitor).build(itemLoader);
allCache = newCacheBuilder(caching, cacheMonitor).build(allLoader);
initCache = newCacheBuilder(caching, cacheMonitor).build(initLoader);
if (cacheMonitor != null) {
cacheMonitor.setSource(new CacheStatsSource());
}
}
}
private static CacheBuilder