/// <summary> /// Thread that ages the cache and purges expired entries. /// </summary> private void CacheThread() { // Don't ever exit. while (true) { try { lock (typeof(NodeCache)) { // TODO: Remove // log.Debug( "Cache efficency = {0}%, Current count = {1}, Access Count = {2}, Hit Count = {3}", ( ( accessCount > 0 ) ? ( hitCount / accessCount ) * 100 : 0 ), cacheTable.Count, accessCount, hitCount ); ArrayList keyList = new ArrayList(cacheTable.Keys); foreach (string key in keyList) { NodeCacheEntry entry = cacheTable[key] as NodeCacheEntry; if (!entry.IsValid) { cacheTable.Remove(key); } } } Thread.Sleep(cachePurgeTimeout); } catch (Exception ex) { log.Error(ex, "NodeCache Thread exception"); } } }
/// <summary> /// Creates a new entry and adds it to the head of the list. /// </summary> public ICacheEntry <TKey, TValue> NewEntry(TKey key, TValue value) { var node = new LinkedListNode <TKey>(key); var entry = new NodeCacheEntry <TKey, TValue>() { Key = key, Value = value, Node = node }; list.AddFirst(node); return(entry); }
/// <summary> /// Adds a node object to the cache. /// </summary> /// <param name="collection">The collection that the node object belongs to.</param> /// <param name="node">The node object to add to the cache.</param> public void Add(Collection collection, Node node) { // Make sure that the node can be cached. if (IsCacheable(collection, node)) { // Generate a key to use with the cache table. string key = CreateKey(collection.ID, node.ID); lock (typeof(NodeCache)) { // Check to see if the entry is already in the cache. NodeCacheEntry entry = cacheTable[key] as NodeCacheEntry; if (entry == null) { cacheTable[key] = new NodeCacheEntry(node.Properties.ToString(), cacheTimeToLive); } else { entry.Value = node.Properties.ToString(); } } } }
/// <summary> /// Gets a node object from the cache table. /// </summary> /// <param name="collectionID">The ID of the collection that the node belongs to.</param> /// <param name="nodeID">The ID of the node object.</param> /// <returns>A node object if the ID exists in the cache. Otherwise a null is returned.</returns> public Node Get(string collectionID, string nodeID) { string cacheValue = null; string key = CreateKey(collectionID, nodeID); lock (typeof(NodeCache)) { // TODO: Remove accessCount++; NodeCacheEntry entry = cacheTable[key] as NodeCacheEntry; if (entry != null) { // Keep this entry in the cache. entry.UpdateExpirationTime(); cacheValue = entry.Value as string; // TODO: Remove hitCount++; } } return((cacheValue != null) ? NodeFromString(cacheValue) : null); }