Team blog of MCS @ Middle East and Africa

This blog is created by Microsoft MEA HQ near shoring team, and it aims to share knowledge with the IT community.With its infrastructure and development sides,It brings to you the proven best practices and real world experiences from Subject Matter Experts
Follow Us On Twitter! Subscribe To Our Blog! Contact Us

Proxy Caching with Lazy Loading

Proxy Caching with Lazy Loading

  • Comments 3
  • Likes

We can optimize proxy caching solution defined here one step ahead by leveraging lazy loading -there is always room for improvement, isn’t it?

Lazy loading is one of the design patterns, which delays initialization of an object to the time when it is actually used. It can be useful when resource-sensitive objects are to be created. It may be in 1 of 4 forms: Lazy initialization, virtual proxy, ghost, value holder.

With version 4.0, thread-safe lazy pattern is adapted into the framework. You can visit here for details on Lazy<T> implementation. It is important to point out that object locking (may be required in case of concurrent use) is implemented inclusively:

“By default, all public and protected members of the Lazy<T> class are thread safe and may be used concurrently from multiple threads. These thread-safety guarantees may be removed optionally and per instance, using parameters to the type's constructors.”

In this blog, I am going to use it for proxy/channel factory caching. To see the whole picture, please visit prior post on this series.

To make it simple and clear, I am going to show code for each case:

  • Eager loading (Non-lazy):
   1: internal static class ProxyManager<IType>
   2: {
   3:     internal static ConcurrentDictionary<string, ChannelFactory<IType>> proxies = new ConcurrentDictionary<string, ChannelFactory<IType>>();
   4:  
   5:     internal static IType GetProxy(string key)
   6:     {
   7:         return proxies.GetOrAdd(key, m => ProxyFactory<IType>.Create()).CreateChannel();
   8:     }
   9:  
  10:     internal static bool RemoveProxy(string key)
  11:     {
  12:         ChannelFactory<IType> proxy;
  13:         return proxies.TryRemove(key, out proxy);
  14:     }
  15: }
  • Lazy loading
   1: internal static class ProxyManager<IType>
   2: {
   3:     internal static ConcurrentDictionary<string, ChannelFactory<IType>> proxies = null;
   4:  
   5:     internal static IType GetProxy(string key)
   6:     {
   7:         if (proxies == null)
   8:             proxies = new ConcurrentDictionary<string, ChannelFactory<IType>>();
   9:  
  10:         return proxies.GetOrAdd(key, m => ProxyFactory<IType>.Create()).CreateChannel();
  11:     }
  12:  
  13:     internal static bool RemoveProxy(string key)
  14:     {
  15:         ChannelFactory<IType> proxy;
  16:         return proxies.TryRemove(key, out proxy);
  17:     }
  18: }
  • Lazy loading by using FW
   1: internal static class ProxyManager<IType>
   2: {
   3:     internal static ConcurrentDictionary<string, Lazy<ChannelFactory<IType>>> proxies = new ConcurrentDictionary<string, Lazy<ChannelFactory<IType>>>();
   4:  
   5:     internal static IType GetProxy(string key)
   6:     {
   7:         return proxies.GetOrAdd(key, m => new Lazy<ChannelFactory<IType>>(() => ProxyFactory<IType>.Create())).Value.CreateChannel();            
   8:     }
   9:  
  10:     internal static bool RemoveProxy(string key)
  11:     {
  12:         Lazy<ChannelFactory<IType>> proxy;
  13:         return proxies.TryRemove(key, out proxy);
  14:     }
  15: }
Comments
  • I have to say this post was certainly informative and contains useful content for enthusiastic visitors. I will definitely bookmark this blog for future reference and further viewing. Thanks a bunch for sharing this with us!<a href="http://www.rangatel.com">internet phone service provider</a>

  • Thanks Neil. Glad you like it!

  • thank you

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment