public T Get(Func<T> factory, string cacheKey)The beauty with this method is that popular items will always be in cache where as less popular items will not be cached for unnecessary lengths of time.
{
if (cache.Contains(cacheKey))
{
Task.Factory.StartNew(() => FluffTheCache(factory, cacheKey));
return cache.Get(cacheKey);
}
return Put(factory, cacheKey);
}
public void FluffTheCache(Func<T> factory, string cacheKey)
{
var expiry = cache.getExpiry(cacheKey);
if (expiry.Subtract(new TimeSpan(0, 0, 10)).Second < new Random().Next(1, 60))
{
Put(factory, cacheKey);
}
}
public T Put(Func<T> factory, string cacheKey)
{
var item = factory();
cache.Set(item, cacheKey);
return item;
}
You also get some randomization mixed in for free meaning that items don't come out of sync at the same time.
No comments:
Post a Comment