Problem implementing server side caching

Problem implementing server side caching

Old forum URL: forums.lhotka.net/forums/t/9334.aspx


ThomasHepp posted on Friday, August 06, 2010

Hallo,

I'am using Csla for my current project. It's a website. The DataPortal run's in a seperate windows service. I want to cache frequently used BOs server side. In my examle I want to cache my User and my UserGroup BOs. I implemented it very easy:

        private static Users _users;

     

        public static Users Get()

        {

            if (_users == null)

            {

                SelectCriteria crit = new SelectCriteria(typeof(Users));

                _users = DataPortal.Fetch<Users>(crit);

            }

            return _users;

        }

 

This code is working perfekt, but now I want to access the "cache" from the User BO:

        public static User Get(Guid id)

        {

            Users users = Users.Get();

            return users.GetByID(id);

        }

This is working until I want wo save this user bo instance, because it is a child object of Users. Now I tried the following:

        public static User Get(Guid id)

        {

            Users users = Users.Get();

            return users.GetByID(id).Clone();

        }

But my clone is still a child object. Now the question: How can I mark the clone of a child as "Not Child"? Or is there a better way.

Thank's

 

 

ajj3085 replied on Wednesday, August 11, 2010

It sounds like you want to cache user data which doesn't frequently change, but then you're trying to use cached data to update.

You'd probably be better off creating a UserList which contains UserInfo objects.  these would be a ROLB and RO objects.  Getting the cached user data seems like a different use case with different requirements, and thus seperate BOs to handle them.

Your current user class would then only be used for editing (and you'd probably want to invalidate the cache of userinfo when this is saved as well).

RockfordLhotka replied on Wednesday, August 11, 2010

Caching actual business objects is essentially unworkable, because CSLA isn't threadsafe. Web servers are multithreaded, hence the conflict.

You can cache serialized objects, because when you deserialize the data you get your own copy of the object. That serialization/deserialization can be somewhat expensive though, so you should do perf testing to make sure it is workable for your application.

As Andy notes, it is probably better to consider caching the data than to cache the objects themselves.

Copyright (c) Marimer LLC