Bia Securities

Caching for SubSonic

by Jim Geurts on Aug.09, 2007

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);

            }

        }

    }

}


Comments are closed.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...

Archives

All entries, chronologically...