Caching for SubSonic

I’ve had some time recently to get more comfortable with SubSonic and I’m really enjoying using it.  When they add support for joins, it should rock…  and hopefully they find a way to have it co-exist and complement linq when that is released.  One thing that SubSonic seems to lack is support for caching objects.  I looked around the web, but couldn’t find a helper class/method that did what I wanted, so I whipped one up that you can use as a starting point if you’re looking for some way to cache your objects:

using System.Web;

using SubSonic;

 

namespace Util

{

    public class CacheUtil

    {

        public static ListType FetchAll<T, ListType>()

            where T : AbstractRecord<T>, new()

            where ListType : AbstractList<T, ListType>, new()

        {

            string key = typeof(T).ToString();

            object item = HttpContext.Current.Cache[key];
 

            if (item == null)

            {

                ListType collection = new ListType();

                collection.LoadAndCloseReader(AbstractRecord<T>.FetchAll());

 

                HttpContext.Current.Cache.Insert(key, collection);

                return collection;

            }

            return (ListType)item;

        }

 

        public static void ClearCache<T>()

        {

            string key = typeof(T).ToString();

            if (HttpContext.Current.Cache[key] != null)

            {

                HttpContext.Current.Cache.Remove(key);

            }

        }

    }

}