DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
Cache.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 
20 
21 namespace Deveel.Data.Caching {
25  public abstract class Cache : ICache {
29  private int currentCacheSize;
30 
35  private readonly int wipeTo;
36 
37  protected Cache() {
38  }
39 
40 
41  ~Cache() {
42  Dispose(false);
43  }
44 
52  protected virtual void CheckClean() {
53  }
54 
64  protected virtual bool WipeMoreNodes() {
65  return (currentCacheSize >= wipeTo);
66  }
67 
73  protected virtual void OnWipingNode(object value) {
74  }
75 
81  protected virtual void OnObjectAdded(object key, object value) {
82  ++currentCacheSize;
83  }
84 
90  protected virtual void OnObjectRemoved(object key, Object value) {
91  --currentCacheSize;
92  }
93 
97  protected virtual void OnAllCleared() {
98  currentCacheSize = 0;
99  }
100 
115  protected virtual void OnGetWalks(long totalWalks, long totalGetOps) {
116  }
117 
118 
119  // ---------- Public cache methods ----------
120 
125  public virtual int NodeCount {
126  get { return currentCacheSize; }
127  }
128 
139  protected abstract bool SetObject(object key, object value);
140 
141  protected abstract bool TryGetObject(object key, out object value);
142 
143  protected abstract object RemoveObject(object key);
144 
150  public bool Set(object key, object value) {
151 
152  // Do we need to clean any cache elements out?
153  CheckClean();
154 
155  bool newValue = SetObject(key, value);
156  if (newValue)
157  OnObjectAdded(key, value);
158 
159  return newValue;
160  }
161 
162  public bool TryGet(object key, out object value) {
163  return TryGetObject(key, out value);
164  }
165 
178  public object Remove(Object key) {
179  object obj = RemoveObject(key);
180 
181  if (obj != null)
182  OnObjectRemoved(key, obj);
183 
184  return obj;
185  }
186 
190  public virtual void Clear() {
191  OnAllCleared();
192  }
193 
204  protected virtual int Clean() {
205  return 0;
206  }
207 
214  public static int ClosestPrime(int value) {
215  for (int i = 0; i < PrimeList.Length; ++i) {
216  if (PrimeList[i] >= value) {
217  return PrimeList[i];
218  }
219  }
220  // Return the last prime
221  return PrimeList[PrimeList.Length - 1];
222  }
223 
227  private static readonly int[] PrimeList = new int[] {
228  3001, 4799, 13999, 15377, 21803, 24247, 35083, 40531, 43669,
229  44263, 47387, 50377, 57059, 57773, 59399, 59999, 75913, 96821,
230  140551, 149011, 175633, 176389, 183299, 205507, 209771, 223099,
231  240259, 258551, 263909, 270761, 274679, 286129, 290531, 296269,
232  298021, 300961, 306407, 327493, 338851, 351037, 365489, 366811,
233  376769, 385069, 410623, 430709, 433729, 434509, 441913, 458531,
234  464351, 470531, 475207, 479629, 501703, 510709, 516017, 522211,
235  528527, 536311, 539723, 557567, 593587, 596209, 597451, 608897,
236  611069, 642547, 670511, 677827, 679051, 688477, 696743, 717683,
237  745931, 757109, 760813, 763957, 766261, 781559, 785597, 788353,
238  804493, 813559, 836917, 854257, 859973, 883217, 884789, 891493,
239  902281, 910199, 915199, 930847, 939749, 940483, 958609, 963847,
240  974887, 983849, 984299, 996211, 999217, 1007519, 1013329,
241  1014287, 1032959, 1035829, 1043593, 1046459, 1076171, 1078109,
242  1081027, 1090303, 1095613, 1098847, 1114037, 1124429, 1125017,
243  1130191, 1159393, 1170311, 1180631, 1198609, 1200809, 1212943,
244  1213087, 1226581, 1232851, 1287109, 1289867, 1297123, 1304987,
245  1318661, 1331107, 1343161, 1345471, 1377793, 1385117, 1394681,
246  1410803, 1411987, 1445261, 1460497, 1463981, 1464391, 1481173,
247  1488943, 1491547, 1492807, 1528993, 1539961, 1545001, 1548247,
248  1549843, 1551001, 1553023, 1571417, 1579099, 1600259, 1606153,
249  1606541, 1639751, 1649587, 1657661, 1662653, 1667051, 1675273,
250  1678837, 1715537, 1718489, 1726343, 1746281, 1749107, 1775489,
251  1781881, 1800157, 1806859, 1809149, 1826753, 1834607, 1846561,
252  1849241, 1851991, 1855033, 1879931, 1891133, 1893737, 1899137,
253  1909513, 1916599, 1917749, 1918549, 1919347, 1925557, 1946489,
254  1961551, 1965389, 2011073, 2033077, 2039761, 2054047, 2060171,
255  2082503, 2084107, 2095099, 2096011, 2112193, 2125601, 2144977,
256  2150831, 2157401, 2170141, 2221829, 2233019, 2269027, 2270771,
257  2292449, 2299397, 2303867, 2309891, 2312407, 2344301, 2348573,
258  2377007, 2385113, 2386661, 2390051, 2395763, 2422999, 2448367,
259  2500529, 2508203, 2509841, 2513677, 2516197, 2518151, 2518177,
260  2542091, 2547469, 2549951, 2556991, 2563601, 2575543, 2597629,
261  2599577, 2612249, 2620003, 2626363, 2626781, 2636773, 2661557,
262  2674297, 2691571, 2718269, 2725691, 2729381, 2772199, 2774953,
263  2791363, 2792939, 2804293, 2843021, 2844911, 2851313, 2863519,
264  2880797, 2891821, 2897731, 2904887, 2910251, 2928943, 2958341,
265  2975389
266  };
267 
268 
269 
270  public void Dispose() {
271  Dispose(true);
272  GC.SuppressFinalize(this);
273  }
274 
275  protected virtual void Dispose(bool disposing) {
276  if (disposing) {
277  Clear();
278  }
279  }
280  }
281 }
virtual bool WipeMoreNodes()
Checks if the clean-up method should clean up more elements from the cache.
Definition: Cache.cs:64
virtual int Clean()
Cleans away some old elements in the cache.
Definition: Cache.cs:204
int currentCacheSize
The current cache size.
Definition: Cache.cs:29
virtual void OnObjectRemoved(object key, Object value)
Notifies that the given object and key has been removed from the cache.
Definition: Cache.cs:90
virtual void OnObjectAdded(object key, object value)
Notifies that the given object and key has been added to the cache.
Definition: Cache.cs:81
virtual void OnAllCleared()
Notifies that the cache has been entirely cleared of all elements.
Definition: Cache.cs:97
bool TryGet(object key, out object value)
Tries to get an object for the given key from the underlying cache system.
Definition: Cache.cs:162
virtual void CheckClean()
This is called whenever at object is put into the cache.
Definition: Cache.cs:52
virtual void Clear()
Clear the cache of all the entries.
Definition: Cache.cs:190
virtual void OnWipingNode(object value)
Notifies that the given object has been wiped from the cache by the clean up procedure.
Definition: Cache.cs:73
bool Set(object key, object value)
Puts an object into the cache with the given key.
Definition: Cache.cs:150
readonly int wipeTo
The number of nodes that should be left available when the cache becomes too full and a clean up oper...
Definition: Cache.cs:35
Represents a cache of Objects. /summary>
Definition: Cache.cs:25
virtual void OnGetWalks(long totalWalks, long totalGetOps)
Notifies that some statistical information about the hash map has updated.
Definition: Cache.cs:115
Provides a contract to access a caching system.
Definition: ICache.cs:25
object Remove(Object key)
Removes a node for the given key from the cache.
Definition: Cache.cs:178
static int ClosestPrime(int value)
Definition: Cache.cs:214
virtual void Dispose(bool disposing)
Definition: Cache.cs:275