Does GetItem return a reference to the cached object

Topics: Caching Application Block
Jul 9, 2011 at 4:08 PM


I am using Entlib Caching Application Block in a project,

I have multiple threads concurrently doing a getItem, perform some updates on the object, then replace the original object in cache with the new object

Since these calls are concurrent,  I want to know if the EntLib Caching returns a reference to the cached object, or hands out a deep-copy of the object to each caller

I  know Entlib Caching guarantees thread safety, just wondering about the getitem, does it hand out a reference or a new object

Jul 11, 2011 at 4:12 AM
Edited Jul 11, 2011 at 4:29 AM


It returns the reference to the cached object rather a copy of it. So when you modify the cached object, the changes will be reflected across all the variables that is referencing that object.  


Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.

Jul 11, 2011 at 6:23 AM


So this could potentially cause data inconsistency issues, as one user could be reading while another is writing.

Does Entlib provide for a way to sync access to the cached items in a multi-threaded, multi-user environment?


Jul 11, 2011 at 8:28 AM
Edited Jul 11, 2011 at 8:29 AM

I've looked into the source code of caching and I can say it is implementing sync access. First, cached items are stored on HashTable and it uses SyncRoot to make enumeration thread safe. After retrieving the specific cache item, it is then put into lock by using a monitor so only a single thread can have access to the object whether it is a read (GetData) or modify (Add, Remove) operation. If you look at the caching source code, the approach is consistently implemented on GetData, Add and Remove method.


Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.

Jul 13, 2011 at 12:35 AM

Thanks a million

Aug 10, 2011 at 3:22 PM

In our scenario, we store datatable objects in the cache. Lets assume the following.

User1: calls GetDataObject and gets a reference to the cached object and starts updating some of the fields in the dataTable.

User2: calls GetDataObject while User1 is still updating, gets a reference to the same object, but the table is still being updated by User1

This results in User2 seeing the incomplete update from User1 and a few mins later, if User2 makes another getdata request, she gets the completed datatable.

I am guessing Entlib expected the useage to be readonly for cache objects, in this case, we get the datatable from cache, update and then re-insert.

Since we are getting a reference, there is no reason to re-insert, but the multi user update problem is a killer

Aug 10, 2011 at 7:51 PM

The caching block is multi-threading safe, but it is explicitly NOT multi-user / process safe. If you need a distributed cache, use a distributed cache. The Entlib caching block is not a distributed cache.