From 03c9a24ebf7bee3ae767236b1fd3ae0ce8fccec6 Mon Sep 17 00:00:00 2001 From: Jan Srnicek Date: Thu, 3 Nov 2016 09:37:28 +0100 Subject: HONEYCOMB-259 - CacheKeyFactory Provides logic for creating scoped keys Change-Id: I126bcb9255b4f8a3f2585f50e6e718948581e7f0 Signed-off-by: Jan Srnicek --- .../util/read/cache/DumpCacheManager.java | 34 +++++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) (limited to 'infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/DumpCacheManager.java') diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/DumpCacheManager.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/DumpCacheManager.java index f5895038e..f1b265dec 100644 --- a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/DumpCacheManager.java +++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/DumpCacheManager.java @@ -30,6 +30,9 @@ import org.slf4j.LoggerFactory; /** * Manager responsible for returning Data object dumps
either from cache or by invoking specified {@link * EntityDumpExecutor} + * + * @param Type of data returned by {@code EntityDumpExecutor},and stored cache + * @param Type of dumping params */ public final class DumpCacheManager { @@ -37,27 +40,29 @@ public final class DumpCacheManager { private final EntityDumpExecutor dumpExecutor; private final EntityDumpPostProcessingFunction postProcessor; + private final CacheKeyFactory cacheKeyFactory; private DumpCacheManager(DumpCacheManagerBuilder builder) { this.dumpExecutor = builder.dumpExecutor; this.postProcessor = builder.postProcessingFunction; + this.cacheKeyFactory = builder.cacheKeyFactory; } /** * Returns {@link Optional} of dump * * @param identifier identifier for origin of dumping context - * @param entityKey key that defines scope for caching * @param cache modification cache of current transaction * @param dumpParams parameters to configure dump request * @throws ReadFailedException if execution of dumping request failed - * @returns If present in cache ,returns cached instance, if not, tries to dump data using provided executor, otherwise - * Optional.absent() + * @returns If present in cache ,returns cached instance, if not, tries to dump data using provided executor, + * otherwise Optional.absent() */ - public Optional getDump(@Nonnull final InstanceIdentifier identifier, @Nonnull String entityKey, - @Nonnull ModificationCache cache, final U dumpParams) + public Optional getDump(@Nonnull final InstanceIdentifier identifier, + @Nonnull final ModificationCache cache, final U dumpParams) throws ReadFailedException { + final String entityKey = this.cacheKeyFactory.createKey(identifier); // this key binding to every log has its logic ,because every customizer have its own cache manager and if // there is need for debugging/fixing some complex call with a lot of data,you can get lost in those logs LOG.debug("Loading dump for KEY[{}]", entityKey); @@ -81,29 +86,42 @@ public final class DumpCacheManager { public static final class DumpCacheManagerBuilder { + private static final CacheKeyFactory DEFAULT_CACHE_KEY_FACTORY_INSTANCE = new IdentifierCacheKeyFactory(); + private EntityDumpExecutor dumpExecutor; private EntityDumpPostProcessingFunction postProcessingFunction; + private CacheKeyFactory cacheKeyFactory; public DumpCacheManagerBuilder() { // for cases when user does not set specific post-processor postProcessingFunction = new NoopDumpPostProcessingFunction(); + + //use no additional scopes version by default + cacheKeyFactory = DEFAULT_CACHE_KEY_FACTORY_INSTANCE; } - public DumpCacheManagerBuilder withExecutor(@Nonnull EntityDumpExecutor executor) { + public DumpCacheManagerBuilder withExecutor(@Nonnull final EntityDumpExecutor executor) { this.dumpExecutor = executor; return this; } public DumpCacheManagerBuilder withPostProcessingFunction( - EntityDumpPostProcessingFunction postProcessingFunction) { + @Nonnull final EntityDumpPostProcessingFunction postProcessingFunction) { this.postProcessingFunction = postProcessingFunction; return this; } + public DumpCacheManagerBuilder withCacheKeyFactory(@Nonnull final CacheKeyFactory cacheKeyFactory) { + this.cacheKeyFactory = cacheKeyFactory; + return this; + } + public DumpCacheManager build() { checkNotNull(dumpExecutor, "Dump executor cannot be null"); checkNotNull(postProcessingFunction, - "Dump post-processor cannot be null cannot be null, default implementation is used if its not set"); + "Dump post-processor cannot be null cannot be null, default implementation is used when not set explicitly"); + checkNotNull(cacheKeyFactory, + "Cache key factory cannot be null, default non-extended implementation is used when not set explicitly"); return new DumpCacheManager<>(this); } -- cgit 1.2.3-korg