From: Michael D. <mik...@us...> - 2004-10-26 21:15:59
|
Update of /cvsroot/nhibernate/nhibernate/src/NHibernate/Cache In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3143 Modified Files: HashtableCache.cs ICache.cs ICacheConcurrencyStrategy.cs NonstrictReadWriteCache.cs ReadOnlyCache.cs ReadWriteCache.cs Added Files: HashtableCacheProvider.cs Log Message: modifications made to make more progress towards a plugabble cache added some more logging to it. Index: NonstrictReadWriteCache.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Cache/NonstrictReadWriteCache.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** NonstrictReadWriteCache.cs 6 May 2004 20:53:59 -0000 1.1 --- NonstrictReadWriteCache.cs 26 Oct 2004 21:15:09 -0000 1.2 *************** *** 11,19 **** private static readonly long timeout = 10000; ! private readonly ICache cache; public NonstrictReadWriteCache(ICache cache) { ! this.cache = cache; } --- 11,19 ---- private static readonly long timeout = 10000; ! private ICache _cache; public NonstrictReadWriteCache(ICache cache) { ! _cache = cache; } *************** *** 22,31 **** public object Get(object key, long txTimestamp) { ! object result = cache.Get(key); if( result!=null & !(result is Int64) ) { ! if (log.IsDebugEnabled) log.Debug("Cache hit: " + key); return result; } return null; --- 22,46 ---- public object Get(object key, long txTimestamp) { ! if( log.IsDebugEnabled ) ! { ! log.Debug( "Cache lookup: " + key ); ! } ! ! object result = _cache.Get(key); if( result!=null & !(result is Int64) ) { ! if (log.IsDebugEnabled) ! { ! log.Debug( "Cache hit" ); ! } return result; } + else + { + if( log.IsDebugEnabled ) + { + log.Debug( "Cache miss" ); + } + } return null; *************** *** 34,47 **** public bool Put(object key, object value, long txTimestamp) { ! object result = cache.Get(key); ! if(result==null) { ! if (log.IsDebugEnabled) log.Debug("Caching new: " + key); } ! else if ( (result is Int64) && ( (Int64)result < txTimestamp / Timestamper.OneMs ) ) { // note that this is not guaranteed to be correct in a cluster // because system times could be inconsistent ! if(log.IsDebugEnabled) log.Debug("Caching invalidated: " + key); } else --- 49,68 ---- public bool Put(object key, object value, long txTimestamp) { ! object result = _cache.Get( key ); ! if( result==null ) { ! if( log.IsDebugEnabled ) ! { ! log.Debug("Caching new: " + key); ! } } ! else if( (result is Int64) && ( (Int64)result < txTimestamp / Timestamper.OneMs ) ) { // note that this is not guaranteed to be correct in a cluster // because system times could be inconsistent ! if( log.IsDebugEnabled ) ! { ! log.Debug( "Caching invalidated: " + key ); ! } } else *************** *** 50,54 **** } ! cache.Put(key, value); return true; } --- 71,75 ---- } ! _cache.Put( key, value ); return true; } *************** *** 57,81 **** { // in case the server crashes, we need the lock to timeout ! cache.Put( key, ( timeout + Timestamper.Next() / Timestamper.OneMs ) ); } public void Release(object key) { ! if(log.IsDebugEnabled) log.Debug("Invalidating: " + key); //remove the lock (any later transactions can recache) ! cache.Put(key, Timestamper.Next() / Timestamper.OneMs); } public void Remove(object key) { ! if(log.IsDebugEnabled) log.Debug("Removing: " + key); ! cache.Remove(key); } public void Clear() { ! if(log.IsDebugEnabled) log.Debug("Clearing"); ! cache.Clear(); } --- 78,111 ---- { // in case the server crashes, we need the lock to timeout ! _cache.Put( key, ( timeout + Timestamper.Next() / Timestamper.OneMs ) ); } public void Release(object key) { ! if( log.IsDebugEnabled ) ! { ! log.Debug( "Invalidating: " + key ); ! } //remove the lock (any later transactions can recache) ! _cache.Put( key, Timestamper.Next() / Timestamper.OneMs ); } public void Remove(object key) { ! if( log.IsDebugEnabled ) ! { ! log.Debug( "Removing: " + key ); ! } ! _cache.Remove( key ); } public void Clear() { ! if( log.IsDebugEnabled ) ! { ! log.Debug( "Clearing" ); ! } ! _cache.Clear(); } *************** *** 84,95 **** try { ! cache.Destroy(); } catch(Exception e) { ! log.Warn("Could not destroy cache", e); } } #endregion } --- 114,131 ---- try { ! _cache.Destroy(); } catch(Exception e) { ! log.Warn( "Could not destroy cache", e ); } } + public ICache Cache + { + get { return _cache; } + set { _cache = value; } + } + #endregion } Index: ICacheConcurrencyStrategy.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Cache/ICacheConcurrencyStrategy.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** ICacheConcurrencyStrategy.cs 6 May 2004 20:53:59 -0000 1.2 --- ICacheConcurrencyStrategy.cs 26 Oct 2004 21:15:09 -0000 1.3 *************** *** 71,74 **** --- 71,80 ---- /// <exception cref="CacheException"></exception> void Destroy(); + + /// <summary> + /// Gets or sets the <see cref="ICache"/> for this strategy to use. + /// </summary> + /// <value>The <see cref="ICache"/> for this strategy to use.</value> + ICache Cache { get; set ;} } } Index: ReadWriteCache.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Cache/ReadWriteCache.cs,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** ReadWriteCache.cs 6 May 2004 20:53:59 -0000 1.3 --- ReadWriteCache.cs 26 Oct 2004 21:15:09 -0000 1.4 *************** *** 2,7 **** using System.Runtime.CompilerServices; ! namespace NHibernate.Cache { ! /// <summary> /// Caches data that is sometimes updated while maintaining "read committed" --- 2,7 ---- using System.Runtime.CompilerServices; ! namespace NHibernate.Cache ! { /// <summary> /// Caches data that is sometimes updated while maintaining "read committed" *************** *** 13,45 **** public class ReadWriteCache : ICacheConcurrencyStrategy { ! private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(ReadWriteCache)); ! private readonly ICache cache; public ReadWriteCache(ICache cache) { ! this.cache = cache; } #region ICacheConcurrencyStrategy Members - [MethodImpl(MethodImplOptions.Synchronized)] public object Get(object key, long txTimestamp) { ! if (log.IsDebugEnabled) log.Debug("Cache lookup: " + key); ! ! CachedItem item = cache.Get(key) as CachedItem; ! if ( ! item!=null && ! item.FreshTimestamp < txTimestamp && ! item.IsFresh // || txTimestamp < item.LockTimestamp ! ) ! { ! if (log.IsDebugEnabled) log.Debug("Cache hit: " + key); ! return item.Value; ! } ! else { ! if (log.IsDebugEnabled) log.Debug("Cache miss: " + key); ! return null; } } --- 13,57 ---- public class ReadWriteCache : ICacheConcurrencyStrategy { ! private static readonly log4net.ILog log = log4net.LogManager.GetLogger( typeof(ReadWriteCache) ); ! private readonly object lockObject = new object(); ! private ICache _cache; public ReadWriteCache(ICache cache) { ! _cache = cache; } #region ICacheConcurrencyStrategy Members public object Get(object key, long txTimestamp) { ! lock( lockObject ) { ! if( log.IsDebugEnabled ) ! { ! log.Debug("Cache lookup: " + key); ! } ! ! CachedItem item = _cache.Get(key) as CachedItem; ! if ( ! item!=null && ! item.FreshTimestamp < txTimestamp && ! item.IsFresh // || txTimestamp < item.LockTimestamp ! ) ! { ! if( log.IsDebugEnabled ) ! { ! log.Debug( "Cache hit: " + key ); ! } ! return item.Value; ! } ! else ! { ! if( log.IsDebugEnabled ) ! { ! log.Debug( "Cache miss: " + key ); ! } ! return null; ! } } } *************** *** 48,98 **** // in this class until unlocked - [MethodImpl(MethodImplOptions.Synchronized)] public void Lock(object key) { ! if (log.IsDebugEnabled) log.Debug("Invalidating: " + key); ! CachedItem item = cache.Get(key) as CachedItem; ! if ( item==null) item = new CachedItem(null); ! item.Lock(); ! cache.Put(key, item); } - [MethodImpl(MethodImplOptions.Synchronized)] public bool Put(object key, object value, long txTimestamp) { ! if (log.IsDebugEnabled) log.Debug("Caching: " + key); ! ! CachedItem item = cache.Get(key) as CachedItem; ! if ( ! item==null || ! (item.IsUnlocked && !item.IsFresh && item.UnlockTimestamp < txTimestamp) ! ) ! { ! cache.Put(key, new CachedItem(value) ); ! if (log.IsDebugEnabled) log.Debug("Cached: " + key); ! return true; ! } ! else { ! if (log.IsDebugEnabled) log.Debug("Could not cache: " + key); ! return false; } } - [MethodImpl(MethodImplOptions.Synchronized)] public void Release(object key) { ! if (log.IsDebugEnabled) log.Debug("Releasing: " + key); ! ! CachedItem item = cache.Get(key) as CachedItem; ! if (item != null) ! { ! item.Unlock(); ! cache.Put(key, item); ! } ! else { ! log.Warn("An item was expired by the cache while it was locked"); } } --- 60,130 ---- // in this class until unlocked public void Lock(object key) { ! lock( lockObject ) ! { ! if( log.IsDebugEnabled ) ! { ! log.Debug("Invalidating: " + key); ! } ! CachedItem item = _cache.Get( key ) as CachedItem; ! if ( item==null ) item = new CachedItem( null ); ! item.Lock(); ! _cache.Put( key, item ); ! } } public bool Put(object key, object value, long txTimestamp) { ! lock( lockObject ) { ! if( log.IsDebugEnabled ) ! { ! log.Debug( "Caching: " + key ); ! } ! ! CachedItem item = _cache.Get( key ) as CachedItem; ! if ( ! item==null || ! (item.IsUnlocked && !item.IsFresh && item.UnlockTimestamp < txTimestamp) ! ) ! { ! _cache.Put( key, new CachedItem( value ) ); ! if( log.IsDebugEnabled ) ! { ! log.Debug( "Cached: " + key ); ! } ! return true; ! } ! else ! { ! if( log.IsDebugEnabled ) ! { ! log.Debug( "Could not cache: " + key ); ! } ! return false; ! } } } public void Release(object key) { ! lock( lockObject ) { ! if( log.IsDebugEnabled ) ! { ! log.Debug( "Releasing: " + key ); ! } ! CachedItem item = _cache.Get( key ) as CachedItem; ! if( item!=null ) ! { ! item.Unlock(); ! _cache.Put( key, item ); ! } ! else ! { ! log.Warn( "An item was expired by the cache while it was locked" ); ! } } } *************** *** 100,109 **** public void Clear() { ! cache.Clear(); } public void Remove(object key) { ! cache.Remove(key); } --- 132,141 ---- public void Clear() { ! _cache.Clear(); } public void Remove(object key) { ! _cache.Remove( key ); } *************** *** 112,123 **** try { ! cache.Destroy(); } catch(Exception e) { ! log.Warn("Could not destroy cache", e); } } #endregion --- 144,161 ---- try { ! _cache.Destroy(); } catch(Exception e) { ! log.Warn( "Could not destroy cache", e ); } } + public ICache Cache + { + get { return _cache; } + set { _cache = value; } + } + #endregion Index: HashtableCache.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Cache/HashtableCache.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** HashtableCache.cs 20 Aug 2004 02:34:20 -0000 1.2 --- HashtableCache.cs 26 Oct 2004 21:15:07 -0000 1.3 *************** *** 54,69 **** } ! public void Lock( object key ) ! { ! } ! ! public void Unlock( object key ) ! { ! } ! ! public long NextTimestamp() ! { ! return Timestamper.Next(); ! } #endregion --- 54,70 ---- } ! // were added in h2.1 ! // public void Lock( object key ) ! // { ! // } ! // ! // public void Unlock( object key ) ! // { ! // } ! // ! // public long NextTimestamp() ! // { ! // return Timestamper.Next(); ! // } #endregion Index: ReadOnlyCache.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Cache/ReadOnlyCache.cs,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** ReadOnlyCache.cs 6 May 2004 20:53:59 -0000 1.4 --- ReadOnlyCache.cs 26 Oct 2004 21:15:09 -0000 1.5 *************** *** 10,29 **** { private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(ReadOnlyCache)); ! private readonly ICache cache; ! public ReadOnlyCache(ICache cache) { ! this.cache = cache; } #region ICacheConcurrencyStrategy Members - [MethodImpl(MethodImplOptions.Synchronized)] public object Get(object key, long timestamp) { ! object result = cache.Get(key); ! if ( result!=null && log.IsDebugEnabled) log.Debug("Cache hit: " + key); ! return result; } --- 10,44 ---- { private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(ReadOnlyCache)); ! ! private object lockObject = new object(); ! private ICache _cache; public ReadOnlyCache(ICache cache) { ! _cache = cache; } #region ICacheConcurrencyStrategy Members public object Get(object key, long timestamp) { ! lock( lockObject ) ! { ! if( log.IsDebugEnabled ) ! { ! log.Debug( "Cache lookup: " + key ); ! } ! ! object result = _cache.Get( key ); ! if ( result!=null && log.IsDebugEnabled ) ! { ! log.Debug( "Cache hit" ); ! } ! else if( log.IsDebugEnabled ) ! { ! log.Debug( "Cache miss" ); ! } ! return result; ! } } *************** *** 34,42 **** } - [MethodImpl(MethodImplOptions.Synchronized)] public bool Put(object key, object value, long timestamp) { ! if (log.IsDebugEnabled) log.Debug("Caching: " + key); ! cache.Put(key, value); ! return true; } --- 49,63 ---- } public bool Put(object key, object value, long timestamp) { ! ! lock( lockObject ) ! { ! if (log.IsDebugEnabled) ! { ! log.Debug("Caching: " + key); ! } ! _cache.Put(key, value); ! return true; ! } } *************** *** 49,58 **** public void Clear() { ! cache.Clear(); } public void Remove(object key) { ! cache.Remove(key); } --- 70,79 ---- public void Clear() { ! _cache.Clear(); } public void Remove(object key) { ! _cache.Remove(key); } *************** *** 61,65 **** try { ! cache.Destroy(); } catch(Exception e) --- 82,86 ---- try { ! _cache.Destroy(); } catch(Exception e) *************** *** 68,71 **** --- 89,98 ---- } } + + public ICache Cache + { + get { return _cache; } + set { _cache = value; } + } #endregion Index: ICache.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Cache/ICache.cs,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** ICache.cs 20 Aug 2004 02:34:20 -0000 1.3 --- ICache.cs 26 Oct 2004 21:15:09 -0000 1.4 *************** *** 50,59 **** /// <exception cref="CacheException"></exception> string Region {set;} ! ! void Lock( object key ); ! ! void Unlock( object key ); ! ! long NextTimestamp(); } --- 50,60 ---- /// <exception cref="CacheException"></exception> string Region {set;} ! ! // were added in h2.1 ! // void Lock( object key ); ! // ! // void Unlock( object key ); ! // ! // long NextTimestamp(); } --- NEW FILE: HashtableCacheProvider.cs --- using System; using System.Collections; namespace NHibernate.Cache { /// <summary> /// Cache Provider plugin for NHibernate that is configured by using /// <c>hibernate.cache.provider_class="NHibernate.Cache.HashtableCacheProvider"</c> /// </summary> public class HashtableCacheProvider : ICacheProvider { #region ICacheProvider Members public ICache BuildCache(string regionName, ICollection properties) { return new HashtableCache( regionName ); } public long NextTimestamp() { return Timestamper.Next(); } #endregion } } |