Commits

Jordan Earls committed f432f19

Added interface for CacheDictionary
Fixed some concurrency issues in ICacheDictionary

Comments (0)

Files changed (3)

CacheGen/CacheDictionary.cs

 		void Add (K key, V value)
 		{
 			string realkey=RealKeys.GetOrAdd(key, (s) => AddKey(key));
-			StoreTo(realkey, value, Info);
+			lock(realkey)
+			{
+				StoreTo(realkey, value, Info);
+			}
 		}
 		public V Remove (K key)
 		{
 				{
 					return default(V);
 				}
-				object tmp=GetFrom(realkey);
-				if(tmp!=null && tmp is V)
+				lock(realkey)
 				{
-					return (V)tmp;
-				}
-				else
-				{
-					string trash=null;
-					RealKeys.TryRemove(key, out trash); //cleanup
-					return default(V);
+					object tmp=GetFrom(realkey);
+					if(tmp!=null && tmp is V)
+					{
+						return (V)tmp;
+					}
+					else
+					{
+						string trash=null;
+						RealKeys.TryRemove(key, out trash); //cleanup
+						return default(V);
+					}
 				}
 			}
 			set {
 		}
 		public void Clear ()
 		{
-			lock(RealKeys)
+			foreach(var key in RealKeys.Keys)
 			{
-				foreach(var key in RealKeys.Keys)
+				var realkey=GetKey(key);
+				if(realkey!=null) //ConcurrentDictionary's enumator represents a snapshot, so while iterating, the key may no longer exist
 				{
-					//don't worry about concurrency here. Iterating over the collection is so non-thread-safe it's not even funny. 
 					StoreTo(GetKey(key), null, Info);
 				}
-				RealKeys.Clear();
 			}
+			RealKeys.Clear();
 		}
 	}
 }

CacheGen/CacheGen.csproj

     <Compile Include="CacheDictionary.cs" />
     <Compile Include="BaseCacheClass.cs" />
     <Compile Include="ASPCacheMechanism.cs" />
+    <Compile Include="ICacheDictionary.cs" />
   </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <ItemGroup>

CacheGen/ICacheDictionary.cs

+using System;
+
+namespace Earlz.CacheGen
+{
+	public interface ICacheDictionary<K,V>
+	{
+		CacheObject CacheInfo
+		{
+			get;set;
+		}
+		V this[K key]
+		{
+			get;
+			set;
+		}
+		V Remove(K key);
+		V Add(K key, V value, CacheObject info);
+		V Set(K key, V value, CacheObject info);
+		void Clear();
+
+	}
+}
+