EPiServer and cache, not always a love story

by: Fredrik Karlsson

When you are working with some more backend stuff of EPiServer, you are never really sure if you load it from the database or from the cache. Here are a few examples.

PageDefinitions

When you want to load a PageDefinition, the normal way would be:

EPiServer.DataAbstraction.PageDefinition.Load(int id);
The problem is that PageDefinition.Load goas straight to the database, no cache in sight. What you can do is to use the List-method:
EPiServer.DataAbstraction.PageDefinition.List(int PageTypeId);

cause this is actually cached.

So, theres a good way and a bad way:

// Loads from database
PageDefinition pageDef = PageDefinition.Load(
CurrentPage.Property[0].PageDefinitionID
); // loads from cache PageDefinition pageDefinition = PageDefinition.List(
CurrentPage.PageTypeID
).First( pd => pd.ID == CurrentPage.Property[0].PageDefinitionID );

Dynamic Properties

If you ever want to list all dynamic properties from a page, you get the same problem. When the most logical method goes to the database, you have to work around it again.

// Loads from database
DynamicPropertyCollection dynPropertiesDb = 
DynamicProperty.ListForPage( CurrentPage.PageLink ); // Loads from cache DynamicPropertyCollection dynPropertiesCache =
new DynamicPropertyCollection();
// List all from PageType = 0 = Dynamic proerties PageDefinitionCollection pdc = PageDefinition.List(0); foreach (PageDefinition pd in pdc) dynPropertiesCache.Add(new DynamicProperty( CurrentPage.PageLink,
CurrentPage.ParentLink,
CurrentPage.Property[pd.Name]) );

Conclusion

Sure, these two examples doesn't matter that much in a normal installation. But if you are working on something special it can be annoying. And, if you ask me, it demonstrates very clearly the "works good enough" mentality you sometimes hate in EPiServer. And don't get me started on why EPiServer can't cache a page version. So remember, Reflector is your friend. Always!

06 October 2008


Comments

  1. Nice Linq construct, I'll pretend I did not read the conclusion.
  2. There is an expression in English which is "Don't sh*t on your own doorstep". I think you know what I'm getting at ;-|
  3. Great! Thanks Fredrik! I've been struggling a lot with EPiServer cache and it would be great with more information in the SDK. Perhaps EPiServer could open up the SDK as a world-wiki-page attached?
  4. Great post. Good to know. And I agree reflector is your best friend.
Post a comment    
User verification Image for user verification  
Fredrik Karlsson

About me

System Specialist at Dropit. Contact info:
email: fredrik@dropit.se
cell: +46 708 80 10 05
direct: +46 10 588 64 07
msn: karf@hotmail.com
new blog: http://labs.dropit.se/blogs/author/fredrik.karlsson.aspx

Syndications


Archive


Tag cloud

EPiTrace logger