All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hibernate.cache.StandardQueryCache Maven / Gradle / Ivy

//$Id: StandardQueryCache.java 10118 2006-07-13 21:38:41Z [email protected] $
package org.hibernate.cache;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.hibernate.HibernateException;
import org.hibernate.UnresolvableObjectException;
import org.hibernate.cfg.Settings;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.type.Type;
import org.hibernate.type.TypeFactory;

/**
 * The standard implementation of the Hibernate QueryCache interface.  This
 * implementation is very good at recognizing stale query results and
 * and re-running queries when it detects this condition, recaching the new
 * results.
 *
 * @author Gavin King
 */
public class StandardQueryCache implements QueryCache {

	private static final Log log = LogFactory.getLog( StandardQueryCache.class );

	private Cache queryCache;
	private UpdateTimestampsCache updateTimestampsCache;
	private final String regionName;

	public void clear() throws CacheException {
		queryCache.clear();
	}

	public StandardQueryCache(
			final Settings settings, 
			final Properties props, 
			final UpdateTimestampsCache updateTimestampsCache, 
			String regionName) throws HibernateException {
		if ( regionName == null ) {
			regionName = StandardQueryCache.class.getName();
		}
		String prefix = settings.getCacheRegionPrefix();
		if ( prefix != null ) {
			regionName = prefix + '.' + regionName;
		}
		log.info( "starting query cache at region: " + regionName );

		this.queryCache = settings.getCacheProvider().buildCache(regionName, props);
		this.updateTimestampsCache = updateTimestampsCache;
		this.regionName = regionName;
	}

	public boolean put(
			QueryKey key,
			Type[] returnTypes,
			List result,
			boolean isNaturalKeyLookup,
			SessionImplementor session) throws HibernateException {
		
		if ( isNaturalKeyLookup && result.size()==0 ) {
			return false;
		}
		else {
			Long ts = new Long( session.getTimestamp() );

			if ( log.isDebugEnabled() ) {
				log.debug( "caching query results in region: " + regionName + "; timestamp=" + ts );
			}
			
			List cacheable = new ArrayList( result.size()+1 );
			cacheable.add( ts );
			for ( int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy