Long time listener - first time caller. I am hoping to get some advice. I have been reading about caching in .net - both with System.Web.Caching and System.Runtime.Caching. I am wondering what additional benefits I can get vs simply creating a static variable with locking. My current (simple minded) caching method is like this:
public class Cache
{
private static List<Category> _allCategories;
private static readonly object _lockObject = new object();
public static List<Category> AllCategories
{
get
{
lock (_lockObject)
{
if (_allCategories == null)
{
_allCategories = //DB CALL TO POPULATE
}
}
return _allCategories;
}
}
}
Other than expiration (and I wouldn't want this to expire) I am at a loss to see what the benefit of using the built in caching are.
Maybe there are benefits for more complex caching scenarios that don't apply to me - or maybe I am just missing something (would not be the first time).
So, what is the advantage of using cache if I want a cache that never expires? Doesn't static variables do this?