示例#1
0
        public IMemoryCache CreateMemoryCache(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("'name' cannot be null or empty", nameof(name));
            }

            return(s_memoryCacheStore.GetOrAdd(name, (cacheName) =>
            {
                IMemoryCache memoryCache = _cacheServiceProvider.ResolveMemoryCache();

                if (memoryCache == null)
                {
                    _logger.LogInformation($"Loaded '{nameof(NoopMemoryCache)}' for named cache '{{cache}}'", cacheName);
                    return NoopMemoryCache.Instance;
                }
                else
                {
                    CacheConfigurations cacheConfigurations =
                        _serviceProvider.GetService(typeof(CacheConfigurations)) as CacheConfigurations;

                    return new MemoryCacheDecorator(cacheName, memoryCache, cacheConfigurations ?? new CacheConfigurations());
                }
            }));
        }
        public static IServiceCollection AddCacheFactory(this IServiceCollection services, IConfigurationSection configurationSection)
        {
            // run this first as we want to override the configuration dependency
            services.RegisterAllDependendecies();

            var reader = new ConfigurationReader();
            CacheConfigurations cacheConfigurations = reader.Read(configurationSection);

            services.AddSingleton <CacheConfigurations>(cacheConfigurations);

            return(services);
        }
示例#3
0
        public IDistributedCache CreateDistributedCache(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("'name' cannot be null or empty", nameof(name));
            }

            return(s_distributedCacheStore.GetOrAdd(name, (cacheName) =>
            {
                IDistributedCache distributedCache = _cacheServiceProvider.ResolveDistributedCache();

                if (distributedCache == null)
                {
                    _logger.LogInformation($"Loaded '{nameof(NoopDistributedCache)}' for named cache '{{cache}}'", cacheName);
                    return NoopDistributedCache.Instance;
                }
                else
                {
                    IDataProtectionProvider dataProtectorProvider =
                        _serviceProvider.GetService <IDataProtectionProvider>();

                    IDataProtector dataProtector;
                    if (dataProtectorProvider == null)
                    {
                        dataProtector = NoopDataProtector.Instance;
                    }
                    else
                    {
                        const string purpose = "OpenWorks Distributed Cache";
                        dataProtector =
                            dataProtectorProvider.CreateProtector(purpose);
                    }

                    CacheConfigurations cacheConfigurations =
                        _serviceProvider.GetService(typeof(CacheConfigurations)) as CacheConfigurations;

                    return new DistributedCacheDecorator(
                        cacheName,
                        distributedCache,
                        dataProtector,
                        cacheConfigurations ?? new CacheConfigurations());
                }
            }));
        }