Caching App Block: pausing expiration time check

Topics: Caching Application Block
Jul 7, 2008 at 2:49 PM
Hi,

i"m trying to pause the expiration time check - make some actions that require everything to stay - and then continue with the check.

haven't found a way of doing it.

Can anybody help me?

Yanai
Jul 7, 2008 at 4:24 PM
Hi,

There is no way of doing that in int he cache manager API.

You could extend the ExpirationTask class to have an expirations-suspended mode, and wire the cache manager yourself using an instance of this new class instead of the default one, but you'd need to keep track of both the cache manager and the expiration task. Wiring the caching objects up can be tricky, too; look at the CacheManagerFactoryHelper class for an example of how this wiring-up is done.

Fernando


yanairon wrote:
Hi,

i"m trying to pause the expiration time check - make some actions that require everything to stay - and then continue with the check.

haven't found a way of doing it.

Can anybody help me?

Yanai



Jul 15, 2008 at 1:44 PM
Hi,

Thanks a lot for the reply.

I've managed to do a workaround:
wrapping the expiration item i want with a custom one
Do you think it could work?

    [Serializable]
    public class CacheItemExpiration : ICacheItemExpiration
    {
        #region Data Members
        private SlidingTime m_InnerExpiration;
        private static bool s_ExpirationEnabled = true;
        #endregion

        #region Constructor
        public CacheItemExpiration()
        {
            m_InnerExpiration = new SlidingTime(TimeSpan.FromMinutes(10));           
        }
        #endregion

        #region ICacheItemExpiration Members
        public bool HasExpired()
        {
            return m_InnerExpiration.HasExpired() && s_ExpirationEnabled;
        }

        public void Initialize(CacheItem owningCacheItem)
        {
            m_InnerExpiration.Initialize(owningCacheItem);
        }

        public void Notify()
        {
            m_InnerExpiration.Notify();
        }
        #endregion    

        #region Public Static
        public static void HaltExpirationCheck()
        {
            s_ExpirationEnabled = false;
        }

        public static void ResumeExpirationCheck()
        {
            s_ExpirationEnabled = true;
        }
        #endregion
    }
Jul 15, 2008 at 2:27 PM

Hi,

That's a neat approach! In fact, you could add a .ctor or a static method to create a wrapper for any possible ICacheItemExpiration instead of just the sliding time one.

I'm not thrilled about the static variable, though. Because statics are evil en general, but also because it will pause expirations for all caches. How about externalizing this into a simple class with a boolean-typed Enabled property (I can only think of the name ExpirationStatus, but it's not quite the right one) and pass it along when you create the wrapper expiration. You would then be free to use different instances of the status for different caches, or just one if that's what you need (but you wouldn't be forced to do it).

Another thing, which may be an issue for you is that you is that it would be possible for your app to suspend expirations while an expiration run is in progress. The override-the-expiration-task approach lets you have control over this (but it's somewhat more complex).

Fernando


yanairon wrote:
Hi,

Thanks a lot for the reply.

I've managed to do a workaround:
wrapping the expiration item i want with a custom one
Do you think it could work?



Jul 16, 2008 at 10:48 AM
Hi,

Actually that's a good idea.

altough i do have just one cache, it's much nicer then the static methods.

Thanks, i'll use it

Yanai