public virtual ICache CreateCache(string cacheName)
        {
            Condition.Requires(cacheName, nameof(cacheName)).IsNotNullOrWhiteSpace();

            var cacheOptions = _cacheOptions.GetMatchingOptions(cacheName);

            if (string.IsNullOrEmpty(cacheOptions.StoreName))
            {
                throw new ArgumentOutOfRangeException(nameof(cacheName), $"Cannot create cache '{cacheName}'.  No cache store has been configured.");
            }

            var store = _storeRegistry.GetStore(cacheOptions.StoreName);

            if (store == null)
            {
                throw new ArgumentException($"Cannot create cache '{cacheName}'.  Cache store '{cacheOptions.StoreName}' has not been configured.");
            }

            var cache = _caches.GetOrAdd(cacheName, key =>
            {
                _logger.LogDebug("Creating cache {CacheName} with store '{CacheStoreName}'", cacheName, store.Name);
                store.Initialize();
                var newCache = cacheOptions.CacheFactory(store, cacheName);
                (newCache as IPrioritizableCache)?.SetCacheDefaultPriority(cacheOptions.EntryPriority);
                return(newCache);
            });

            return(cache);
        }
Example #2
0
        public RedisCacheStore(
            ILoggerFactory loggerFactory,
            ICacheStoreSerializer objectSerializer,
            IMatchingOptions <RedisCacheOptions> options,
            string name)
        {
            Condition.Requires(loggerFactory, nameof(loggerFactory)).IsNotNull();
            Condition.Requires(options, nameof(options)).IsNotNull();
            Condition.Requires(objectSerializer, nameof(objectSerializer)).IsNotNull();

            Name              = name;
            _options          = options.GetMatchingOptions(Name);
            _objectSerializer = objectSerializer;
            _logger           = loggerFactory.CreateLogger($"{name}-{nameof(RedisCacheStore)}");
        }
Example #3
0
        public MemoryCacheStore(
            ILoggerFactory loggerFactory,
            IMatchingOptions <MemoryCacheStoreOptions> options,
            string name)
        {
            Condition.Requires(loggerFactory, nameof(loggerFactory)).IsNotNull();
            Condition.Requires(options, nameof(options)).IsNotNull();
            Condition.Requires(name, nameof(name)).IsNotNullOrEmpty();

            _options = options.GetMatchingOptions(name);
            _maxSize = _options.MaxSizeInBytes;
            Name     = name;

            _logger       = loggerFactory.CreateLogger($"{name}-{nameof(MemoryCacheStore)}");
            _cleanupTimer = new Timer(obj => CompactIfNeeded(), this, _options.PollingInterval, _options.PollingInterval);
        }