29 using System.Collections.Generic;
32 namespace OpenSim.Framework
72 public class CacheItemBase : IEquatable<CacheItemBase>, IComparable<CacheItemBase>
77 public DateTime expires =
new DateTime(0);
85 public virtual void Store(Object data)
92 entered = DateTime.Now;
99 entered = DateTime.Now;
106 return uuid == item.uuid;
111 return uuid.CompareTo(item.uuid);
124 private Object m_Data;
153 public override void Store(Object data)
191 public override void Store(Object data)
205 private List<CacheItemBase> m_Index =
new List<CacheItemBase>();
210 private Dictionary<string, CacheItemBase> m_Lookup =
211 new Dictionary<string, CacheItemBase>();
216 private int m_Size = 1024;
217 private TimeSpan m_DefaultTTL =
new TimeSpan(0);
222 private class SortLRU : IComparer<CacheItemBase>
226 if (a == null && b == null)
241 m_Strategy = CacheStrategy.Balanced;
242 m_Medium = CacheMedium.Memory;
257 this(medium, strategy, 0)
274 m_Strategy = strategy;
283 get { lock (m_Index) {
return m_Index.Count; } }
290 get {
return m_Size; }
291 set { SetSize(value); }
294 private void SetSize(
int newSize)
301 m_Index.Sort(
new SortLRU());
304 m_Index.RemoveRange(newSize, Count - newSize);
310 m_Lookup[item.uuid] = item;
314 public TimeSpan DefaultTTL
316 get {
return m_DefaultTTL; }
317 set { m_DefaultTTL = value; }
328 if (m_Lookup.ContainsKey(index))
329 item = m_Lookup[index];
338 item.lastUsed = DateTime.Now;
349 public virtual Object
Get(
string index)
356 return item.Retrieve();
364 Object item = Get(index);
368 Object data = fetch(index);
376 if (!m_Index.Contains(missing))
378 m_Index.Add(missing);
379 m_Lookup[index] = missing;
393 public Object
Find(Predicate<CacheItemBase> d)
398 item = m_Index.Find(d);
403 return item.Retrieve();
406 public virtual void Store(
string index, Object data)
412 case CacheMedium.Memory:
415 case CacheMedium.File:
421 Store(index, data, container);
424 public virtual void Store(
string index, Object data, Type container)
426 Store(index, data, container,
new Object[] { index });
429 public virtual void Store(
string index, Object data, Type container,
442 item = GetItem(index);
445 item.lastUsed = DateTime.Now;
446 if (m_DefaultTTL.Ticks != 0)
447 item.expires = DateTime.Now + m_DefaultTTL;
457 if (m_DefaultTTL.Ticks != 0)
458 item.expires = DateTime.Now + m_DefaultTTL;
461 m_Lookup[index] = item;
474 protected virtual void Expire(
bool getting)
479 if (m_DefaultTTL.Ticks != 0)
481 DateTime now= DateTime.Now;
483 foreach (
CacheItemBase item
in new List<CacheItemBase>(m_Index))
488 m_Index.Remove(item);
489 m_Lookup.Remove(item.uuid);
496 case CacheStrategy.Aggressive:
500 m_Index.Sort(
new SortLRU());
503 int target = (int)((
float)
Size * 0.9);
509 if (doExpire != null)
511 List<CacheItemBase> candidates =
512 m_Index.GetRange(target, Count - target);
516 if (doExpire(i.
uuid))
519 m_Lookup.Remove(i.uuid);
525 m_Index.RemoveRange(target, Count - target);
530 m_Lookup[item.uuid] = item;
544 if (!m_Lookup.ContainsKey(uuid))
548 m_Lookup.Remove(uuid);
549 m_Index.Remove(item);
virtual void Store(string index, Object data, Type container, Object[] parameters)
virtual void Store(string index, Object data)
override Object Retrieve()
delegate Object FetchDelegate(string index)
void Invalidate(string uuid)
MemoryCacheItem(string index)
override Object Retrieve()
FileCacheItem(string index, DateTime ttl, Object data)
override void Store(Object data)
virtual void Store(Object data)
virtual Object Retrieve()
FileCacheItem(string index, DateTime ttl)
Object Find(Predicate< CacheItemBase > d)
Cache(CacheMedium medium, CacheFlags flags)
virtual bool Equals(CacheItemBase item)
delegate bool ExpireDelegate(string index)
CacheItemBase(string index, DateTime ttl)
override void Store(Object data)
virtual void Store(string index, Object data, Type container)
MemoryCacheItem(string index, DateTime ttl)
MemoryCacheItem(string index, Object data)
FileCacheItem(string index)
FileCacheItem(string index, Object data)
CacheItemBase(string index)
virtual Object Get(string index)
Cache(CacheMedium medium, CacheStrategy strategy, CacheFlags flags)
Cache(CacheMedium medium)
MemoryCacheItem(string index, DateTime ttl, Object data)
virtual Object Get(string index, FetchDelegate fetch)
virtual int CompareTo(CacheItemBase item)
Cache(CacheMedium medium, CacheStrategy strategy)
virtual CacheItemBase GetItem(string index)
virtual void Expire(bool getting)
Expire items as appropriate.
Cache(CacheStrategy strategy, CacheFlags flags)