public abstract class AbstractCachingSqlQueryResolver extends java.lang.Object implements SqlQueryResolver
SqlQueryResolver
implementations. 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 org.springframework.web.servlet.view.AbstractCachingViewResolver
.
loadSqlQuery(java.lang.String, org.hawaiiframework.sql.AbstractCachingSqlQueryResolver.QueryHolder)
Modifier and Type | Class and Description |
---|---|
protected static class |
AbstractCachingSqlQueryResolver.QueryHolder
QueryHolder for caching.
|
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_CACHE_LIMIT
Default maximum number of entries for the sql query cache (
1024 ). |
Constructor and Description |
---|
AbstractCachingSqlQueryResolver() |
Modifier and Type | Method and Description |
---|---|
void |
clearCache()
Clear the entire sql query cache, removing all cached sql queries.
|
protected void |
doRefreshQueryHolder(java.lang.String sqlQueryName,
AbstractCachingSqlQueryResolver.QueryHolder queryHolder)
Subclasses may override this method to implement their own expiry mechanism.
|
protected java.lang.Object |
getCacheKey(java.lang.String sqlQueryName)
Return the cache key for the given sql query name.
|
int |
getCacheLimit()
Return the maximum number of entries for the sql query cache.
|
boolean |
isCache()
Return if caching is enabled.
|
protected abstract java.lang.String |
loadSqlQuery(java.lang.String sqlQueryName,
AbstractCachingSqlQueryResolver.QueryHolder queryHolder)
Subclasses must implement this method to load the sql query.
|
void |
removeFromCache(java.lang.String sqlQueryName)
Provides functionality to clear the cache for a certain sql query.
|
java.lang.String |
resolveSqlQuery(java.lang.String sqlQueryName)
Resolve the given sql query by name.
|
void |
setCache(boolean cache)
Enable or disable caching.
|
void |
setCacheLimit(int cacheLimit)
Specify the maximum number of entries for the sql query cache.
|
void |
setCacheSeconds(int cacheSeconds)
Set the number of seconds to cache a loaded query.
|
void |
setCacheUnresolved(boolean cacheUnresolved)
Whether a sql query name once resolved to
null should be cached and automatically resolved to null subsequently. |
public static final int DEFAULT_CACHE_LIMIT
1024
).public int getCacheLimit()
public void setCacheLimit(int cacheLimit)
public boolean isCache()
public void setCache(boolean cache)
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.
public void setCacheUnresolved(boolean cacheUnresolved)
null
should be cached and automatically resolved to null
subsequently.
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.
protected java.lang.Object getCacheKey(java.lang.String sqlQueryName)
Default is the sql query name but can be overridden in subclasses.
public void setCacheSeconds(int cacheSeconds)
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.
public void removeFromCache(java.lang.String sqlQueryName)
sqlQueryName
- the sql query name for which the cached sql query (if any) needs to be removedpublic void clearCache()
public java.lang.String resolveSqlQuery(java.lang.String sqlQueryName) throws HawaiiException
SqlQueryResolver
To allow for SqlQueryResolver
chaining, a ViewResolver
should return null
if a sql query with the given name
is not defined in it.
resolveSqlQuery
in interface SqlQueryResolver
sqlQueryName
- name of the sql query to resolvenull
if not found (optional, to allow for SqlQueryResolver
chaining)HawaiiException
- if the sql query could not be resolved (typically in case of problems resolving the sql query)protected void doRefreshQueryHolder(java.lang.String sqlQueryName, AbstractCachingSqlQueryResolver.QueryHolder queryHolder)
This method is only called when the current thread has a lock on the QueryHolder, so subclasses need not deal with thread-safety.
sqlQueryName
- the name of the query to refreshqueryHolder
- the cached QueryHolder to checkprotected abstract java.lang.String loadSqlQuery(java.lang.String sqlQueryName, AbstractCachingSqlQueryResolver.QueryHolder queryHolder) throws HawaiiException
SqlQueryResolver
base class.sqlQueryName
- the name of the sql query to retrievequeryHolder
- the QueryHolder to enrichnull
if not found (optional, to allow for SqlQueryResolver
chaining)HawaiiException
- if the sql query could not be resolved (typically in case of problems resolving the sql query)resolveSqlQuery(java.lang.String)