Wednesday, December 19, 2012

Creating the .NET Solution Part 2


Cache

Continuing on the principles of extensibility, we will build a cache object implementing ICache interface. Our cache implementation will wrap the HttpContext.Current.Cache object from System.Web.Caching. Tomorrow if need be, we can replace this implementation with say MemCached or AppFabric.



The logic will deal with saving and retrieving items from the cache using a key as reference to identify cached items. Cache will also support time-based caching, where the cached item will expire once the time limit is over. We will also support deleting entries from cache. This could be used to make room for newer entries once the cache starts to fill up or simply to remove an item that is no longer needed by the application.

  1. We will continue from last post Creating the .NET Solution Part 1 open the Fishbook solution
  2. Right click in Interfaces project and select Add/New Item.
    Create ICache.cs
    Create ICache.cs
    1. Select Interface Item from C# Template and name it ICache.cs and then write the following code:
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;

      namespace Fishbook.Interfaces
      {
          public interface ICache
          {
              object Get(string cache_key);
              List<string> GetCacheKeys();
              void Set(string cache_key, object cache_object);
              void Set(string cache_key, object cache_object, DateTime expiration);
              void Set(string cache_key, object cache_object, TimeSpan expiration);
              void Delete(string cache_key);
              bool Exists(string cache_key);
              void Flush();
             
          }

      } 

    2. Right click in Components project and select Add/New Item.
      Create Setting file
      Create Setting file
      1. Select Settings File and name it Settings.Settings and set it like the following image:
        Put Setting file in Properties
        Put Setting file in Properties
        Notes:
        Put setting file under the Properties of component project.
        1. Right click in Components project and select Add/New Item.
          Create Cache class
          Create Cache class
          1. Select Class Item from C# Template and name it Configuration.cs and write the following code:
            using System;
            using System.Collections;
            using System.Collections.Generic;
            using System.ComponentModel.Composition;
            using System.Web;
            using System.Web.Caching;
            using Fishbook.Components.Properties;
            using Fishbook.Interfaces;

            namespace Fishbook.Components
            {
                [Export(typeof(ICache))]
                public class Cache :ICache
                {
                    private static System.Web.Caching.Cache cache;
                    private static TimeSpan timeSpan = new TimeSpan(
                        Settings.Default.DefaultCacheDuration_Days,
                        Settings.Default.DefaultCacheDuration_Hours,
                        Settings.Default.DefaultCacheDuration_Minutes, 0);

                    static Cache()
                    {
                        cache = HttpContext.Current.Cache;
                    }

                    public object Get(string cache_key)
                    {
                        return cache.Get(cache_key);
                    }

                    public List<string> GetCacheKeys()
                    {
                        List<string> keys = new List<string>();
                        IDictionaryEnumerator ca = cache.GetEnumerator();
                        while (ca.MoveNext())
                        {
                            keys.Add(ca.Key.ToString());
                        }
                        return keys;
                    }

                    public void Set(string cache_key, object cache_object)
                    {
                        Set(cache_key, cache_object, timeSpan);
                    }

                    public void Set(string cache_key, object cache_object, DateTime expiration)
                    {
                        Set(cache_key, cache_object, expiration, CacheItemPriority.Normal);
                    }

                    public void Set(string cache_key, object cache_object, TimeSpan expiration)
                    {
                        Set(cache_key, cache_object, expiration, CacheItemPriority.Normal);
                    }

                    public void Set(string cache_key, object cache_object, DateTime expiration, CacheItemPriority priority)
                    {
                        cache.Insert(cache_key, cache_object, null, expiration, System.Web.Caching.Cache.NoSlidingExpiration, priority, null);
                    }

                    public void Set(string cache_key, object cache_object, TimeSpan expiration, CacheItemPriority priority)
                    {
                        cache.Insert(cache_key, cache_object, null, System.Web.Caching.Cache.NoAbsoluteExpiration, expiration, priority, null);
                    }

                    public void Delete(string cache_key)
                    {
                        if (Exists(cache_key))
                            cache.Remove(cache_key);
                    }

                    public bool Exists(string cache_key)
                    {
                        if (cache[cache_key] != null)
                            return true;
                        else
                            return false;
                    }

                    public void Flush()
                    {
                        foreach (string s in GetCacheKeys())
                        {
                            Delete(s);
                        }
                    }
                }
            }




          No comments:

          Post a Comment

          Automatic Traffic Exchange

          YallaTech Facebook page