[java-oidc-common] branch main updated: Add explicit cache operation mode enum

Phil Smart philip.smart at jisc.ac.uk
Fri Oct 15 07:36:26 UTC 2021


This is an automated email from the git hooks/post-receive script.

philsmart pushed a commit to branch main
in repository java-oidc-common.

View the commit online:
http://git.shibboleth.net/view/?p=java-oidc-common.git;a=commit;h=d6247bdb5513fa8fc6fa03ad1a6b470a636f6224

The following commit(s) were added to refs/heads/main by this push:
     new d6247bd  Add explicit cache operation mode enum
d6247bd is described below

commit d6247bdb5513fa8fc6fa03ad1a6b470a636f6224
Author: Phil Smart <philip.smart at jisc.ac.uk>
AuthorDate: Fri Oct 15 08:36:23 2021 +0100

    Add explicit cache operation mode enum
---
 .../metadata/cache/impl/DynamicMetadataCache.java  | 10 ++--
 .../metadata/cache/impl/MetadataCacheBuilder.java  | 65 ++++++++++++++--------
 .../impl/OIDCProviderMetadataCacheFactoryBean.java |  8 ++-
 3 files changed, 52 insertions(+), 31 deletions(-)

diff --git a/oidc-common-metadata-impl/src/main/java/net/shibboleth/oidc/metadata/cache/impl/DynamicMetadataCache.java b/oidc-common-metadata-impl/src/main/java/net/shibboleth/oidc/metadata/cache/impl/DynamicMetadataCache.java
index d91a9eb..67a9c19 100644
--- a/oidc-common-metadata-impl/src/main/java/net/shibboleth/oidc/metadata/cache/impl/DynamicMetadataCache.java
+++ b/oidc-common-metadata-impl/src/main/java/net/shibboleth/oidc/metadata/cache/impl/DynamicMetadataCache.java
@@ -235,8 +235,8 @@ public class DynamicMetadataCache<IdentifierType, MetadataType>
     }
     
     /**
-     * Fetch metadata using the supplied fetch function, filter the metadata, then save it to the
-     * backing store - updating the management data at the same time.
+     * Fetch metadata using the supplied fetch function, then save it to the
+     * backing store.
      * 
      * @param mgmtData the metadata management data.
      * @param identifier the identifier of the metadata to fetch.
@@ -284,9 +284,9 @@ public class DynamicMetadataCache<IdentifierType, MetadataType>
      * <li>Write the metadata to the backing store (cache).</li>
      * <li>Update the metadata's management information with new refresh information.</li>
      * </ol>
-     * @param mgmtData
-     * @param metadata
-     * @param expectedIdentifier
+     * @param mgmtData the management data.
+     * @param metadata the new metadata to process.
+     * @param expectedIdentifier the identifier expected on the new metadata.
      */
     private void storeNewMetadata(@Nonnull final MetadataManagementData<IdentifierType> mgmtData,
             @Nonnull final MetadataType metadata,
diff --git a/oidc-common-metadata-impl/src/main/java/net/shibboleth/oidc/metadata/cache/impl/MetadataCacheBuilder.java b/oidc-common-metadata-impl/src/main/java/net/shibboleth/oidc/metadata/cache/impl/MetadataCacheBuilder.java
index 063a0cf..3fc6918 100644
--- a/oidc-common-metadata-impl/src/main/java/net/shibboleth/oidc/metadata/cache/impl/MetadataCacheBuilder.java
+++ b/oidc-common-metadata-impl/src/main/java/net/shibboleth/oidc/metadata/cache/impl/MetadataCacheBuilder.java
@@ -43,7 +43,25 @@ import net.shibboleth.utilities.java.support.resolver.CriteriaSet;
  * @param <T> the metadata identifier/key
  * @param <U> the metadata type. 
  */
-public abstract class MetadataCacheBuilder<T,U> extends AbstractFactoryBean<MetadataCache<U>>{
+public abstract class MetadataCacheBuilder<T,U> extends AbstractFactoryBean<MetadataCache<U>> {
+    
+    
+    /** Which mode of operation to support.*/
+    public enum CacheOperationMode {
+        
+        /** 
+         * A batch mode of operation where caches are completely reloaded
+         * on refresh using refresh-ahead semantics.
+         * Satisfied by constructing a {@link BatchMetadataCache}.
+         */
+        BATCH,
+        
+        /**
+         * A dynamic mode of operation where individual entries are updated
+         * when stale or not found using a read-through semantic.
+         */
+        DYNAMIC,
+    }
     
     /** Maximum cache duration. */
     @Nonnull private Duration maxCacheDuration;
@@ -103,11 +121,8 @@ public abstract class MetadataCacheBuilder<T,U> extends AbstractFactoryBean<Meta
     /** The function to use to load metadata. Applicable for {@link BatchMetadataCache} types.*/
     @Nullable private Function<CacheLoadingContext, byte[]> loadingStrategy;
     
-    /** 
-     * Should the metadata be background refreshed ahead of time. If true, a {@link BatchMetadataCache}
-     * will be constructed.
-     */
-    private boolean refreshAhead;
+    /** Which cache mode of operation to use.*/
+    @Nonnull private CacheOperationMode cacheOperationMode;
     
     
     
@@ -124,9 +139,28 @@ public abstract class MetadataCacheBuilder<T,U> extends AbstractFactoryBean<Meta
         minRefreshDelay = Duration.ofMinutes(5);
         removeIdleEntityData = true;
         // create a default direct in/out filter
-        metadataFilterStrategy = (metadata, context) -> metadata;
+        metadataFilterStrategy = (metadata, context) -> metadata;        
+        // create batch mode by default
+        cacheOperationMode = CacheOperationMode.BATCH;
     }
     
+    /**
+     * Set the cache operation mode.
+     * 
+     * @param mode the cache operation mode.
+     */
+    public void setCacheOperationMode(@Nonnull final CacheOperationMode mode) {
+        cacheOperationMode = Constraint.isNotNull(mode, "Cache operation mode can not be null");
+    }
+    
+    /**
+     * Get the cache operation mode.
+     * 
+     * @return the cache operation mode.
+     */
+    @Nonnull protected CacheOperationMode getCacheOperationMode() {
+        return cacheOperationMode;
+    }
     
     /**
      * Get the min delay to wait before refreshing metadata.
@@ -202,23 +236,6 @@ public abstract class MetadataCacheBuilder<T,U> extends AbstractFactoryBean<Meta
         maxRefreshDelay = delay;
     }
     
-    /**
-     * Should the metadata be refreshed ahead of time?
-     * 
-     * @return true if the metadata should be refreshed ahead of time. False otherwise.
-     */
-    public boolean isRefreshAhead() {
-        return refreshAhead;
-    }
-    
-    /**
-     * Should the metadata be refreshed ahead of time.
-     * @param refresh
-     */
-    public void setRefreshAhead(final boolean refresh) {
-        refreshAhead = refresh;
-    }
-    
     /**
      * Set the metadata fetching strategy.
      * 
diff --git a/oidc-common-metadata-impl/src/main/java/net/shibboleth/oidc/metadata/cache/impl/OIDCProviderMetadataCacheFactoryBean.java b/oidc-common-metadata-impl/src/main/java/net/shibboleth/oidc/metadata/cache/impl/OIDCProviderMetadataCacheFactoryBean.java
index 3c7d6cb..e6690e3 100644
--- a/oidc-common-metadata-impl/src/main/java/net/shibboleth/oidc/metadata/cache/impl/OIDCProviderMetadataCacheFactoryBean.java
+++ b/oidc-common-metadata-impl/src/main/java/net/shibboleth/oidc/metadata/cache/impl/OIDCProviderMetadataCacheFactoryBean.java
@@ -24,7 +24,7 @@ public class OIDCProviderMetadataCacheFactoryBean extends MetadataCacheBuilder<I
         
         // batch refresh-ahead is only supported by the batch cache type. 
         //TODO change to explicit batch mode v dynamic mode.
-        if (!isRefreshAhead()) {
+        if (getCacheOperationMode() == CacheOperationMode.DYNAMIC) {
             final DynamicMetadataCache<Issuer, OIDCProviderMetadata> cache = new DynamicMetadataCache<>(
                     new DefaultDynamicBackingStore<>(getMaxCacheDuration()), getFetchStrategy());
             cache.setMinCacheDuration(getMinCacheDuration());
@@ -42,7 +42,7 @@ public class OIDCProviderMetadataCacheFactoryBean extends MetadataCacheBuilder<I
             cache.setId("OIDCProviderDynamicMetadataCache");
             cache.initialize();
             return cache;
-        } else {
+        } else if (getCacheOperationMode() == CacheOperationMode.BATCH) {
             //TODO why have the backing stores as interfaces if you are setting the concrete type here? inject somehow?
             final BatchMetadataCache<Issuer, OIDCProviderMetadata> cache = 
                     new BatchMetadataCache<>(
@@ -60,7 +60,11 @@ public class OIDCProviderMetadataCacheFactoryBean extends MetadataCacheBuilder<I
             cache.setId("OIDCProviderBatchMetadataCache");
             cache.initialize();
             return cache;
+        } else {
+            //should never get here.
+            throw new Exception("Cache mode of operation not supported");
         }
+        
     }
    
 

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list