Caching LocallyPublished: Jun 16, 2011 17:39:00
Physical Link: Caching Locally
One of the applications I have been working recently is a windows based application. Most things that the user keys into the system results in a database hit validation. To cut back on this I have implemented classes that assist in caching to the user’s local system. Based on the user’s habits, it will cache what the user uses commonly.
I am using a datatable in this example, but you could use a dictionary object. Should you choose to use a dictionary object, you could use LINQ to query the object. You could also make the caching class an extension which could cut down on some code an make it a little cleaner.
At the end of the day, this is one example only of what could be done.
public class CurrencyCodes
private static CurrencyCodes _instance; // Instance Of Self
private static DataTable dt = new DataTable("CurrencyCodes");
public static CurrencyCodes Instance
if (_instance == null)
_instance = new CurrencyCodes();
public String Search(String value)
DataRow rows = dt.Select("CurrencyCode='" + value + "'");
if (rows.Count() == 1) return value;
public void Add(String value)
DataRow row = dt.NewRow();
row["CurrencyCode"] = value;
row["LastUpdated"] = DateTime.Now;
public void ClearCache()
public void LoadCache(String xml)
StringReader theReader = new StringReader(xml);
public String SaveCache()
using (MemoryStream ms = new MemoryStream())
ms.Position = 0;
StreamReader sr = new StreamReader(ms, System.Text.Encoding.UTF8);
Calling the class
CachedCurrencyCodes cachedCurrencyCodes = CachedCurrencyCodes.Instance;
CurrencyCode = cachedCurrencyCodes.Search(Criteria);
Note Comments have been stripped to clean up the posting. Always Comment Your Code.
The post Caching Locally appeared first on Software Developer In London.
Author: Andrew Pallant
Categories: C#, Caching