CacheAdapter always creates new config instance

Issue #40 resolved
Former user created an issue

In order to use multiple cache providers with different configurations (not stored in app.config) I've created provider factory that accepts configuration as input param:

public ICacheProvider GetCacheProvider(CacheConfiguration configuration, ILogging logging = null)
        {
            ICacheProvider ret = null;

            try
            {
                ret = CacheBinder.ResolveCacheFromConfig(configuration, logging);
                CacheBinder.Logger.WriteInfoMessage(string.Format("Initialised cache of type: {0}", CacheBinder.Configuration.CacheToUse));
            }
            catch (Exception err)
            {
                var outerEx = new ApplicationException(string.Format("Problem initialising cache of type: {0}", CacheBinder.Configuration.CacheToUse), err);
                CacheBinder.Logger.WriteException(outerEx);
                throw outerEx;
            }

            return ret;
        }

The problem is that CacheProvider creates new instance of CacheConfig (initialized from MainConfig or app.config) and does not use config provided by resolver.

public class CacheProvider : ICacheProvider
    {
        private readonly ICache _cache;
        private readonly ILogging _logger;
        private CacheConfig _config = new CacheConfig();

I suppose that config should be added in CacheProvider constructor.

Comments (3)

  1. Paul Glavich repo owner

    Thanks for reporting. Could you elaborate on your use case so I can get a better idea of how you use the CacheAdapter in the multiple cache scenario? Is this for using an in memory primary cache and a secondary memcached or redis type scenario?

  2. Davorin

    I have a similar issue. It is not possible to pass the configuration set by AppServices.SetConfig() to a CacheProvider. I've created the following patch to fix that.

    diff -r 955bcf575be9 Glav.CacheAdapter/CacheProvider.cs
    --- a/Glav.CacheAdapter/CacheProvider.cs        Fri Mar 04 12:31:13 2016 +1100
    +++ b/Glav.CacheAdapter/CacheProvider.cs        Mon Jun 13 17:22:54 2016 +0200
    @@ -25,15 +25,16 @@
                 : this(cache, logger, null, null)
             {
             }
    -        public CacheProvider(ICache cache, ILogging logger, ICacheDependencyManager cacheDependencyManager)
    -            : this(cache, logger, cacheDependencyManager, null)
    +        public CacheProvider(ICache cache, ILogging logger, CacheConfig config, ICacheDependencyManager cacheDependencyManager)
    +            : this(cache, logger, config, cacheDependencyManager, null)
             {
             }
    
    -        public CacheProvider(ICache cache, ILogging logger, ICacheDependencyManager cacheDependencyManager, ICacheFeatureSupport featureSupport)
    +        public CacheProvider(ICache cache, ILogging logger, CacheConfig config, ICacheDependencyManager cacheDependencyManager, ICacheFeatureSupport featureSupport)
             {
                 _cache = cache;
                 _logger = logger;
    +            _config = config;
                 _featureSupport = featureSupport ?? new CacheFeatureSupport(cache);
                 if (_config.IsCacheDependencyManagementEnabled)
                 {
    diff -r 955bcf575be9 Glav.CacheAdapter/DependencyInjection/CacheAdapterResolver.cs
    --- a/Glav.CacheAdapter/DependencyInjection/CacheAdapterResolver.cs     Fri Mar 04 12:31:13 2016 +1100
    +++ b/Glav.CacheAdapter/DependencyInjection/CacheAdapterResolver.cs     Mon Jun 13 17:22:54 2016 +0200
    @@ -31,7 +31,7 @@
                 {
                     var dependencyManager = GetCacheDependencyManager(config, cache);
    
    -                provider = new CacheProvider(cache, _logger, dependencyManager);
    +                provider = new CacheProvider(cache, _logger, config, dependencyManager);
                 }
                 else
                 {
    
  3. Log in to comment