ASP.NET came up with a very powerful feature of in-memory object cache in it earlier days. In asp.net 4.0 they introduce the notion of MemoryCache, but yet has to be proven.
The good thing with the first one is that if you have a website you can make your site run faster by caching some data. On the other site this was only advisable for web applicatioms. . It was designed and tested for use in ASP.NET to provide caching for Web applications. In other types of applications, such as console applications or Windows Forms applications, ASP.NET caching might not work correctly.” http://msdn.microsoft.com/en-us/library/system.web.caching.cache.aspx
While creating a webservice or simply a WCF , in some reason, you may be tented to use caching. You can the create your customized cache engine that will take care of your memory management.
In my example below, which is straight forward, you will be able to cache object by reference type and by value type. Let say you have a list of the 10 most sale products, that you want to show to a customer that just come in your big retail store, you can just save that list of type PRODUCT with a key top10product on the fly. You may be able to save a specific parameters or any value type like the normale cache. I did not had a threat safe scenario by locking and unlocking but it is also easy since a lot of people have already done this for you..
Insert code here... public static class RWCache
{
//create a static object for storing the cache
private static Hashtable _items = new Hashtable();
/// <summary>
/// Insert value into the cache using
/// appropriate name/value pairs
/// </summary>
/// <typeparam name="T">Type of cached item</typeparam>
/// <param name="o">Item to be cached</param>
/// <param name="key">Name of item</param>
public static void Add<T>(T o, string key)
{
_items[key] = o;
}
/// <summary>
/// Remove item from cache
/// </summary>
/// <param name="key">Name of cached item</param>
public static void Clear(string key)
{
_items.Remove(key);
}
/// <summary>
/// Check for item in cache
/// </summary>
/// <param name="key">Name of cached item</param>
/// <returns></returns>
public static bool Exists(string key)
{
return _items.ContainsKey(key);
}
/// <summary>
/// Retrieve cached item
/// </summary>
/// <typeparam name="T">Type of cached item</typeparam>
/// <param name="key">Name of cached item</param>
/// <param name="value">Cached value. Default(T) if item doesn't exist.</param>
/// <returns>Cached item as type</returns>
public static bool Get<T>(string key, out T value)
{
try
{
if (!Exists(key))
{
value = default(T);
return false;
}
value = (T)_items[key];
}
catch
{
value = default(T);
return false;
}
return true;
}
public static void ClearAllCache()
{
try
{
foreach (DictionaryEntry dEntry in _items)
{
_items.Remove(dEntry.Key.ToString());
}
}
catch (Exception ex)
{
}
}
}
You can see that this class will do everything you want for you.
Let add a Data file to test this :
public class DataAccess
{
internal static List<Parameter> GetParameterList()
{
return
new List<Parameter>
{
new Parameter {Name = "storecode", Value = "sc001"},
new Parameter {Name = "terminal", Value = "zt2"}
};
}
}
public class Parameter
{
public string Value { get; set; }
public string Name { get; set; }
}
What you can nnow do is just add a website to your project to test this..
add a button BtnGetCache, and a label lblMessage, a GridView GridView1 in your aspx page then this in the click event
protected void BtnGetCache_Click(object sender, EventArgs e)
{
string key = "oneParameter";
List<Parameter> parameters;
if (!CacheHelper.RWCache.Get(key, out parameters))
{
parameters = DataAccess.GetParameterList();
CacheHelper.RWCache.Add(parameters, key);
lblMessage.Text =
string.Format("Parameters [Reference Type Count = {0}] We did not found but retrieved and now added item to cache.",
parameters.Count);
}
else
{
lblMessage.Text = string.Format("Parameters [Reference Type Count = {0}] pulled item from cache.",
parameters.Count);
}
GridView1.DataSource = parameters;
GridView1.DataBind();
}
Hope you enjoy this.