Caching Application Block Sync with Backing Store (SQL DB)

Topics: Caching Application Block , Data Access Application Block
Feb 16, 2009 at 9:04 PM
Edited Feb 16, 2009 at 10:47 PM
Hi Guys,

I am using the Caching Application Block with an SQL BackingStore.
I wrote the bellow code:

MyCachedItem mci=_cache["key"];
mci.Value1=1;

If my application cracshes immediatly after the execution of the above code- when it is being reloaded the cache backing store is not loading with the correct value, _cach["key"].Val1=1.
Unless i explicitly re-add the item to the cache - the changes are not applied to the backing store (SQL database):
_cache.Add("key",mci);

This means my code must look like:

MyCachedItem mci=_cache["key"];
mci.Value1=1;
_cache.Add("key",mci);

to keep the backing store synced with the cache.

Is this normal? Should i re-add my item everytime i want the item to be saved in the backing store?
Isn't the backing store used for persistency? Isn't persistency means that the cache is alwats synced with the backing store?

Maybe i am missing something here,
Can you please help?

THANX!
-Gili
Feb 17, 2009 at 2:35 AM
In your other post, I read that you are modifying the item in the cache.  Is this the code you're referring to? 

MyCachedItem mci=_cache["key"];
mci.Value1=1;

You didn't modify the value of the item in the cache, you just retrieved it and assigned it to a cache item and the value of the cache item is the one which you modified. 

And yes, you should add the item to modify it since the cacheManager[key] is only used to retrieve the item, not to modify it.  If you're going to look at the source code, it just uses a Hashtable to store the items. 

hashtable[key] = objectvalue

just overwrites (if exists) or creates (if not) the value of the key within the collection.


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com
Feb 17, 2009 at 9:03 AM
Edited Feb 17, 2009 at 9:06 AM
Sarah,

First - Thanx!!

1. When does the cache saves an item to the backing store? Is there a scheduler or only when an item is being explicitly added to the cache?

2. Does the cache hold references to its items or copies of them? Meaning, if i retrieve an item, change its inner values and later on (before it was expired) try to retrieve it back from the cache - will my changes apply?

3. If i write the following code:

MyCachedItem mci=_cache["key"];
mci.Value1=1;

And then the cache expires my item - will i still be able to use the mci reference?

4. One last thing:

You wrote: "In your other post, I read that you are modifying the item in the cache" - Is there a way to modify the items in the cache such that the backing store will be kept synced with the changes?



Where/How can i read the open source of the cache manager? Maybe i'll have less questions then ;-)


Thanx guys - you ROCK!
-Gili
Feb 17, 2009 at 9:53 AM
1.  The item is being saved when you explicitly add it to the cachemanager. 
2.  No, the cachemanager does not hold references to copies of its items.  Like a hashtable, if you retrieve an item from it, changing that item doesn't change the value in the hashtable.
3.  Yes, as related to answer to number 2.
4.  If you add an item using an existing key, then it will replace the existing entry.  So the way to modify an item in the cache is to re-add it. 
5.  Upon installing entlib, the source code installer will immediately follow.  If you cancelled it, there is an Enterprise Library Source Code Installer which can be found in Start -> Programs -> Microsoft Patterns and Practices -> Enterprise Library version - date.  You can open it in Visual Studio.


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com
Feb 17, 2009 at 6:26 PM
Edited Feb 17, 2009 at 6:28 PM
Sarah,

I meant that the cache manager holds references to it objects and not copies of them. This is right, isn't it?

This means that  if i change the object of a cache item / hash, since the cache holds a reference to that object - when i'll retake it - i'll get the same reference - thus with the changes i've applied.

Of course that this is unless the object had expired - which is cool - just want to make sure i get things right.

Thanx,
Gili

Feb 18, 2009 at 2:04 AM
Sorry for not making it clear.  Yes, I do get what you mean.  As you explained before, you expect that this code

MyCachedItem mci=_cache["key"];
mci.Value1=1;

will change the value of _cache["key"] the next time you retrieve it to 1.  My answer would be no.  You can try it, just assign the value of _cache["key"] to an object, change the value of an object, the next time you retrieve it, you will still get the same value unless you add the new value to the cache manager.


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com
Feb 18, 2009 at 9:57 AM
Edited Feb 18, 2009 at 9:58 AM
Sarah,

I have tried it, check this code:

            ph = dcm["1"];
            Console.WriteLine("ph val1 (before chaning) is = " + ph.val1);
            ph.val1 = 222;
            ph2 = dcm["1"];
            Console.WriteLine("ph2 val1 is = "+ph2.val1);

The output is:

ph val1 (before chaning) is = 0
Item 1 retrieved from cache
ph2 val1 is = 222


Am i missing something?

Thanx,
Gili


Feb 18, 2009 at 10:11 AM
Can I see your implementation of Value1 property?


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com
Feb 18, 2009 at 10:22 AM
Sure,
My object is called Dependency, the class code is:

    [Serializable]
    public class Dependency
    {
        public string ID;
        public int val1;
        public Dependency ref1;
        public string str1;

        public Dependency(string DepID)
        {
            this.ID = DepID;
        }
    }
Feb 19, 2009 at 9:11 AM
Hi,

After trying your code, it seems that when you try to edit the value of something that is a primitive type in the in-memory representation cache, it doesnt get updated. But when you use a Custom class just like what you did, the in memory cache gets updated when you perform edit to the item. We are not sure if that would also update the backing store or it really requires you to explicitly add again in order to sync.


Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com