Esempio n. 1
0
        /// <summary>
        /// Returns a named <see cref="IDeliveryCacheManager"/>.
        /// </summary>
        /// <param name="name">A name of <see cref="IDeliveryCacheManager"/> configuration</param>
        /// <returns>The <see cref="IDeliveryCacheManager"/> instance that represents named cache manager</returns>
        public IDeliveryCacheManager Get(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (!_cache.TryGetValue(name, out var manager))
            {
                var deliveryCacheManagerFactoryOptions = _optionsMonitor.Get(name);
                var deliveryCacheOptions = deliveryCacheManagerFactoryOptions?.DeliveryCacheOptions.LastOrDefault()?.Invoke();
                if (deliveryCacheOptions != null)
                {
                    if (deliveryCacheOptions.CacheType == CacheTypeEnum.Memory)
                    {
                        var memoryCache = _serviceProvider.GetService <IMemoryCache>();
                        manager = new MemoryCacheManager(memoryCache, Options.Create(deliveryCacheOptions));
                    }
                    else
                    {
                        var distributedCache = _serviceProvider.GetService <IDistributedCache>();
                        manager = new DistributedCacheManager(distributedCache, Options.Create(deliveryCacheOptions));
                    }

                    _cache.TryAdd(name, manager);
                }
            }

            return(manager);
        }
        /// <summary>
        /// Returns a named instance of the <see cref="IDeliveryClient"/> wrapped in a caching layer.
        /// </summary>
        /// <param name="name">A name of the configuration to be used to instantiate the client.</param>
        /// <returns>Returns an <see cref="IDeliveryClient"/> instance with the given name.</returns>
        public IDeliveryClient Get(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (!_cache.TryGetValue(name, out var client))
            {
                client = _innerClientFactory.Get(name);
                var cacheOptions = _deliveryCacheOptions.Get(name);
                if (cacheOptions.Name == name)
                {
                    // Build caching services according to the options
                    IDeliveryCacheManager manager;
                    if (cacheOptions.CacheType == CacheTypeEnum.Memory)
                    {
                        var memoryCache = _serviceProvider.GetService <IMemoryCache>();
                        manager = new MemoryCacheManager(memoryCache, Options.Create(cacheOptions));
                    }
                    else
                    {
                        var distributedCache = _serviceProvider.GetService <IDistributedCache>();
                        manager = new DistributedCacheManager(distributedCache, Options.Create(cacheOptions));
                    }

                    // Decorate the client with a caching layer
                    client = new DeliveryClientCache(manager, client);

                    _cache.TryAdd(name, client);
                }
            }

            return(client);
        }