Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Design and Architecture
  4. Cache Manager design problem

Cache Manager design problem

Scheduled Pinned Locked Moved Design and Architecture
helpdesignbusiness
3 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Steve Holdorf
    wrote on last edited by
    #1

    I have some basic code for a cache manager provided below. My design is far from complete and I need help. What I need is a single cache manager CacheManager.cs GetCacheItem method that accepts as parameters a cache key, business object delegate method and a variable set of parameters. This manager should be able to get and retrieve items in cache, handle dependencies, and work with every business manager object in the project (i.e. bc_CustomerManager.GetCustomer, bc_ProductManager.GetProduct, etc.) Anyone who can assist me or work with me on this would be wonderful. Cache Manager GetCacheItem:

        public object GetCacheItem(String sCacheKey, QueryExpression QueryExpression, params object\[\] parameters)
        {
            object objCacheItem = null;
    
            if (!CachingEnabled)
            {
                return objCacheItem;
            }
    
            if (CacheItem<Object>(sCacheKey) != null)
            {
                return CacheItem<Object>(sCacheKey);
            }
            else
            {
                lock (syncObject)
                {
                    if (CacheItem<Object>(sCacheKey) != null)
                    {
                        return CacheItem<Object>(sCacheKey);
                    }
                    else
                    {
                        Object cacheItem = QueryExpression(parameters);
    
                        AddToCache<Object>(sCacheKey, cacheItem);
    
                        return cacheItem;
                    }
                }
            }
        }
    

    In my project I have a business layer and a data layer. I am not sure where and how the code below should be called. Incomplete code call to CacheManager.GetCacheItem:

        // Main delegate on which all Get requests are based on.
        delegate object QueryExpression(params object\[\] parameters);
    
        // Create instance of cacheManager
        CacheManager cacheManager = new CacheManager();
    
        // Create instance of BC\_Assects
        BC\_Assects bc\_Assects = new BC\_Assects();
        
        // Make the generic get cache item call GetAssetsByCollectionID must be declared as a QueryExpression delegate
        DataTable dt = (DataTable) cacheManager.GetCacheItem(sCacheKey, bc\_Assects.GetAssetsByCollectionID, parameters);
    

    Thank you very much!

    Steve

    S 1 Reply Last reply
    0
    • S Steve Holdorf

      I have some basic code for a cache manager provided below. My design is far from complete and I need help. What I need is a single cache manager CacheManager.cs GetCacheItem method that accepts as parameters a cache key, business object delegate method and a variable set of parameters. This manager should be able to get and retrieve items in cache, handle dependencies, and work with every business manager object in the project (i.e. bc_CustomerManager.GetCustomer, bc_ProductManager.GetProduct, etc.) Anyone who can assist me or work with me on this would be wonderful. Cache Manager GetCacheItem:

          public object GetCacheItem(String sCacheKey, QueryExpression QueryExpression, params object\[\] parameters)
          {
              object objCacheItem = null;
      
              if (!CachingEnabled)
              {
                  return objCacheItem;
              }
      
              if (CacheItem<Object>(sCacheKey) != null)
              {
                  return CacheItem<Object>(sCacheKey);
              }
              else
              {
                  lock (syncObject)
                  {
                      if (CacheItem<Object>(sCacheKey) != null)
                      {
                          return CacheItem<Object>(sCacheKey);
                      }
                      else
                      {
                          Object cacheItem = QueryExpression(parameters);
      
                          AddToCache<Object>(sCacheKey, cacheItem);
      
                          return cacheItem;
                      }
                  }
              }
          }
      

      In my project I have a business layer and a data layer. I am not sure where and how the code below should be called. Incomplete code call to CacheManager.GetCacheItem:

          // Main delegate on which all Get requests are based on.
          delegate object QueryExpression(params object\[\] parameters);
      
          // Create instance of cacheManager
          CacheManager cacheManager = new CacheManager();
      
          // Create instance of BC\_Assects
          BC\_Assects bc\_Assects = new BC\_Assects();
          
          // Make the generic get cache item call GetAssetsByCollectionID must be declared as a QueryExpression delegate
          DataTable dt = (DataTable) cacheManager.GetCacheItem(sCacheKey, bc\_Assects.GetAssetsByCollectionID, parameters);
      

      Thank you very much!

      Steve

      S Offline
      S Offline
      Steve Holdorf
      wrote on last edited by
      #2

      I got something working. Here is a sample cache manager and aspx page. Now, all I need is to handle dependicies. Can anyone help?

      aspx code:

      protected void Page\_Load(object sender, EventArgs e)
      {
          // Create instance of cacheManager
          CacheManager cacheManager = new CacheManager();
      
          // Create instance of BC\_Customer
          BC\_Customer bc\_Customer = new BC\_Customer();
      
          string\[\] param1 = { "customers", "stephen", "lisa" };
      
          // Must be declared as InsertExpression degelate
          cacheManager.InsertInCache(bc\_Customer.InsertCustomerName, param1);
      
          // Make the generic get cache item call must be declared as a QueryExpression delegate
          List<string> cNameList = (List<string>)cacheManager.GetCacheItem(bc\_Customer.GetCustomerName, param1);
      
          DropDownList1.DataSource = cNameList;
          DropDownList1.DataBind();
      
          string\[\] param2 = { "products", "Book", "Card" };
      
          // Create instance of BC\_Products
          BC\_Products bc\_Products = new BC\_Products();
      
          // Must be declared as InsertExpression degelate
          cacheManager.InsertInCache(bc\_Products.InsertProductName, param2);
      
      
          // Make the generic get cache item call must be declared as a QueryExpression delegate
          List<string> pNameList = (List<string>)cacheManager.GetCacheItem(bc\_Products.GetProductName, param2);
      
          DropDownList2.DataSource = pNameList;
          DropDownList2.DataBind();
      
      }
      

      Cache manager:

      // Main delegate on which all Get requests are based on.
      public delegate object QueryExpression(params object[] parameters);

      // Main delegate on which all insert requests are based on.
      public delegate object InsertExpression(params object[] parameters);

      /// <summary>
      /// Summary description for CacheManager
      /// </summary>
      public class CacheManager
      {

      public TimeSpan CacheDuration { get; set; }
      private static object syncObject = new object();
      private bool CachingEnabled = true;
      
      private bool HasKey(string Key)
      {            
          return (HttpContext.Current.Cache\[Key\] != null);
      }
      
      private Object CacheItem(string sCacheKey)
      {
          return (Object)HttpRuntime.Cache\[sCacheKey\];
      }
      
      public object GetCacheItem(QueryExpression QueryExpression, params object\[\] param)
      {
          object objCacheItem = null;
      
          if (!CachingEnabled)
          {
              return objCacheItem;
          }
      
          if
      
      S 1 Reply Last reply
      0
      • S Steve Holdorf

        I got something working. Here is a sample cache manager and aspx page. Now, all I need is to handle dependicies. Can anyone help?

        aspx code:

        protected void Page\_Load(object sender, EventArgs e)
        {
            // Create instance of cacheManager
            CacheManager cacheManager = new CacheManager();
        
            // Create instance of BC\_Customer
            BC\_Customer bc\_Customer = new BC\_Customer();
        
            string\[\] param1 = { "customers", "stephen", "lisa" };
        
            // Must be declared as InsertExpression degelate
            cacheManager.InsertInCache(bc\_Customer.InsertCustomerName, param1);
        
            // Make the generic get cache item call must be declared as a QueryExpression delegate
            List<string> cNameList = (List<string>)cacheManager.GetCacheItem(bc\_Customer.GetCustomerName, param1);
        
            DropDownList1.DataSource = cNameList;
            DropDownList1.DataBind();
        
            string\[\] param2 = { "products", "Book", "Card" };
        
            // Create instance of BC\_Products
            BC\_Products bc\_Products = new BC\_Products();
        
            // Must be declared as InsertExpression degelate
            cacheManager.InsertInCache(bc\_Products.InsertProductName, param2);
        
        
            // Make the generic get cache item call must be declared as a QueryExpression delegate
            List<string> pNameList = (List<string>)cacheManager.GetCacheItem(bc\_Products.GetProductName, param2);
        
            DropDownList2.DataSource = pNameList;
            DropDownList2.DataBind();
        
        }
        

        Cache manager:

        // Main delegate on which all Get requests are based on.
        public delegate object QueryExpression(params object[] parameters);

        // Main delegate on which all insert requests are based on.
        public delegate object InsertExpression(params object[] parameters);

        /// <summary>
        /// Summary description for CacheManager
        /// </summary>
        public class CacheManager
        {

        public TimeSpan CacheDuration { get; set; }
        private static object syncObject = new object();
        private bool CachingEnabled = true;
        
        private bool HasKey(string Key)
        {            
            return (HttpContext.Current.Cache\[Key\] != null);
        }
        
        private Object CacheItem(string sCacheKey)
        {
            return (Object)HttpRuntime.Cache\[sCacheKey\];
        }
        
        public object GetCacheItem(QueryExpression QueryExpression, params object\[\] param)
        {
            object objCacheItem = null;
        
            if (!CachingEnabled)
            {
                return objCacheItem;
            }
        
            if
        
        S Offline
        S Offline
        Steve Holdorf
        wrote on last edited by
        #3

        Now, the cache manager handles dependencies. See below:

        // Main delegate on which all Get requests are based on.
        public delegate object QueryExpression(params object[] parameters);

        // Main delegate on which all insert requests are based on.
        public delegate object InsertExpression(params object[] parameters);

        /// <summary>
        /// Summary description for CacheManager
        /// </summary>
        public class CacheManager
        {

        public TimeSpan CacheDuration { get; set; }
        private static object syncObject = new object();
        private bool CachingEnabled = true;
        
        private bool HasKey(string Key)
        {            
            return ((Object)HttpRuntime.Cache\[Key\] != null);
        }
        
        private Object CacheItem(string sCacheKey)
        {
            return (Object)HttpRuntime.Cache\[sCacheKey\];
        }
        
        public object GetCacheItem(QueryExpression QueryExpression, params object\[\] param)
        {
            object objCacheItem = null;
        
            if (!CachingEnabled)
            {
                return objCacheItem;
            }
        
            if (CacheItem((string) param\[0\]) != null)
            {
                return CacheItem((string) param\[0\]);
            }
            else
            {
                lock (syncObject)
                {
                    if (CacheItem((string)param\[0\]) != null)
                    {
                        return CacheItem((string) param\[0\]);
                    }
                    else
                    {
                        Object cacheItem = QueryExpression(param);
        
                        DateTime expiration = DateTime.Now.Add(new TimeSpan (6000));
        
                        HttpContext.Current.Cache.Add((string) param\[0\], (List<string>) cacheItem, null, expiration,
                            TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Default, null);
        
                        return cacheItem;
                    }
                }
            }
        }
        
        public Object InsertInCache(InsertExpression insertExpression, params object\[\] param)
        {
            System.Web.Caching.CacheDependency cacheDependency;
        
            string\[\] dependencyKey = { (string) param\[1\] };
        
            if (HasKey((string) param\[0\]))
            {
                return CacheItem((string)(param\[0\]));
            }
        
            DateTime expiration = DateTime.Now.Add(new TimeSpan (6000));
        
            cacheDependency = new System.Web.Caching.CacheDependency(null, dependencyKey);
        
            HttpContext.Current.Cache.Add((string) param\[0\], insertExpression(param), 
                (param\[1\] == "" ? null : (cacheDependency)), expiration,
        
        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups