Caching Application Block and BackingStore - Please help ;-)

Topics: Caching Application Block , Data Access Application Block
Feb 15, 2009 at 4:03 PM
Hi Guys,

I am trying to work with the Cache Application Block and BackingStore and i need your help with two small issues:

1. I have my cache saved to an SQL backing store. So if my application crashed - it loads back the items from the SQL database.
The issue is that i'm changing the objects in the cache - and the changes doesn't apply in the database unless i explicitly re-insert them to the cache.
Is this the way it should be? It seems that only in the moment of insertion they are saved to DB. Thus the DB is not fully synchronized with the items in the cache.
I can handle saving them in crutial points - maybe it is even better since is saves un-needed saving operations, but just want to know if this is how it should be.

2. The second one is more complicated... The scenario i'm dealing with is as follows:
- when an item is being expired from the cache - i'm saving it to my application operational database.
- if an item is not in the cache - i'm retrievng it from the operational database
- What will happan if:
    * An item is expiring and should be saved to database
    * At the same time - a code line is trying to access the item
    * It seems that the cache removes an item and only then handles the Refresh action
    * This can cause issues - since the Refresher might not complete the save operation to the database and the retrieving operation will result in a non-updated data
How could i make the cache not remove the item from the cache until a complete save operation was preformed?
If it removed it before that - there might be inconsistency... since the data was not saved to the main operational database and a retrive operation might take an un-updated version of the item.

Is this how it works, maybe i'm wrong, does the cache remove the item and only then handles the refresh procedure.
Is there a way to insert a code that is being executed BEFORE an item is being removed from the cache and not AFTER?

Feb 16, 2009 at 7:16 AM
Hi Gili,

On your first item, are you saying that when you edit the in memory representation of the cached data from the backing store, any updates is not persisted on the backingstore?

On your second item, is your concern is that when you try to poll something from the cache and its not there (beacause it has expired or due to refresh) and will cause troubles? You can try implementing ICacheItemRefreshAction just like mention to this posts:

Valiant A. Dudan
Global Technology & Solutions
Avanade, Inc.
Feb 16, 2009 at 8:44 AM

This is exactly what i meant - but there isn't a build in solution in the thread you gave me.
I don't want the item to automatically be removed when an item expires - i want to first dump it into database and only then remove it from the cache.

The problem with the current schedualer is that it removes the item from the cache and only then handles the refresh action.
I'm using the refresh to dump/save items that are being removed from the cache to database.
This may cause integrity issues, since if the refresher didn't complete the save operation while i was re-asking for the object - the object will be retrieved from database and only then the refresher will save it - this is an integrity issue - since i should have for the object with the changes.
I can alternatively - when an item is being saved to the cache - save them to database - but this adds more delay time to the save/update operation which is one of the reasons we're moving to cache.

The thread you gave me refers to the same problem but from a diffrent prespective - it says that the user should have control weather the item is being removed from the cache on a refresh action or not - which will suit my problem - since i'll be able to save it first and only then remove it from the cache.

Either this is a bug - or i'm missing something importent here, can you hlep?
I assume implementing the ICacheItemExpiration can be a solution - do you have a reference on how to implement it?

Thanx alot - Gili.

Feb 16, 2009 at 9:45 AM
see this :
Feb 16, 2009 at 10:51 AM
Hi Guys,

First - thanx for the fast replies!!!

Before i get to the ICacheItemExpiration implementation - did i get the way the cache works correctly?
I mean - does the cache manager / expirer first remove the item from the cache and only then handles the refresh action?

The article you've sent me is very superficial. Let me explain further what i would want my implememntation to do:

1. I want the has expired to use the same expiration machanism that is being given by default by the cache manager - thus, for example, i'm using sliding time
and i want the item to be marked as expired if the last access time to that object exceeded certain threshold.
How do i access the actual items of the cache? Can i use the default implementation to determine if an item has expired? if not - how would i keep track on the item.
I couldn't find in the cache manager a way to access the items and get information related to their caching.

2. What about the notify - what should i put there? Should i send an event to the cache? How do i do that?
I think that if the above is solveable - i can first save the item to database and only then notify the cache that it can remove the item - this will be a good solution.

But i have yet got the idea onl how to notify the cache or how to check if an item was expired, thus for example check the last access time and decide if it is expired or not.

Please help, this machanism looks great but the problem i'm expiriencing is really bad - specially since it is not configurable.

If there is any other way to tell the cache not to remove the item until the refresh is done completely - it will be a better solution.

Feb 16, 2009 at 4:48 PM
Edited Feb 16, 2009 at 4:51 PM
Feb 16, 2009 at 8:10 PM
I have succeeded in implementing ICacheItemExpiration,
So for those who might need it:





public class CacheExpirer : ICacheItemExpiration




SlidingTime _slidingTime;



public CacheExpirer(SlidingTime slidingTime)



_slidingTime = slidingTime;



public bool HasExpired()




//save the object to database before the item is being removed from cache



bool hasExpired=_slidingTime.HasExpired();



if (hasExpired == true)



Console.WriteLine("Item has expired");



return hasExpired;




public void Initialize(CacheItem owningCacheItem)




//not in use




public void Notify()






Feb 16, 2009 at 8:12 PM
I have posted this again in a diffrent thread - just noticed you have refered my issue,
so this is the situation i'm dealing with:

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

MyCachedItem mci=_cache["key"];

If my application cracshes immediatly after the execution of the aboce rows - 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).

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?
Feb 17, 2009 at 1:37 AM
See the other thread -

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.