Class AbstractCachingSqlQueryResolver
- java.lang.Object
-
- org.hawaiiframework.sql.AbstractCachingSqlQueryResolver
-
- All Implemented Interfaces:
SqlQueryResolver
- Direct Known Subclasses:
ResourceSqlQueryResolver
public abstract class AbstractCachingSqlQueryResolver extends Object implements SqlQueryResolver
Convenient base class forSqlQueryResolverimplementations. Caches sql queries once resolved: This means that sql query resolution won't be a performance problem, no matter how costly initial sql query retrieval is.Subclasses need to implement the
loadSqlQuery(java.lang.String, org.hawaiiframework.sql.AbstractCachingSqlQueryResolver.QueryHolder)template method to load the sql query.Note this implementation is based on Spring's
AbstractCachingViewResolver.- Since:
- 2.0.0
- Author:
- Marcel Overdijk, Paul Klos
- See Also:
loadSqlQuery(java.lang.String, org.hawaiiframework.sql.AbstractCachingSqlQueryResolver.QueryHolder)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classAbstractCachingSqlQueryResolver.QueryHolderQueryHolder for caching.
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_CACHE_LIMITDefault maximum number of entries for the sql query cache (1024).
-
Constructor Summary
Constructors Constructor Description AbstractCachingSqlQueryResolver()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidclearCache()Clear the entire sql query cache, removing all cached sql queries.protected voiddoRefreshQueryHolder(String sqlQueryName, AbstractCachingSqlQueryResolver.QueryHolder queryHolder)Subclasses may override this method to implement their own expiry mechanism.protected ObjectgetCacheKey(String sqlQueryName)Return the cache key for the given sql query name.intgetCacheLimit()Return the maximum number of entries for the sql query cache.booleanisCache()Return if caching is enabled.protected abstract StringloadSqlQuery(String sqlQueryName, AbstractCachingSqlQueryResolver.QueryHolder queryHolder)Subclasses must implement this method to load the sql query.voidremoveFromCache(String sqlQueryName)Provides functionality to clear the cache for a certain sql query.StringresolveSqlQuery(String sqlQueryName)Resolve the given sql query by name.voidsetCache(boolean cache)Enable or disable caching.voidsetCacheLimit(int cacheLimit)Specify the maximum number of entries for the sql query cache.voidsetCacheSeconds(int cacheSeconds)Set the number of seconds to cache a loaded query.voidsetCacheUnresolved(boolean cacheUnresolved)Whether a sql query name once resolved tonullshould be cached and automatically resolved tonullsubsequently.
-
-
-
Field Detail
-
DEFAULT_CACHE_LIMIT
public static final int DEFAULT_CACHE_LIMIT
Default maximum number of entries for the sql query cache (1024).- See Also:
- Constant Field Values
-
-
Method Detail
-
getCacheLimit
public int getCacheLimit()
Return the maximum number of entries for the sql query cache.
-
setCacheLimit
public void setCacheLimit(int cacheLimit)
Specify the maximum number of entries for the sql query cache. Default is 1024.
-
isCache
public boolean isCache()
Return if caching is enabled.
-
setCache
public void setCache(boolean cache)
Enable or disable caching.This is equivalent to setting the
"cacheLimit"property to the default limit (1024) or to 0, respectively.Default is "true": caching is enabled. Disable this only for debugging and development.
-
setCacheUnresolved
public void setCacheUnresolved(boolean cacheUnresolved)
Whether a sql query name once resolved tonullshould be cached and automatically resolved tonullsubsequently.Default is "true": unresolved sql query names are being cached. Note that this flag only applies if the general
"cache"flag is kept at its default of "true" as well.
-
getCacheKey
protected Object getCacheKey(String sqlQueryName)
Return the cache key for the given sql query name.Default is the sql query name but can be overridden in subclasses.
-
setCacheSeconds
public void setCacheSeconds(int cacheSeconds)
Set the number of seconds to cache a loaded query.After the cache seconds have expired,
doRefreshQueryHolder(String, QueryHolder)will be called, so even if refreshing a once loaded query is enabled, it is up to the subclass to define the refresh mechanism.Note, setting this to anything other than -1 only makes sense if the queries are loaded from a source other than the classpath.
- Default is "-1", indicating to cache forever.
- A positive number will cache a query for the given number of seconds. This is essentially the interval between refresh checks.
- A value of "0" will check for expiry on each query access!
-
removeFromCache
public void removeFromCache(String sqlQueryName)
Provides functionality to clear the cache for a certain sql query.- Parameters:
sqlQueryName- the sql query name for which the cached sql query (if any) needs to be removed
-
clearCache
public void clearCache()
Clear the entire sql query cache, removing all cached sql queries. Subsequent resolve calls will lead to loading of demanded sql queries.
-
resolveSqlQuery
public String resolveSqlQuery(String sqlQueryName) throws HawaiiException
Description copied from interface:SqlQueryResolverResolve the given sql query by name.To allow for
SqlQueryResolverchaining, aViewResolvershould returnnullif a sql query with the given name is not defined in it.- Specified by:
resolveSqlQueryin interfaceSqlQueryResolver- Parameters:
sqlQueryName- name of the sql query to resolve- Returns:
- the sql query, or
nullif not found (optional, to allow forSqlQueryResolverchaining) - Throws:
HawaiiException- if the sql query could not be resolved (typically in case of problems resolving the sql query)
-
doRefreshQueryHolder
protected void doRefreshQueryHolder(String sqlQueryName, AbstractCachingSqlQueryResolver.QueryHolder queryHolder)
Subclasses may override this method to implement their own expiry mechanism. The default implementation does nothing, i.e. once a query is cached it will never be updated.This method is only called when the current thread has a lock on the QueryHolder, so subclasses need not deal with thread-safety.
- Parameters:
sqlQueryName- the name of the query to refreshqueryHolder- the cached QueryHolder to check
-
loadSqlQuery
protected abstract String loadSqlQuery(String sqlQueryName, AbstractCachingSqlQueryResolver.QueryHolder queryHolder) throws HawaiiException
Subclasses must implement this method to load the sql query. The returned sql queries will be cached by thisSqlQueryResolverbase class.- Parameters:
sqlQueryName- the name of the sql query to retrievequeryHolder- the QueryHolder to enrich- Returns:
- the sql query, or
nullif not found (optional, to allow forSqlQueryResolverchaining) - Throws:
HawaiiException- if the sql query could not be resolved (typically in case of problems resolving the sql query)- See Also:
resolveSqlQuery(java.lang.String)
-
-