Author: gbayon
Date: Wed Dec 14 10:37:26 2005
New Revision: 356824

URL: http://svn.apache.org/viewcvs?rev=356824&view=rev
Log:
- Clean Cache code
- Add unit test for IBATISNET-134

Modified:
    
ibatis/trunk/cs/mapper/IBatisNet.DataMapper.Test/NUnit/SqlMapTests/CacheKeyTest.cs
    
ibatis/trunk/cs/mapper/IBatisNet.DataMapper.Test/NUnit/SqlMapTests/CacheTest.cs
    
ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Configuration/Cache/CacheModel.cs
    
ibatis/trunk/cs/mapper/IBatisNet.DataMapper/MappedStatements/CachingStatement.cs

Modified: 
ibatis/trunk/cs/mapper/IBatisNet.DataMapper.Test/NUnit/SqlMapTests/CacheKeyTest.cs
URL: 
http://svn.apache.org/viewcvs/ibatis/trunk/cs/mapper/IBatisNet.DataMapper.Test/NUnit/SqlMapTests/CacheKeyTest.cs?rev=356824&r1=356823&r2=356824&view=diff
==============================================================================
--- 
ibatis/trunk/cs/mapper/IBatisNet.DataMapper.Test/NUnit/SqlMapTests/CacheKeyTest.cs
 (original)
+++ 
ibatis/trunk/cs/mapper/IBatisNet.DataMapper.Test/NUnit/SqlMapTests/CacheKeyTest.cs
 Wed Dec 14 10:37:26 2005
@@ -1,5 +1,6 @@
 
 
+using IBatisNet.DataMapper.Configuration.Cache;
 using NUnit.Framework;
 
 namespace IBatisNet.DataMapper.Test.NUnit.SqlMapTests
@@ -27,11 +28,11 @@
                private static void DoTestClassEquals(long firstLong, long 
secondLong)
                {
                        // Two cache keys are equal except for the parameter.
-                       Configuration.Cache.CacheKey key = new 
Configuration.Cache.CacheKey();
+                       CacheKey key = new CacheKey();
 
                        key.Update(firstLong);
 
-                       Configuration.Cache.CacheKey aDifferentKey = new 
Configuration.Cache.CacheKey();
+                       CacheKey aDifferentKey = new CacheKey();
 
                        key.Update(secondLong);
 
@@ -41,8 +42,8 @@
                [Test]
                public void CacheKeyWithSameHashcode() 
                {
-                       Configuration.Cache.CacheKey key1 = new 
Configuration.Cache.CacheKey();
-                       Configuration.Cache.CacheKey key2 = new 
Configuration.Cache.CacheKey();
+                       CacheKey key1 = new CacheKey();
+                       CacheKey key2 = new CacheKey();
 
                        key1.Update("HS1CS001");
                        key2.Update("HS1D4001");
@@ -54,8 +55,8 @@
                [Test]
                public void CacheKeyWithTwoParamsSameHashcode() 
                {
-                       Configuration.Cache.CacheKey key1 = new 
Configuration.Cache.CacheKey();
-                       Configuration.Cache.CacheKey key2 = new 
Configuration.Cache.CacheKey();
+                       CacheKey key1 = new CacheKey();
+                       CacheKey key2 = new CacheKey();
 
                        key1.Update("HS1CS001");
                        key1.Update("HS1D4001");

Modified: 
ibatis/trunk/cs/mapper/IBatisNet.DataMapper.Test/NUnit/SqlMapTests/CacheTest.cs
URL: 
http://svn.apache.org/viewcvs/ibatis/trunk/cs/mapper/IBatisNet.DataMapper.Test/NUnit/SqlMapTests/CacheTest.cs?rev=356824&r1=356823&r2=356824&view=diff
==============================================================================
--- 
ibatis/trunk/cs/mapper/IBatisNet.DataMapper.Test/NUnit/SqlMapTests/CacheTest.cs 
(original)
+++ 
ibatis/trunk/cs/mapper/IBatisNet.DataMapper.Test/NUnit/SqlMapTests/CacheTest.cs 
Wed Dec 14 10:37:26 2005
@@ -164,9 +164,12 @@
                public void TestCacheNullObject()
                {
                        CacheModel cache = GetCacheModel();
-                       cache["testKey"] = null;
+                       CacheKey key = new CacheKey();
+                       key.Update("testKey");
 
-                       object returnedObject = cache["testKey"];
+                       cache[key] = null;
+
+                       object returnedObject = cache[key];
                        Assert.AreEqual(CacheModel.NULL_OBJECT, returnedObject);
                        
Assert.AreEqual(HashCodeProvider.GetIdentityHashCode(CacheModel.NULL_OBJECT), 
HashCodeProvider.GetIdentityHashCode(returnedObject));
                        Assert.AreEqual(1, cache.HitRatio);
@@ -180,10 +183,13 @@
                public void TestCacheHit() 
                {
                        CacheModel cache = GetCacheModel();
+                       CacheKey key = new CacheKey();
+                       key.Update("testKey");
+
                        string value = "testValue";
-                       cache["testKey"] = value;
+                       cache[key] = value;
 
-                       object returnedObject = cache["testKey"];
+                       object returnedObject = cache[key];
                        Assert.AreEqual(value, returnedObject);
                        
Assert.AreEqual(HashCodeProvider.GetIdentityHashCode(value), 
HashCodeProvider.GetIdentityHashCode(returnedObject));
                        Assert.AreEqual(1, cache.HitRatio);
@@ -196,10 +202,16 @@
                public void TestCacheMiss() 
                {
                        CacheModel cache = GetCacheModel();
+                       CacheKey key = new CacheKey();
+                       key.Update("testKey");
+
                        string value = "testValue";
-                       cache["testKey"] = value;
+                       cache[key] = value;
 
-                       object returnedObject = cache["wrongKey"];
+                       CacheKey wrongKey = new CacheKey();
+                       wrongKey.Update("wrongKey");
+
+                       object returnedObject = cache[wrongKey];
                        Assert.IsTrue(!value.Equals(returnedObject));
                        Assert.IsNull(returnedObject) ;
                        Assert.AreEqual(0, cache.HitRatio);
@@ -212,19 +224,51 @@
                public void TestCacheHitMiss() 
                {
                        CacheModel cache = GetCacheModel();
+                       CacheKey key = new CacheKey();
+                       key.Update("testKey");
+
                        string value = "testValue";
-                       cache["testKey"] = value;
+                       cache[key] = value;
 
-                       object returnedObject = cache["testKey"];
+                       object returnedObject = cache[key];
                        Assert.AreEqual(value, returnedObject);
                        
Assert.AreEqual(HashCodeProvider.GetIdentityHashCode(value), 
HashCodeProvider.GetIdentityHashCode(returnedObject));
 
-                       returnedObject = cache["wrongKey"];
+                       CacheKey wrongKey = new CacheKey();
+                       wrongKey.Update("wrongKey");
+
+                       returnedObject = cache[wrongKey];
                        Assert.IsTrue(!value.Equals(returnedObject));
                        Assert.IsNull(returnedObject) ;
                        Assert.AreEqual(0.5, cache.HitRatio);
                }
 
+
+               /// <summary>
+               /// Test Duplicate Add to Cache
+               /// </summary>
+               /// <remarks>IBATISNET-134</remarks>
+               [Test]
+               public void TestDuplicateAddCache() 
+               {
+                       CacheModel cache = GetCacheModel();
+                       CacheKey key = new CacheKey();
+                       key.Update("testKey");
+                       string value = "testValue";
+
+                       object obj = null;
+                       obj = cache[key];
+                       Assert.IsNull(obj);
+                       obj = cache[key];
+                       Assert.IsNull(obj);
+
+                       cache[key] = value;
+                       cache[key] = value;
+
+                       object returnedObject = cache[key];
+                       Assert.AreEqual(value, returnedObject);
+                       
Assert.AreEqual(HashCodeProvider.GetIdentityHashCode(value), 
HashCodeProvider.GetIdentityHashCode(returnedObject));
+               }
 
                private CacheModel GetCacheModel() 
                {

Modified: 
ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Configuration/Cache/CacheModel.cs
URL: 
http://svn.apache.org/viewcvs/ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Configuration/Cache/CacheModel.cs?rev=356824&r1=356823&r2=356824&view=diff
==============================================================================
--- 
ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Configuration/Cache/CacheModel.cs 
(original)
+++ 
ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Configuration/Cache/CacheModel.cs 
Wed Dec 14 10:37:26 2005
@@ -30,13 +30,13 @@
 using System.Collections;
 using System.Collections.Specialized;
 using System.Reflection;
+using System.Runtime.CompilerServices;
 using System.Xml.Serialization;
 using IBatisNet.Common.Exceptions;
 using IBatisNet.Common.Logging;
 using IBatisNet.Common.Utilities;
 using IBatisNet.DataMapper.Exceptions;
 using IBatisNet.DataMapper.MappedStatements;
-using IBatisNet.DataMapper.Scope;
 
 #endregion
 
@@ -52,6 +52,8 @@
        {
                #region Fields
 
+               private static IDictionary  _lockMap = new HybridDictionary();
+
                [NonSerialized]
                private static readonly ILog _logger = LogManager.GetLogger( 
MethodBase.GetCurrentMethod().DeclaringType );
                /// <summary>
@@ -233,7 +235,7 @@
                /// A side effect of this method is that is may clear the cache
                /// if it has not been cleared in the flushInterval.
                /// </remarks> 
-               public object this [object key] 
+               public object this [CacheKey key] 
                {
                        get
                        {
@@ -246,7 +248,11 @@
                                        }
                                }
 
-                               object value = _controller[key];
+                               object value = null;
+                               lock (GetLock(key)) 
+                               {
+                                       value = _controller[key];
+                               }
 
                                lock(_statLock) 
                                {
@@ -295,5 +301,24 @@
                }
                #endregion
 
+               /// <summary>
+               /// 
+               /// </summary>
+               /// <param name="key"></param>
+               /// <returns></returns>
+               [MethodImpl(MethodImplOptions.Synchronized)]
+               public object GetLock(CacheKey key) 
+               {
+                       int controllerId = 
HashCodeProvider.GetIdentityHashCode(_controller);
+                       int keyHash = key.GetHashCode();
+                       int lockKey = 29 * controllerId + keyHash;
+                       object lok =_lockMap[lockKey];
+                       if (lok == null) 
+                       {
+                               lok = lockKey; //might as well use the same 
object
+                               _lockMap[lockKey] = lok;
+                       }
+                       return lok;
+               }
        }
 }

Modified: 
ibatis/trunk/cs/mapper/IBatisNet.DataMapper/MappedStatements/CachingStatement.cs
URL: 
http://svn.apache.org/viewcvs/ibatis/trunk/cs/mapper/IBatisNet.DataMapper/MappedStatements/CachingStatement.cs?rev=356824&r1=356823&r2=356824&view=diff
==============================================================================
--- 
ibatis/trunk/cs/mapper/IBatisNet.DataMapper/MappedStatements/CachingStatement.cs
 (original)
+++ 
ibatis/trunk/cs/mapper/IBatisNet.DataMapper/MappedStatements/CachingStatement.cs
 Wed Dec 14 10:37:26 2005
@@ -30,11 +30,8 @@
 using IBatisNet.Common;
 using IBatisNet.DataMapper.Commands;
 using IBatisNet.DataMapper.Configuration.Cache;
-using IBatisNet.DataMapper.Configuration.ParameterMapping;
 using IBatisNet.DataMapper.Configuration.Statements;
 using IBatisNet.DataMapper.Scope;
-using IBatisNet.DataMapper.MappedStatements;
-using Cache = IBatisNet.DataMapper.Configuration.Cache;
 
 #endregion 
 
@@ -61,22 +58,6 @@
                        _mappedStatement = statement;
                }
 
-               /// <summary>
-               /// Gets a percentage of successful cache hits achieved
-               /// </summary>
-               /// <returns>The percentage of hits (0-1), or -1 if cache is 
disabled.</returns>
-               public double GetDataCacheHitRatio() 
-               {
-                       if (_mappedStatement.Statement.CacheModel != null) 
-                       {
-                               return 
_mappedStatement.Statement.CacheModel.HitRatio;
-                       } 
-                       else 
-                       {
-                               return -1;
-                       }
-               }
-
                #region IMappedStatement Members
 
                /// <summary>
@@ -130,7 +111,7 @@
 
                        _mappedStatement.PreparedCommand.Create( request, 
session, this.Statement, parameterObject );
 
-                       Cache.CacheKey cacheKey = this.GetCacheKey(request);
+                       CacheKey cacheKey = this.GetCacheKey(request);
                        cacheKey.Update("ExecuteQueryForMap");
                        cacheKey.Update(keyProperty);
                        cacheKey.Update(valueProperty);
@@ -198,7 +179,7 @@
 
                        _mappedStatement.PreparedCommand.Create( request, 
session, this.Statement, parameterObject );
 
-                       Cache.CacheKey cacheKey = this.GetCacheKey(request);
+                       CacheKey cacheKey = this.GetCacheKey(request);
                        cacheKey.Update("ExecuteQueryForList");
                        cacheKey.Update(skipResults);
                        cacheKey.Update(maxResults);
@@ -252,7 +233,7 @@
 
                        _mappedStatement.PreparedCommand.Create( request, 
session, this.Statement, parameterObject );
 
-                       Cache.CacheKey cacheKey = this.GetCacheKey(request);
+                       CacheKey cacheKey = this.GetCacheKey(request);
                        cacheKey.Update("ExecuteQueryForObject");
 
                        obj = this.Statement.CacheModel[cacheKey];
@@ -302,9 +283,25 @@
 
                #endregion
 
-               private Cache.CacheKey GetCacheKey(RequestScope request) 
+               /// <summary>
+               /// Gets a percentage of successful cache hits achieved
+               /// </summary>
+               /// <returns>The percentage of hits (0-1), or -1 if cache is 
disabled.</returns>
+               public double GetDataCacheHitRatio() 
+               {
+                       if (_mappedStatement.Statement.CacheModel != null) 
+                       {
+                               return 
_mappedStatement.Statement.CacheModel.HitRatio;
+                       } 
+                       else 
+                       {
+                               return -1;
+                       }
+               }
+
+               private CacheKey GetCacheKey(RequestScope request) 
                {
-                       Cache.CacheKey cacheKey = new Cache.CacheKey();
+                       CacheKey cacheKey = new CacheKey();
                        for (int i = 0; i < 
request.IDbCommand.Parameters.Count; i++) 
                        {
                                IDataParameter dataParameter = 
(IDataParameter)request.IDbCommand.Parameters[i];


Reply via email to