Turn off or disable caching

Topics: Caching Application Block
Oct 22, 2009 at 4:56 PM

Is there an ability to turn off caching completely?

For example when I want to do a performance test to compare with and without caching, or something like that.

Oct 23, 2009 at 5:22 AM

Hi,

There is no direct way to do that within ent lib out-of-the-box. But what you can do is to implement a cache manager that does nothing. Here is a sample code to show my idea.

    [ConfigurationElementType(typeof(CustomCacheManagerData))]
    public class CachingOffCacheManager : ICacheManager
    {
        public CachingOffCacheManager(NameValueCollection collection)
        { 

        }

        #region ICacheManager Members

        public void Add(string key, object value, CacheItemPriority scavengingPriority, ICacheItemRefreshAction refreshAction, params ICacheItemExpiration[] expirations)
        {
        }

        public void Add(string key, object value)
        {
        }

        public bool Contains(string key)
        {
            return false;
        }

        public int Count
        {
            get { return 0; }
        }

        public void Flush()
        {
        }

        public object GetData(string key)
        {
            return null;
        }

        public void Remove(string key)
        {
        }

        public object this[string key]
        {
            get { return null; }
        }

        #endregion
    }

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

Jul 29, 2014 at 9:03 AM
@AdvanceSupport, just one or two questions:
  1. What does the annotation do? [ConfigurationElementType(typeof(CustomCacheManagerData))]
    Would I have to configure something in the web.config for this to work?
  2. Also in the web.config what should this be changed to, to pick up the custom cache manager?
    <cacheManagers>
    <add name="CurrentCacheManager" type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" expirationPollFrequencyInSeconds="36000" maximumElementsInCacheBeforeScavenging="3000" numberToRemoveWhenScavenging="10" backingStoreName="inMemory"/>
    </cacheManagers>
Thanks,
Jacques
Aug 1, 2014 at 5:38 AM
[ConfigurationElementType(typeof(CustomCacheManagerData))]
This attribute associates an implementation class with the respective configuration class. In this case, it would indicate that the class is configured with CustomCacheManagerData. See Create a custom caching manager for Enterprise Library 4 for an example.

For the configuration, you would just change the type to the custom type of the cache manager. See http://stackoverflow.com/questions/1069597/extending-enterprise-library-caching-block-how-to-get-instance-of-mycachemanag as well.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to