Caching Best Practices - Business Layer

Posts   
 
    
Sam avatar
Sam
User
Posts: 95
Joined: 30-Jun-2004
# Posted on: 29-Jun-2005 16:42:34   

I have a busness tier with some static methods to fetch common collections. My question is that with web apps I have been doing something that looks like this:


    public class StateManager
    {
        public static StateCollection GetAllStates()
        {
            StateCollection states = new StateCollection();
            if (System.Web.HttpContext.Current.Cache["GetAllStates"] != null)
                return System.Web.HttpContext.Current.Cache["GetAllStates"] as StateCollection;
            ISortExpression sorter = new SortExpression(SortClauseFactory.Create(StateFieldIndex.State, SortOperator.Ascending));
            states.GetMulti(null, 0, sorter);
            System.Web.HttpContext.Current.Cache.Insert("GetAllStates", states, null, DateTime.Now.AddHours(12), Cache.NoSlidingExpiration);
            return states;
        }
    }

Is the above code accptable or should I be doing this some other way?

JimFoye avatar
JimFoye
User
Posts: 656
Joined: 22-Jun-2004
# Posted on: 29-Jun-2005 16:58:37   

Just my two cents and I'm sure more experienced guys will chime in.

It seems to me your BLL is dependent on web sessions, and more specifically the ASP.NET implementation of a web session. The usefulness of your BLL class is somewhat negated by the fact that only an ASP.NET client can use it right now. Why not cache the states collection within your manager class and let your client worry about caching (if desired) an instance of your manager class?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 30-Jun-2005 10:56:17   

I agree with Jim, you should try to leave ties with PL code out of your BL, if possible.

After all, caching is nothing more than using a hashtable with a key to index and to find back the cached object/collection.

The advantage of focussing on caching in the GUI automatically moves the focus on what's cached from input data for a process to processed data. Which is even more efficient. simple_smile

Frans Bouma | Lead developer LLBLGen Pro
Sam avatar
Sam
User
Posts: 95
Joined: 30-Jun-2004
# Posted on: 30-Jun-2005 16:03:52   

Thanks for the input guys!