A Wonderfull Cache - Manage Cache Per Module
A Wonderfull Cache? Hmm, for some people, maybe it's nothing. But for me it's more than anything. I'll try to learn cache just for one condition, it's for boost web application performance. Btw, cache is not just only one solution to make web application performance more better.
I'm new in cache, so I hope, I get feedback from this article.
OK.... until now I have just one conclusion about Cache. Cache is just only storage that store in memory server that can accessed by all user who accessing the application. There is 2 condition when we want to use Cache.
- Cache must be updated constantly. When cache was created at the first time, it will stay a live in memory server until expired. So, cache must be removed from memory when "ADD", "UPDATE", and "DELETE" in database (except you want to let it untill expired).
- When we want to use Cache in filtering data, we must creating some unique keys of Cache. If there is ten combination of filter data is equal as ten keys of Cache.
I have try to make an application to show list of data with Cache. First I make some method to manage Cache.
private ArrayList CacheRemoveAll(ArrayList CacheKeysList)
{
foreach (string CacheKey in CacheKeysList)
{
Cache.Remove(CacheKey);
}
CacheKeysList = new ArrayList();
return CacheKeysList;
}
private ArrayList CacheRemove(ArrayList CacheKeysList, string CacheKey)
{
Cache.Remove(CacheKey);
if (CacheKeysList.Count > 0) CacheKeysList.Remove(CacheKey);
return CacheKeysList;
}
private ArrayList CacheAdd(ArrayList CacheKeysList, string CacheKey, object Value)
{
//You can set time expiration in web config
Cache.Add(CacheKey, Value, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 10, 0), CacheItemPriority.Default, null);
CacheKeysList.Add(CacheKey);
return CacheKeysList;
}
Create private variable to store keys of Cache, and it should be able accessed by all user, so I use Cache too to store list of the keys.
If we want to create another module/page which get the data from other table, we must create a new private variable. Change "ModuleCacheKeys" to another key according to module name or table.
private ArrayList ModuleCacheKeys
{
get { return (Cache["ModuleCacheKeys"] == null) ? new ArrayList() : (ArrayList)Cache["ModuleCacheKeys"]; }
set { Cache["ModuleCacheKeys"] = value; }
}
When binding data to datagrid/gridview, we can use that methods to add or remove datasource to/from Cache. Here is my code when binding the data.
private void BindData()
{
//txtKeyword.Text,ddl.SelectedValue is control in filter area
//Used each control value to format cache key
string CacheKey = string.Format("{0}{1}",txtKeyword.Text,ddl.SelectedValue);
DataSet DataSource = new DataSet();
DataSource = (DataSet)Cache[CacheKey];
if (DataSource == null)
{
ModuleCacheKeys = CacheRemove(ModuleCacheKeys, CacheKey);
ConnectionStringSettings Module = ConfigurationManager.ConnectionStrings["Module"];
DataSet ds = new DataSet();
ds = SqlHelper.ExecuteDataset(Module .ConnectionString, CommandType.Text, "SELECT * FROM TableName WHERE '" + txtKeyword.Text + "' IS NULL OR name LIKE '%" + txtKeyword.Text + "%' AND code = '" + ddl.SelectedValue + "'");
DataSource = ds;
ModuleCacheKeys = CacheAdd(ModuleCacheKeys, CacheKey, DataSource);
}
GridView1.DataSource = DataSource.Tables[0].DefaultView;
GridView1.DataBind();
}
We can use CacheRemoveAll method to remove all cache. We can use it in Add, Update, or Delete method.
Note: You must change some code so that can according to .NET 1.1