Cache Manager design problem
-
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
-
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
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
-
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
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,