Cache Manager - should Contains fire Accessed?

Topics: Caching Application Block
May 30, 2009 at 3:08 AM

As per design of marking and sweeping process:
http://msdn.microsoft.com/en-us/library/cc309118.aspx

The contains method does not mark an object as accessed, should it?
instrumentationProvider.FireCacheAccessed

I came across this when using ICacheManager and trying to introduce a lock scope:

        public bool TryGetData<T>(string view, out T data)
        {
            lock (SyncRoot)
            {
                bool exists = _cacheManager.Contains( view);
                data = exists ? (T) _cacheManager[ view] : default(T);
                return exists;
            }
        }

I didn't think locking using the cache manager as the reference would be a good idea, hence an external lock scope.

My solution was:

        public bool TryGetData<T>(string view, out T data)
        {
            lock (SyncRoot)
            {
                object obj = (object)_cacheManager[view];
                bool exists = obj != null;
                data = exists ? (T)obj : default(T);
                return exists;
            }
        }

Cheers

Jun 1, 2009 at 7:21 AM

Correct me if I'm wrong, are you asking if the Contains method should mark the the item it is trying to check as accessed?  If this is what you're asking about, in my opinion, it shouldn't.  As the name suggests, the logic is that you just want to check if the item is in the cache.  You're not really doing anything to it.  But if changing its logic then you could always go for your modification.  Take advantage of entlib's free source code.

 

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