2010-09-27

Generic cache manager

The following class is a generic cache manager. It requires a reference to System.Web.

public static class CacheManager
{
    public static T GetObject<T>(string key)
    {
        if (System.Web.HttpRuntime.Cache.Get(key) != null) return (T)System.Web.HttpRuntime.Cache.Get(key);
        return default(T);
    }

    public static void AddObject<T>(T customObject, string key, TimeSpan duration)
    {
        if (System.Web.HttpRuntime.Cache.Get(key) == null)
            System.Web.HttpRuntime.Cache.Insert(key, customObject, null, System.Web.Caching.Cache.NoAbsoluteExpiration, duration);
    }
}

No comments:

Post a Comment