18 using System.Collections.Generic;
20 namespace Deveel.Data.Caching {
22 protected LinkedList<KeyValuePair<object, CacheValue>> IndexList {
get;
private set; }
24 private readonly Dictionary<object, CacheValue> valueCache =
new Dictionary<object, CacheValue>();
26 private readonly
object syncRoot =
new object();
27 private DateTime lastCacheAccess = DateTime.MaxValue;
30 IndexList =
new LinkedList<KeyValuePair<object, CacheValue>>();
34 private long totalGets = 0;
35 private long getTotal = 0;
42 IndexList.Remove(idxRef);
44 idxRef =
new LinkedListNode<KeyValuePair<object, CacheValue>>(
new KeyValuePair<object, CacheValue>(key, cacheValue));
48 IndexList.AddFirst(idxRef);
53 return valueCache.TryGetValue(key, out v) ? v : null;
57 lastCacheAccess = DateTime.Now;
58 var cacheValue = GetCacheValueUnlocked(key);
60 if (cacheValue == null) {
62 cacheValue.IsNew =
true;
63 valueCache[key] = cacheValue;
65 cacheValue.Value = value;
66 cacheValue.IsNew =
false;
69 UpdateElementAccess(key, cacheValue);
74 var value = GetCacheValueUnlocked(key);
76 valueCache.Remove(key);
77 IndexList.Remove(value.IndexRef);
84 protected override bool SetObject(
object key,
object value) {
86 var cached = SetValueUnlocked(key, value);
91 protected override bool TryGetObject(
object key, out
object value) {
96 lastCacheAccess = DateTime.Now;
97 v = GetCacheValueUnlocked(key);
100 UpdateElementAccess(key, v);
110 lastCacheAccess = DateTime.Now;
112 return RemoveUnlocked(key);
125 protected override void Dispose(
bool disposing) {
131 base.Dispose(disposing);
138 LastAccess = DateTime.Now;
142 public LinkedListNode<KeyValuePair<object, CacheValue>> IndexRef {
get; set; }
144 public DateTime LastAccess {
get; set; }
146 public object Value {
get; set; }
148 public bool IsNew {
get; set; }
virtual CacheValue SetValueUnlocked(object key, object value)
LinkedListNode< KeyValuePair< object, CacheValue > > IndexRef
virtual CacheValue GetCacheValueUnlocked(object key)
override bool TryGetObject(object key, out object value)
override object RemoveObject(object key)
override void Clear()
Clear the cache of all the entries.
object RemoveUnlocked(object key)
Represents a cache of Objects. /summary>
virtual void UpdateElementAccess(object key, CacheValue cacheValue)
override void Dispose(bool disposing)
override bool SetObject(object key, object value)
When overridden in a derived class, it sets the value for the key given.