When developing C# windows application, we tend to reuse common datasource in serveral forms fo controls like combo bo, listbox. Caching is a good strategy reduce the round trip to database. I cache most of all my commonly used datasets improve loading time.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Cache
{
public class Cache
{
static Dictionary
public Cache() { }
public static bool AddToCache(string key, object obj)
{
try
{
_cache.Add(key, obj);
}
catch (ArgumentException)
{
return false;
}
return true;
}
public static object GetFromCache(string key)
{
object obj = null;
try
{
if (_cache.ContainsKey(key))
_cache.TryGetValue(key, out obj);
}
catch
{
}
return obj;
}
public static void ClearCache()
{
_cache.Clear();
}
}
}