Class ContextCache
- java.lang.Object
-
- org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache
-
public class ContextCache extends Object
Client-side, time-expiring singleton cache of the full Context tree, indexed by UUID and by path, used by client libraries to resolve Context identity without a server round-trip on every call. Populated/refreshed via a pluggableContextCacheRenewalstrategy and automatically renewed onceexpiringTimeoutelapses.- Author:
- Luca Frosini (ISTI - CNR)
-
-
Field Summary
Fields Modifier and Type Field Description protected ContextCacheRenewalcontextCacheRenewalThe context cache renewal.protected Map<String,UUID>contextPathToUUIDThe context path to UUID.protected List<org.gcube.informationsystem.contexts.reference.entities.Context>contextsThe contexts.protected org.gcube.informationsystem.dag.Dag<org.gcube.informationsystem.contexts.reference.entities.Context>contextsTreeThe contexts tree.protected CalendarcreationTimeThe creation time.static longDEFAULT_EXPIRING_TIMEOUTDefault cache validity period, in milliseconds: 6 hours.protected CalendarexpiringTimeThe expiring time.static intexpiringTimeoutThe currently configured cache validity period, in milliseconds; overridable viasetExpiringTimeout(int).protected booleaninitializedThe initialized.static longMISS_RENEWAL_INTERVALThe shortest time between two renewals caused by a missing Context, in milliseconds: 1 minute.protected Map<String,UUID>previousPathToUUIDThe previous paths of the cached Contexts, each to the Context that holds it: a path that is the current path of a Context is never here, since the current path always wins.protected static ContextCachesingletonThe singleton.protected Map<UUID,org.gcube.informationsystem.contexts.reference.entities.Context>uuidToContextThe uuid to context.protected Map<UUID,String>uuidToContextPathThe uuid to context path.
-
Constructor Summary
Constructors Constructor Description ContextCache()Instantiates a new context cache.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcleanCache()Clean cache.protected voidcleanCache(Calendar calendar)Clean cache.org.gcube.informationsystem.contexts.reference.entities.ContextgetContextByPath(String contextPath)Gets the context by path.org.gcube.informationsystem.contexts.reference.entities.ContextgetContextByUUID(String uuid)Gets the context by UUID.org.gcube.informationsystem.contexts.reference.entities.ContextgetContextByUUID(UUID uuid)Gets the context by UUID.ContextCacheRenewalgetContextCacheRenewal()Gets the context cache renewal.protected StringgetContextPath(org.gcube.informationsystem.contexts.reference.entities.Context context)Gets the context path.StringgetContextPathByUUID(String uuid)Gets the context path by UUID.StringgetContextPathByUUID(UUID uuid)Gets the context path by UUID.Map<String,UUID>getContextPathToUUIDAssociation()Gets the context path to UUID association.List<org.gcube.informationsystem.contexts.reference.entities.Context>getContexts()Gets the contexts.org.gcube.informationsystem.dag.Dag<org.gcube.informationsystem.contexts.reference.entities.Context>getContextsTree()Gets the contexts tree.static ContextCachegetInstance()Gets the single instance of ContextCache.UUIDgetUUIDByPath(String contextPath)Gets the UUID of the Context a path names: its current path first, then one of its previous paths, as the Resource Registry resolves a token that still names a moved or renamed Context.Map<UUID,String>getUUIDToContextPathAssociation()Gets the UUID to context path association.voidrefreshContextsIfNeeded()Refresh contexts if needed.voidrenew()Renew.protected booleanrenewOnMiss()Renews the cache when a lookup misses, unless the last renewal is more recent thanMISS_RENEWAL_INTERVAL.voidsetContextCacheRenewal(ContextCacheRenewal contextCacheRenewal)Sets the context cache renewal.protected voidsetContexts(Calendar calendar, List<org.gcube.informationsystem.contexts.reference.entities.Context> contexts)Sets the contexts.voidsetContexts(List<org.gcube.informationsystem.contexts.reference.entities.Context> contexts)Sets the contexts.static voidsetExpiringTimeout(int expiringTimeout)Sets the expiring timeout.
-
-
-
Field Detail
-
DEFAULT_EXPIRING_TIMEOUT
public static final long DEFAULT_EXPIRING_TIMEOUT
Default cache validity period, in milliseconds: 6 hours.
-
MISS_RENEWAL_INTERVAL
public static final long MISS_RENEWAL_INTERVAL
The shortest time between two renewals caused by a missing Context, in milliseconds: 1 minute. A Context created or reached after the last renewal is found at the first lookup that misses it, without waiting forexpiringTimeout; a Context that does not exist costs at most one renewal a minute.
-
expiringTimeout
public static int expiringTimeout
The currently configured cache validity period, in milliseconds; overridable viasetExpiringTimeout(int).
-
singleton
protected static ContextCache singleton
The singleton.
-
contextCacheRenewal
protected ContextCacheRenewal contextCacheRenewal
The context cache renewal.
-
creationTime
protected Calendar creationTime
The creation time.
-
expiringTime
protected Calendar expiringTime
The expiring time.
-
contexts
protected List<org.gcube.informationsystem.contexts.reference.entities.Context> contexts
The contexts.
-
uuidToContext
protected Map<UUID,org.gcube.informationsystem.contexts.reference.entities.Context> uuidToContext
The uuid to context.
-
previousPathToUUID
protected Map<String,UUID> previousPathToUUID
The previous paths of the cached Contexts, each to the Context that holds it: a path that is the current path of a Context is never here, since the current path always wins.
-
contextsTree
protected org.gcube.informationsystem.dag.Dag<org.gcube.informationsystem.contexts.reference.entities.Context> contextsTree
The contexts tree.
-
initialized
protected boolean initialized
The initialized.
-
-
Method Detail
-
setExpiringTimeout
public static void setExpiringTimeout(int expiringTimeout)
Sets the expiring timeout.- Parameters:
expiringTimeout- the new expiring timeout
-
getInstance
public static ContextCache getInstance()
Gets the single instance of ContextCache.- Returns:
- single instance of ContextCache
-
cleanCache
public void cleanCache()
Clean cache.
-
cleanCache
protected void cleanCache(Calendar calendar)
Clean cache.- Parameters:
calendar- the calendar
-
renew
public void renew() throws ResourceRegistryExceptionRenew.- Throws:
ResourceRegistryException- the resource registry exception
-
getContextCacheRenewal
public ContextCacheRenewal getContextCacheRenewal()
Gets the context cache renewal.- Returns:
- the context cache renewal
-
setContextCacheRenewal
public void setContextCacheRenewal(ContextCacheRenewal contextCacheRenewal)
Sets the context cache renewal.- Parameters:
contextCacheRenewal- the new context cache renewal
-
renewOnMiss
protected boolean renewOnMiss() throws ResourceRegistryExceptionRenews the cache when a lookup misses, unless the last renewal is more recent thanMISS_RENEWAL_INTERVAL.- Returns:
trueif the cache has been renewed, so that the lookup is worth repeating- Throws:
ResourceRegistryException- the resource registry exception
-
refreshContextsIfNeeded
public void refreshContextsIfNeeded() throws ResourceRegistryExceptionRefresh contexts if needed.- Throws:
ResourceRegistryException- the resource registry exception
-
getContexts
public List<org.gcube.informationsystem.contexts.reference.entities.Context> getContexts() throws ResourceRegistryException
Gets the contexts.- Returns:
- the contexts
- Throws:
ResourceRegistryException- the resource registry exception
-
setContexts
public void setContexts(List<org.gcube.informationsystem.contexts.reference.entities.Context> contexts)
Sets the contexts.- Parameters:
contexts- the new contexts
-
setContexts
protected void setContexts(Calendar calendar, List<org.gcube.informationsystem.contexts.reference.entities.Context> contexts)
Sets the contexts.- Parameters:
calendar- the calendarcontexts- the contexts
-
getContextPath
protected String getContextPath(org.gcube.informationsystem.contexts.reference.entities.Context context)
Gets the context path.- Parameters:
context- the context- Returns:
- the context path
-
getContextPathByUUID
public String getContextPathByUUID(UUID uuid) throws ResourceRegistryException
Gets the context path by UUID.- Parameters:
uuid- the uuid- Returns:
- the context path by UUID
- Throws:
ResourceRegistryException- the resource registry exception
-
getContextPathByUUID
public String getContextPathByUUID(String uuid) throws ResourceRegistryException
Gets the context path by UUID.- Parameters:
uuid- the uuid- Returns:
- the context path by UUID
- Throws:
ResourceRegistryException- the resource registry exception
-
getUUIDByPath
public UUID getUUIDByPath(String contextPath) throws ResourceRegistryException
Gets the UUID of the Context a path names: its current path first, then one of its previous paths, as the Resource Registry resolves a token that still names a moved or renamed Context. A path found neither way renews the cache (seerenewOnMiss()).- Parameters:
contextPath- the context path- Returns:
- the uuid by path, or
nullif no Context has that path, current or previous - Throws:
ResourceRegistryException- the resource registry exception
-
getContextByUUID
public org.gcube.informationsystem.contexts.reference.entities.Context getContextByUUID(UUID uuid) throws ResourceRegistryException
Gets the context by UUID.- Parameters:
uuid- the uuid- Returns:
- the context by UUID
- Throws:
ResourceRegistryException- the resource registry exception
-
getContextByUUID
public org.gcube.informationsystem.contexts.reference.entities.Context getContextByUUID(String uuid) throws ResourceRegistryException
Gets the context by UUID.- Parameters:
uuid- the uuid- Returns:
- the context by UUID
- Throws:
ResourceRegistryException- the resource registry exception
-
getContextByPath
public org.gcube.informationsystem.contexts.reference.entities.Context getContextByPath(String contextPath) throws ResourceRegistryException
Gets the context by path.- Parameters:
contextPath- the context path- Returns:
- the context by path
- Throws:
ResourceRegistryException- the resource registry exception
-
getUUIDToContextPathAssociation
public Map<UUID,String> getUUIDToContextPathAssociation() throws ResourceRegistryException
Gets the UUID to context path association.- Returns:
- an Map containing UUID to Context path association
- Throws:
ResourceRegistryException- the resource registry exception
-
getContextPathToUUIDAssociation
public Map<String,UUID> getContextPathToUUIDAssociation() throws ResourceRegistryException
Gets the context path to UUID association.- Returns:
- an Map containing Context path to UUID association
- Throws:
ResourceRegistryException- the resource registry exception
-
getContextsTree
public org.gcube.informationsystem.dag.Dag<org.gcube.informationsystem.contexts.reference.entities.Context> getContextsTree() throws ResourceRegistryExceptionGets the contexts tree.- Returns:
- the contexts tree
- Throws:
ResourceRegistryException- the resource registry exception
-
-