Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Caching with Weak References in .NET : Page 2

Caching can help minimize network traffic when building service-oriented architecture applications; unfortunately, Microsoft's Caching Application Block falls short in a few areas.




Application Security Testing: An Integral Part of DevOps

Checking the Pulse
Finally, you need a way to determine whether the item has been collected or not. The IsAlive property returns true if you have a strong reference; otherwise, it returns the value of the m_weakReference.IsAlive property:

public override Boolean IsAlive { get { if (m_strongReference != null) return true; if (m_weakReference == null) return false; return m_weakReference.IsAlive; } }

You should check this property before attempting to access the cached object to avoid an InvalidOperationException.

Abstract the Non-Generic Properties
To make caching a list of objects of different types easier, you can move some items to a base class. The following code shows the base class as well as an added field that keeps track of the last time an instance of this object was accessed. The cache manager uses that field value to expire the object:

abstract class CacheReference { protected DateTime m_lastAccess; public CacheReference() { m_lastAccess = DateTime.Now; } public DateTime LastAccess { get { return m_lastAccess; } } public abstract Boolean IsAlive { get; } public abstract Boolean AllowCollection { get; set; } }

Exploring the CacheManager Class
This class is similar to the CacheManager class that is part of the Caching Application Block, but this version is a little less pluggable because it contains a hard-coded scavenging scheme:

class CacheManager { private Dictionary<String, CacheReference> m_cache = new Dictionary<string, CacheReference>();

The m_cache field is a dictionary of CacheReference objects that will contain all the cached items. You can add CacheReference<T> objects because CacheReference<T> inherits from the non-generic CacheReference class.

You will need a method to add an item to the cache. Use a generic method so that you can create a CacheReference<T> instance to hold the item.

public void Add&lt;T&gt;(String id, T tocache) where T : class { m_cache[id] = new CacheReference&lt;T&gt;(tocache); }

You'll also need a way to get items back out. The Get method shown below handles item retrieval. You pass in a string ID; the Get method takes care of checking to see if the provided ID exists in the cache. If so, the method verifies that the object is alive. If the item has already been collected (when temp.IsAlive == false), the method removes the item from the cache:

public T Get&lt;T&gt;(String id) where T : class { CacheReference temp = null; if (m_cache.TryGetValue(id, out temp)) { if (temp.IsAlive) { //The object is alive and doing well - return it. return ((CacheReference&lt;T&gt;)temp).Target; } else { //The item has been garbage collected. Remove it. m_cache.Remove(id); return null; } } else return null; }

Note that the Get method calls a method named Remove, which removes objects from the cache. Remove is a separate method because you'll also want to be able to explicitly remove an item from the cache:

public void Remove(String id) { CacheReference cacheReference; if (m_cache.TryGetValue(id, out cacheReference)) { if (cacheReference.IsAlive) cacheReference.AllowCollection = true; m_cache.Remove(id); } }

So far, you have only basic cache functionality: adding, retrieving, and removing items.

Taking Out the Garbage
The last piece of the puzzle is to provide an expiration mechanism. A straightforward mechanism to expire objects is to scavenge x number of objects after y objects have been added to the cache. To facilitate this approach, add some code to the CacheManager class that gives you the ability to set those values externally:

private int m_maxItemsBeforeScavenge; private int m_itemsToScavenge; public CacheManager(int maxItemsBeforeScavenge, int itemsToScavenge) { m_maxItemsBeforeScavenge = maxItemsBeforeScavenge; m_itemsToScavenge = itemsToScavenge; }

A simple scavenging scheme (see Listing 2) attempts to expire a number of objects (m_itemsToScavenge) whenever the cache grows to a predetermined number of objects (m_maxItemsBeforeScavenge). Note that the Scavenge method uses an explicit call to GC.Collect. While calling GC.Collect explicitly is normally frowned upon, calling it after dereferencing an unusually large number of objects is acceptable (and desirable in this case).

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date