Caching objects

Posts   
 
    
Marian
User
Posts: 1
Joined: 24-Mar-2010
# Posted on: 24-Mar-2010 08:43:27   
Hi,

it is a good programming practice to keep llblgen objects (Entities, Collections) into HttpRuntime.Cache. It seems that I have some memory leaks into my application and I want to know if this can be the cause.

Thanks, Marian.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39797
Joined: 17-Aug-2003
# Posted on: 24-Mar-2010 09:44:44   

When you cache entities in a cache object like HttpRuntime.Cache, you've to take into account that relationships are maintained on both sides.

So say you store a Customer entity in the cache. If you then somewhere do: myOrder.Customer = cachedCustomer;

it will add 'myOrder' to cachedCustomer.Orders as well. Which will keep myOrder in memory till cachedCustomer is removed from the cache.

To avoid this, look into hiding one side of the relationship, namely the PK side (the '1' side in the 1:n relationship) in the designer. This of course means you'll lose the collection on the '1' side, but at the same time you don't run the risk that objects are kept in memory.

It's also good practice to cache objects only for a short period of time, unless all objects in the cache are more or less static.

Frans Bouma | Lead developer LLBLGen Pro