DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
MemoryCache.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 using System;
18 using System.Collections.Generic;
19 
20 namespace Deveel.Data.Caching {
21  public class MemoryCache : Cache {
22  protected LinkedList<KeyValuePair<object, CacheValue>> IndexList { get; private set; }
23 
24  private readonly Dictionary<object, CacheValue> valueCache = new Dictionary<object, CacheValue>();
25 
26  private readonly object syncRoot = new object();
27  private DateTime lastCacheAccess = DateTime.MaxValue;
28 
29  public MemoryCache() {
30  IndexList = new LinkedList<KeyValuePair<object, CacheValue>>();
31  }
32 
33  // Some statistics about the hashing algorithm.
34  private long totalGets = 0;
35  private long getTotal = 0;
36 
37  protected virtual void UpdateElementAccess(object key, CacheValue cacheValue) {
38  // update last access and move it to the head of the list
39  cacheValue.LastAccess = DateTime.Now;
40  var idxRef = cacheValue.IndexRef;
41  if (idxRef != null) {
42  IndexList.Remove(idxRef);
43  } else {
44  idxRef = new LinkedListNode<KeyValuePair<object, CacheValue>>(new KeyValuePair<object, CacheValue>(key, cacheValue));
45  cacheValue.IndexRef = idxRef;
46  }
47 
48  IndexList.AddFirst(idxRef);
49  }
50 
51  protected virtual CacheValue GetCacheValueUnlocked(object key) {
52  CacheValue v;
53  return valueCache.TryGetValue(key, out v) ? v : null;
54  }
55 
56  protected virtual CacheValue SetValueUnlocked(object key, object value) {
57  lastCacheAccess = DateTime.Now;
58  var cacheValue = GetCacheValueUnlocked(key);
59 
60  if (cacheValue == null) {
61  cacheValue = new CacheValue(value);
62  cacheValue.IsNew = true;
63  valueCache[key] = cacheValue;
64  } else {
65  cacheValue.Value = value;
66  cacheValue.IsNew = false;
67  }
68 
69  UpdateElementAccess(key, cacheValue);
70  return cacheValue;
71  }
72 
73  protected object RemoveUnlocked(object key) {
74  var value = GetCacheValueUnlocked(key);
75  if (value != null) {
76  valueCache.Remove(key);
77  IndexList.Remove(value.IndexRef);
78  return value.Value;
79  }
80 
81  return null;
82  }
83 
84  protected override bool SetObject(object key, object value) {
85  lock (syncRoot) {
86  var cached = SetValueUnlocked(key, value);
87  return cached.IsNew;
88  }
89  }
90 
91  protected override bool TryGetObject(object key, out object value) {
92  CacheValue v;
93  value = null;
94 
95  lock (syncRoot) {
96  lastCacheAccess = DateTime.Now;
97  v = GetCacheValueUnlocked(key);
98  if (v != null) {
99  value = v.Value;
100  UpdateElementAccess(key, v);
101  return true;
102  }
103  }
104 
105  return false;
106  }
107 
108  protected override object RemoveObject(object key) {
109  lock (syncRoot) {
110  lastCacheAccess = DateTime.Now;
111 
112  return RemoveUnlocked(key);
113  }
114  }
115 
116  public override void Clear() {
117  lock (syncRoot) {
118  valueCache.Clear();
119  IndexList.Clear();
120  }
121 
122  base.Clear();
123  }
124 
125  protected override void Dispose(bool disposing) {
126  if (disposing) {
127  Clear();
128  }
129 
130 
131  base.Dispose(disposing);
132  }
133 
134  #region CachedValue
135 
136  protected sealed class CacheValue {
137  public CacheValue(object value) {
138  LastAccess = DateTime.Now;
139  Value = value;
140  }
141 
142  public LinkedListNode<KeyValuePair<object, CacheValue>> IndexRef { get; set; }
143 
144  public DateTime LastAccess { get; set; }
145 
146  public object Value { get; set; }
147 
148  public bool IsNew { get; set; }
149  }
150 
151  #endregion
152  }
153 }
virtual CacheValue SetValueUnlocked(object key, object value)
Definition: MemoryCache.cs:56
LinkedListNode< KeyValuePair< object, CacheValue > > IndexRef
Definition: MemoryCache.cs:142
virtual CacheValue GetCacheValueUnlocked(object key)
Definition: MemoryCache.cs:51
override bool TryGetObject(object key, out object value)
Definition: MemoryCache.cs:91
override object RemoveObject(object key)
Definition: MemoryCache.cs:108
override void Clear()
Clear the cache of all the entries.
Definition: MemoryCache.cs:116
object RemoveUnlocked(object key)
Definition: MemoryCache.cs:73
Represents a cache of Objects. /summary>
Definition: Cache.cs:25
virtual void UpdateElementAccess(object key, CacheValue cacheValue)
Definition: MemoryCache.cs:37
override void Dispose(bool disposing)
Definition: MemoryCache.cs:125
override bool SetObject(object key, object value)
When overridden in a derived class, it sets the value for the key given.
Definition: MemoryCache.cs:84