19 using System.Collections.Generic;
22 using System.Threading;
24 using Lucene.Net.Index;
26 namespace Lucene.Net.Util.Cache
39 public abstract void Warm(
IndexReader reader,
string key);
53 private Dictionary<WeakKey, Dictionary<string, T>> readerCache =
new Dictionary<WeakKey, Dictionary<string, T>>();
58 private ReaderWriterLockSlim cacheLock =
new ReaderWriterLockSlim();
66 protected abstract T CreateValue(
IndexReader reader,
string key);
73 get {
return this.readerCache.Count; }
83 this.Get(reader, key);
96 Dictionary<string, T> innerCache;
97 T retVal =
default(T);
98 this.cacheLock.EnterReadLock();
101 if (readerCache.TryGetValue(readerRef, out innerCache))
103 innerCache.TryGetValue(key, out retVal);
108 this.cacheLock.ExitReadLock();
113 retVal = this.CreateValue(reader, key);
114 this.cacheLock.EnterWriteLock();
117 if (!readerCache.TryGetValue(readerRef, out innerCache))
119 innerCache =
new Dictionary<string, T>();
120 readerCache.Add(readerRef, innerCache);
122 if (!innerCache.ContainsKey(key))
124 innerCache[key] = retVal;
134 var keys = from wr in this.readerCache.Keys where !wr.IsAlive select wr;
135 List<WeakKey> keysToRemove = keys.ToList();
136 foreach (WeakKey wk
in keysToRemove)
138 this.readerCache.Remove(wk);
143 this.cacheLock.ExitWriteLock();
156 internal class WeakKey : WeakReference
161 private int hashCode;
167 internal WeakKey(
object target)
170 this.hashCode = target.GetHashCode();
177 public override int GetHashCode()
179 return this.hashCode;
187 public override bool Equals(
object obj)
189 WeakKey other = obj as WeakKey;
195 object a = this.Target;
196 object b = other.Target;
198 if (a == null && b == null)
202 else if (a == null || b == null)