HHH-7866 Avoid redundant log level checking in StandardQueryCache

This commit is contained in:
Sanne Grinovero 2012-12-14 15:43:52 +00:00 committed by Brett Meyer
parent bcf73bd32c
commit 5cf1b469dd
1 changed files with 25 additions and 22 deletions

View File

@ -61,7 +61,7 @@ public class StandardQueryCache implements QueryCache {
StandardQueryCache.class.getName() StandardQueryCache.class.getName()
); );
private static final boolean tracing = LOG.isTraceEnabled(); private static final boolean tracing = LOG.isTraceEnabled();
private QueryResultsRegion cacheRegion; private QueryResultsRegion cacheRegion;
private UpdateTimestampsCache updateTimestampsCache; private UpdateTimestampsCache updateTimestampsCache;
@ -88,13 +88,13 @@ public class StandardQueryCache implements QueryCache {
this.updateTimestampsCache = updateTimestampsCache; this.updateTimestampsCache = updateTimestampsCache;
} }
@SuppressWarnings({ "UnnecessaryBoxing", "unchecked" }) @SuppressWarnings({ "unchecked" })
public boolean put( public boolean put(
QueryKey key, final QueryKey key,
Type[] returnTypes, final Type[] returnTypes,
List result, final List result,
boolean isNaturalKeyLookup, final boolean isNaturalKeyLookup,
SessionImplementor session) throws HibernateException { final SessionImplementor session) throws HibernateException {
if ( isNaturalKeyLookup && result.isEmpty() ) { if ( isNaturalKeyLookup && result.isEmpty() ) {
return false; return false;
} }
@ -103,7 +103,9 @@ public class StandardQueryCache implements QueryCache {
LOG.debugf( "Caching query results in region: %s; timestamp=%s", cacheRegion.getName(), ts ); LOG.debugf( "Caching query results in region: %s; timestamp=%s", cacheRegion.getName(), ts );
List cacheable = new ArrayList( result.size() + 1 ); List cacheable = new ArrayList( result.size() + 1 );
logCachedResultDetails( key, null, returnTypes, cacheable ); if ( tracing ) {
logCachedResultDetails( key, null, returnTypes, cacheable );
}
cacheable.add( ts ); cacheable.add( ts );
final boolean singleResult = returnTypes.length == 1; final boolean singleResult = returnTypes.length == 1;
for ( Object aResult : result ) { for ( Object aResult : result ) {
@ -113,7 +115,7 @@ public class StandardQueryCache implements QueryCache {
null null
) : TypeHelper.disassemble( (Object[]) aResult, returnTypes, null, session, null ); ) : TypeHelper.disassemble( (Object[]) aResult, returnTypes, null, session, null );
cacheable.add( cacheItem ); cacheable.add( cacheItem );
logCachedResultRowDetails( returnTypes, aResult ); if ( tracing ) logCachedResultRowDetails( returnTypes, aResult );
} }
cacheRegion.put( key, cacheable ); cacheRegion.put( key, cacheable );
@ -122,28 +124,29 @@ public class StandardQueryCache implements QueryCache {
@SuppressWarnings({ "unchecked" }) @SuppressWarnings({ "unchecked" })
public List get( public List get(
QueryKey key, final QueryKey key,
Type[] returnTypes, final Type[] returnTypes,
boolean isNaturalKeyLookup, final boolean isNaturalKeyLookup,
Set spaces, final Set spaces,
SessionImplementor session) throws HibernateException { final SessionImplementor session) throws HibernateException {
LOG.debugf( "Checking cached query results in region: %s", cacheRegion.getName() ); final boolean debugEnabled = LOG.isDebugEnabled();
if ( debugEnabled ) LOG.debugf( "Checking cached query results in region: %s", cacheRegion.getName() );
List cacheable = (List) cacheRegion.get( key ); List cacheable = (List) cacheRegion.get( key );
logCachedResultDetails( key, spaces, returnTypes, cacheable ); if ( tracing ) logCachedResultDetails( key, spaces, returnTypes, cacheable );
if ( cacheable == null ) { if ( cacheable == null ) {
LOG.debug( "Query results were not found in cache" ); if ( debugEnabled ) LOG.debug( "Query results were not found in cache" );
return null; return null;
} }
Long timestamp = (Long) cacheable.get( 0 ); Long timestamp = (Long) cacheable.get( 0 );
if ( !isNaturalKeyLookup && !isUpToDate( spaces, timestamp ) ) { if ( !isNaturalKeyLookup && !isUpToDate( spaces, timestamp ) ) {
LOG.debug( "Cached query results were not up-to-date" ); if ( debugEnabled ) LOG.debug( "Cached query results were not up-to-date" );
return null; return null;
} }
LOG.debug( "Returning cached query results" ); if ( debugEnabled ) LOG.debug( "Returning cached query results" );
final boolean singleResult = returnTypes.length == 1; final boolean singleResult = returnTypes.length == 1;
for ( int i = 1; i < cacheable.size(); i++ ) { for ( int i = 1; i < cacheable.size(); i++ ) {
if ( singleResult ) { if ( singleResult ) {
@ -164,7 +167,7 @@ public class StandardQueryCache implements QueryCache {
TypeHelper.assemble( (Serializable[]) cacheable.get( i ), returnTypes, session, null ) TypeHelper.assemble( (Serializable[]) cacheable.get( i ), returnTypes, session, null )
); );
} }
logCachedResultRowDetails( returnTypes, result.get( i - 1 ) ); if ( tracing ) logCachedResultRowDetails( returnTypes, result.get( i - 1 ) );
} }
catch ( RuntimeException ex ) { catch ( RuntimeException ex ) {
if ( isNaturalKeyLookup && if ( isNaturalKeyLookup &&
@ -174,7 +177,7 @@ public class StandardQueryCache implements QueryCache {
// the uoe could occur while resolving // the uoe could occur while resolving
// associations, leaving the PC in an // associations, leaving the PC in an
// inconsistent state // inconsistent state
LOG.debug( "Unable to reassemble cached result set" ); if ( debugEnabled ) LOG.debug( "Unable to reassemble cached result set" );
cacheRegion.evict( key ); cacheRegion.evict( key );
return null; return null;
} }
@ -184,7 +187,7 @@ public class StandardQueryCache implements QueryCache {
return result; return result;
} }
protected boolean isUpToDate(Set spaces, Long timestamp) { protected boolean isUpToDate(final Set spaces, final Long timestamp) {
LOG.debugf( "Checking query spaces are up-to-date: %s", spaces ); LOG.debugf( "Checking query spaces are up-to-date: %s", spaces );
return updateTimestampsCache.isUpToDate( spaces, timestamp ); return updateTimestampsCache.isUpToDate( spaces, timestamp );
} }