Modifing caching instance

Topics: Caching Application Block
Apr 3, 2011 at 12:36 AM

If I call GetData() and get an instance of the object that is cached can I modify it? Is the instance that is passed back passed "by reference" or do I need to assume a kind of checkout and checkin model? If the later what methods would I call?

Thank you.

Apr 4, 2011 at 6:04 AM

The Caching Application Block maintains a strong reference to objects retrieved from it of reference type.  Thus, if you have added a Person object to the cache manager, then retrieved it and modify one of it's properties, that change will get reflected the next time you retrieved that object from the cache manager.

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Apr 4, 2011 at 12:41 PM

What happens if while I am modifying the object that it gets “scavenged” and gets written to the backing store? What “locks” the object so that I am ensured that the modifications made it intact (like a transaction)?

From: AvanadeSupport [email removed]
Sent: Monday, April 04, 2011 1:04 AM
To: rkevinburton@charter.net
Subject: Re: Modifing caching instance [entlib:252285]

From: AvanadeSupport

The Caching Application Block maintains a strong reference to objects retrieved from it of reference type. Thus, if you have added a Person object to the cache manager, then retrieved it and modify one of it's properties, that change will get reflected the next time you retrieved that object from the cache manager.

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Apr 5, 2011 at 6:26 AM
Edited Apr 5, 2011 at 6:37 AM

When you call the GetData method, it checks first if the item has already expired, if yes, it returns null but if not, it will automatically mark the item as not eligible for scavenging.  You can see this logic by navigating to the definition of the code below

cacheItemBeforeLock.TouchedByUserAction(false);

which can be found in the Cache.GetData method.

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Apr 5, 2011 at 1:03 PM

So how long does this marking last? I don’t see a method to “unmark” it.

From: AvanadeSupport [email removed]
Sent: Tuesday, April 05, 2011 1:27 AM
To: rkevinburton@charter.net
Subject: Re: Modifing caching instance [entlib:252285]

From: AvanadeSupport

When you call the GetData method, it checks first if the item has already expired, if yes, it returns null but if not, it will automatically mark the item as not eligible for scavenging. You can see this logic in the GetData method specifically the line

cacheItemBeforeLock.TouchedByUserAction(false);

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Apr 6, 2011 at 12:42 AM

Actually, the word "marking" means it updates the cache item's last accessed time to the current time so that it won't get removed from the cache since it's just been recently accessed.  There's no unmarking process; the expiration policy or policies you used for the cache item still determines when you're item should get removed from the cache.

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Apr 6, 2011 at 3:29 AM
So the assumption is that the expiration policy is time based (as opposed to file or event based). Also the expiration time is further in the future than the time used to modify the object. Right?

On Apr 5, 2011, at 7:42 PM, "AvanadeSupport"<notifications@codeplex.com> wrote:

From: AvanadeSupport

Actually, the word "marking" means it updates the cache item's last accessed time to the current time so that it won't get removed from the cache since it's just been recently accessed. There's no unmarking process; the expiration policy or policies you used for the cache item still determines when you're item should get removed from the cache.

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Apr 6, 2011 at 4:24 AM

No, there's no assumption.   But it specifically updates the lastAccessedTime field since this will be useful for items which you didn't associate any expiration policy.  I didn't mention the rest of the logic but in addition to updating the timestamp, another logic it executes is that if the item has associated expiration policies, the Notify method will be called for each of them to notify that the item was recently accessed by the user.  Thus, it is through the Notify method by which you will be able to do some logic (if you want to) of preventing the item to be included in the next scavenging process. 

For the FileDependency, it has no logic for this method because it doesn't matter; the item will expire if the file has been changed, regardless whether the item was recently used or not.  For event-based expiration, you'll definitely have a custom expiration policy implemented in your code so you'll need to put a logic in the Notify method if you want to make sure your item won't expire while it is being updated. 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Apr 6, 2011 at 2:18 PM
Do you have a pointer or example of setting up 'Notify'?

On Apr 5, 2011, at 11:24 PM, "AvanadeSupport"<notifications@codeplex.com> wrote:

From: AvanadeSupport

No, there's no assumption. But it specifically updates the lastAccessedTime field since this will be useful for items which you didn't associate any expiration policy. I didn't mention the rest of the logic but in addition to updating the timestamp, another logic it executes is that if the item has associated expiration policies, the Notify method will be called for each of them to notify that the item was recently accessed by the user. Thus, it is through the Notify method by which you will be able to do some logic (if you want to) of preventing the item to be included in the next scavenging process.

For the FileDependency, it has no logic for this method because it doesn't matter; the item will expire if the file has been changed, regardless whether the item was recently used or not. For event-based expiration, you'll definitely have a custom expiration policy implemented in your code so you'll need to put a logic in the Notify method if you want to make sure your item won't expire while it is being updated.

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Apr 7, 2011 at 12:40 AM
Edited Apr 7, 2011 at 12:49 AM

You'll need to implement the ICacheItemExpiration.  You can look at the EntLib source code to see the various implementations of that interface.  Only the SlidingTime expiration policy contains logic for the Notify method but the idea is there.

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Apr 7, 2011 at 3:12 AM

That is when the item expires. You indicated that there was a notification when it is accessed.

From: AvanadeSupport [email removed]
Sent: Wednesday, April 06, 2011 7:40 PM
To: rkevinburton@charter.net
Subject: Re: Modifing caching instance [entlib:252285]

From: AvanadeSupport

You'll need to implement the ICacheItemExpiration. You can look at the EntLib source code to see the various implementations of that interface. However, only the SlidingTime expiration policy contains logic for it but the idea is there.

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Apr 7, 2011 at 3:55 AM

No, I didn't say it is being called when the item expires.  The Notify method is being called when the cache item is accessed by the user, that is, it eventually gets called when you call GetData on a cache manager object.  Here's the definition of this method on the ICacheItemExpiration interface.

         /// <summary>
        /// Called to tell the expiration that the CacheItem to which this expiration belongs has been touched by the user
        /// </summary>
        void Notify();

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Apr 7, 2011 at 12:30 PM

I guess the name ICacheItemExpiration threw me off. Thank you.

From: AvanadeSupport [email removed]
Sent: Wednesday, April 06, 2011 10:56 PM
To: rkevinburton@charter.net
Subject: Re: Modifing caching instance [entlib:252285]

From: AvanadeSupport

No, I didn't say it is being called when the item expires. The Notify method is being called when the cache item is accessed by the user, that is, it eventually gets called when you call GetData on a cache manager object. Here's the definition of this method on the ICacheItemExpiration interface.

/// <summary>
/// Called to tell the expiration that the CacheItem to which this expiration belongs has been touched by the user
/// </summary>
void Notify();

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Oct 25, 2011 at 9:48 AM

How modify instance without Modifying caching instance?