19 using System.Collections.Generic;
21 using Lucene.Net.Support;
23 namespace Lucene.Net.Search
43 protected internal const int DEFAULT_CACHE_CLEAN_SIZE = 100;
45 protected internal const long DEFAULT_CACHE_SLEEP_TIME = 1000 * 60 * 10;
48 protected internal IDictionary<int, FilterItem> cache;
50 protected internal int cacheCleanSize;
52 protected internal long cleanSleepTime;
54 protected internal FilterCleaner internalFilterCleaner;
56 private static readonly
object _staticSyncObj =
new object();
71 cache =
new HashMap<int, FilterItem>();
72 cacheCleanSize = DEFAULT_CACHE_CLEAN_SIZE;
73 cleanSleepTime = DEFAULT_CACHE_SLEEP_TIME;
75 internalFilterCleaner =
new FilterCleaner(
this);
84 public virtual void SetCacheSize(
int value)
86 this.cacheCleanSize = value;
91 public virtual void SetCleanThreadSleepTime(
long value)
93 this.cleanSleepTime = value;
109 FilterItem fi = null;
110 fi = cache[filter.GetHashCode()];
113 fi.timestamp = System.DateTime.UtcNow.Ticks;
116 cache[filter.GetHashCode()] =
new FilterItem(filter);
125 protected internal class FilterItem
128 public long timestamp;
130 public FilterItem(
Filter filter)
132 this.filter = filter;
133 this.timestamp = System.DateTime.UtcNow.Ticks;
153 private class FilterItemComparer : IComparer<KeyValuePair<int, FilterItem>>
155 #region IComparer<FilterItem> Members
157 public int Compare(KeyValuePair<int, FilterItem> x, KeyValuePair<int, FilterItem> y)
159 return x.Value.timestamp.CompareTo(y.Value.timestamp);
165 private bool running =
true;
166 private FilterManager manager;
167 private ISet<KeyValuePair<int, FilterItem>> sortedFilterItems;
169 public FilterCleaner(FilterManager enclosingInstance)
171 this.manager = enclosingInstance;
172 sortedFilterItems =
new SortedSet<KeyValuePair<int, FilterItem>>(
new FilterItemComparer());
175 public virtual void Run()
181 if (this.manager.cache.Count >
this.manager.cacheCleanSize)
184 sortedFilterItems.Clear();
185 lock (this.manager.cache)
187 sortedFilterItems.UnionWith(this.manager.cache);
188 int numToDelete = (int)((this.manager.cache.Count -
this.manager.cacheCleanSize) * 1.5);
191 sortedFilterItems.ExceptWith(sortedFilterItems.Take(numToDelete).ToArray());
194 sortedFilterItems.Clear();
197 System.Threading.Thread.Sleep(
new System.TimeSpan((System.Int64)10000 *
this.manager.cleanSleepTime));