“There are only two hard things in Computer Science: cache invalidation and naming things.” -- Phil Karlton
That being said, I created this SimpleCache class which I intend to use to cache database queries and results over a relatively short period of time.
I'm just asking if there are any immediate faults or errors with this?
(The class is internal
because I'm using it inline in another class)
internal sealed class SimpleCache
{
private static Dictionary<string, CacheItem> _cache = new Dictionary<string, CacheItem>();
public void Add(string key, object value)
{
_cache.Add(key, new CacheItem() { Value = value, Created = DateTime.Now.Ticks });
}
public object this[string key]
{
get
{
if(_cache.ContainsKey(key))
{
if ((_cache[key].Created + 30000) > DateTime.Now.Ticks) //30000 was chosen by magic
{
return _cache[key].Value;
}
_cache.Remove(key);
}
return null;
}
}
internal sealed class CacheItem
{
public object Value { get; set; }
public long Created { get; set; }
}
}