Monday, January 17, 2011

Cache Algorithm

For an integer value:

private int GetInteger(int uniqueID)
{
int integerv;

string key = String.Format("{0}_{1}", "SOMETHING", uniqueID);

object value = Cache.Get(key); // could be HttpContext.Current.Cache.Get

if (value != null) {
integerv = (int)value;
}
else {
integerv = _db.GetValueFromDb(uniqueID);
Cache.Add(key, integerv); // could be HttpContext.Current.Cache.Add
}

return integerv;
}

No comments: