public static void AddKentico(this IServiceCollection services, IWebHostEnvironment environment, IConfiguration configuration, ProjectOptions projectOptions)
        {
            var cacheOptions = new DeliveryCacheOptions();

            cacheOptions.CacheType = CacheTypeEnum.Memory;
            //The preview and dev environments turn off caching so that the preview button works within Kentico Kontent
            if (environment.IsDevelopment() || environment.IsEnvironment("LocalDevelopment"))
            {
                cacheOptions.StaleContentExpiration = TimeSpan.FromSeconds(1);
                cacheOptions.DefaultExpiration      = TimeSpan.FromSeconds(1);
            }
            else //Production caches everything
            {
                cacheOptions.StaleContentExpiration = TimeSpan.FromSeconds(2);
                cacheOptions.DefaultExpiration      = TimeSpan.FromHours(24);
            }
            services.Configure <DeliveryCacheOptions>((options) =>
            {
                options = cacheOptions;
            });

            services.AddSingleton <ITypeProvider, CustomTypeProvider>();

            //https://github.com/Kentico/kontent-delivery-sdk-net/wiki/Registering-the-DeliveryClient-to-the-IServiceCollection-in-ASP.NET-Core#registering-multiple-clients
            foreach (var config in projectOptions.AzureSearchConfigs)
            {
                services.AddDeliveryClient(config.Name, config.DeliveryOptions);
                services.AddDeliveryClientCache(config.Name, cacheOptions);
            }
        }
Beispiel #2
0
 /// <summary>
 /// Maps one <see cref="DeliveryCacheOptions"/> object to another.
 /// </summary>
 /// <param name="o">A destination.</param>
 /// <param name="options">A source.</param>
 public static void Configure(this DeliveryCacheOptions o, DeliveryCacheOptions options)
 {
     o.CacheType              = options.CacheType;
     o.DefaultExpiration      = options.DefaultExpiration;
     o.DefaultExpirationType  = options.DefaultExpirationType;
     o.StaleContentExpiration = options.StaleContentExpiration;
     o.Name = options.Name;
 }
        public void Configure(DeliveryCacheOptions options)
        {
            var o = new DeliveryCacheOptions();

            o.Configure(options);

            o.Should().BeEquivalentTo(options);
        }
        public void GetNamedCacheClient_WithWrongName_GetNull()
        {
            var deliveryCacheOptions = new DeliveryCacheOptions();

            A.CallTo(() => _deliveryCacheOptionsMock.Get(_clientName))
            .Returns(deliveryCacheOptions);

            var deliveryClientFactory = new NamedDeliveryClientCacheFactory(_innerDeliveryClientFactoryMock, _deliveryCacheOptionsMock, _serviceCollection.BuildServiceProvider(), _autofacServiceProvider);

            var result = deliveryClientFactory.Get("WrongName");

            result.Should().NotBeNull();
        }
Beispiel #5
0
        public void GetNamedDeliveryCacheManager_WithCorrectName_GetNull(CacheTypeEnum cacheType)
        {
            var deliveryOptions = new DeliveryCacheOptions
            {
                CacheType = cacheType
            };

            var deliveryCacheManagerFactoryOptions = new DeliveryCacheManagerFactoryOptions();

            deliveryCacheManagerFactoryOptions.DeliveryCacheOptions.Add(() => deliveryOptions);

            A.CallTo(() => _deliveryCacheManagerFactoryOptionsMock.Get(_clientName))
            .Returns(deliveryCacheManagerFactoryOptions);

            var deliveryCacheManagerFactory = new DeliveryCacheManagerFactory(_deliveryCacheManagerFactoryOptionsMock, _serviceCollection.BuildServiceProvider());

            var result = deliveryCacheManagerFactory.Get("WrongName");

            result.Should().BeNull();
        }
        /// <summary>
        /// Registers a delegate that will be used to configure a cached <see cref="IDeliveryClient"/>.
        /// </summary>
        /// <param name="services">A <see cref="IServiceCollection"/> instance for registering and resolving dependencies.</param>
        /// <param name="options">A <see cref="DeliveryCacheOptions"/> instance.</param>
        /// <returns>The <paramref name="services"/> instance with cache services registered in it</returns>
        public static IServiceCollection AddDeliveryClientCache(this IServiceCollection services, DeliveryCacheOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options), "The Delivery cache  options object is not specified.");
            }

            return(services
                   .Configure <DeliveryCacheOptions>((o) => o.Configure(options))
                   .RegisterDependencies(options.CacheType)
                   .Decorate <IDeliveryClient, DeliveryClientCache>());
        }
Beispiel #7
0
        private static IServiceCollection RegisterCacheOptions(this IServiceCollection services, DeliveryCacheOptions options, string name = null)
        {
            void Configure(DeliveryCacheOptions o)
            {
                o.DefaultExpiration      = options.DefaultExpiration;
                o.StaleContentExpiration = options.StaleContentExpiration;
                o.CacheType = options.CacheType;
                o.Name      = name;
            }

            if (name == null)
            {
                services.Configure <DeliveryCacheOptions>(Configure);
            }
            else
            {
                services.Configure <DeliveryCacheOptions>(name, Configure);
            }

            return(services);
        }
Beispiel #8
0
        /// <summary>
        ///  Registers a delegate that will be used to configure a cached <see cref="IDeliveryClient"/>.
        /// </summary>
        /// <param name="services">A <see cref="IServiceCollection"/> instance for registering and resolving dependencies.</param>
        /// <param name="name">A name of named client which want to use cached <see cref="IDeliveryClient"/></param>
        /// <param name="options">A <see cref="DeliveryCacheOptions"/> instance. </param>
        /// <returns>The <paramref name="services"/> instance with cache services registered in it</returns>
        public static IServiceCollection AddDeliveryClientCache(this IServiceCollection services, string name, DeliveryCacheOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options), "The Delivery cache  options object is not specified.");
            }

            return(services
                   .RegisterCacheOptions(options, name)
                   .RegisterDependencies(options.CacheType, name)
                   .Decorate <IDeliveryClientFactory, DeliveryClientCacheFactory>());
        }
        /// <summary>
        ///  Registers a delegate that will be used to configure a cached <see cref="IDeliveryClient"/>.
        /// </summary>
        /// <param name="services">A <see cref="IServiceCollection"/> instance for registering and resolving dependencies.</param>
        /// <param name="name">A name of named client which want to use cached. <see cref="IDeliveryClient"/></param>
        /// <param name="options">A <see cref="DeliveryCacheOptions"/> instance. </param>
        /// <returns>The <paramref name="services"/> instance with cache services registered in it</returns>
        public static IServiceCollection AddDeliveryClientCache(this IServiceCollection services, string name, DeliveryCacheOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options), "The Delivery cache  options object is not specified.");
            }

            options.Name = name;

            return(services
                   .Configure <DeliveryCacheOptions>(name, (o) => o.Configure(options))
                   .RegisterDependencies(options.CacheType, name)
                   .RegisterDeliveryClientFactoryCacheDecorator());
        }
Beispiel #10
0
 private static IServiceCollection RegisterCacheOptions(this IServiceCollection services, DeliveryCacheOptions options)
 {
     services.Configure <DeliveryCacheOptions>(o =>
     {
         o.DefaultExpiration      = options.DefaultExpiration;
         o.StaleContentExpiration = options.StaleContentExpiration;
     });
     return(services);
 }
Beispiel #11
0
        /// <summary>
        ///  Registers a delegate that will be used to configure a cached <see cref="IDeliveryClient"/>.
        /// </summary>
        /// <param name="services">A <see cref="IServiceCollection"/> instance for registering and resolving dependencies.</param>
        /// <param name="name">A name of named client which want to use cached <see cref="IDeliveryClient"/></param>
        /// <param name="options">A <see cref="DeliveryCacheOptions"/> instance. </param>
        /// <returns>The <paramref name="services"/> instance with cache services registered in it</returns>
        public static IServiceCollection AddDeliveryClientCache(this IServiceCollection services, string name, DeliveryCacheOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options), "The Delivery cache  options object is not specified.");
            }

            services
            .RegisterNamedDependencies(options.CacheType)
            .AddTransient <IConfigureOptions <DeliveryCacheManagerFactoryOptions> >(sp =>
            {
                return(new ConfigureNamedOptions <DeliveryCacheManagerFactoryOptions>(name, o =>
                {
                    o.DeliveryCacheOptions.Add(() =>
                    {
                        return options;
                    });
                }));
            });

            return(services);
        }
        /// <summary>
        ///  Registers a delegate that will be used to configure a cached <see cref="IDeliveryClient"/>.
        /// </summary>
        /// <param name="services">A <see cref="IServiceCollection"/> instance for registering and resolving dependencies.</param>
        /// <param name="name">A name of named client which want to use cached <see cref="IDeliveryClient"/></param>
        /// <param name="options">A <see cref="DeliveryCacheOptions"/> instance. </param>
        /// <returns>The <paramref name="services"/> instance with cache services registered in it</returns>
        public static IServiceCollection AddDeliveryClientCache(this IServiceCollection services, string name, DeliveryCacheOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options), "The Delivery cache  options object is not specified.");
            }

            services
            .RegisterCacheOptions(options)
            .RegisterDependencies()
            .AddTransient <IConfigureOptions <DeliveryClientFactoryOptions> >(sp =>
            {
                return(new ConfigureNamedOptions <DeliveryClientFactoryOptions>(name, o =>
                {
                    var client = o.DeliveryClientsActions.FirstOrDefault()?.Invoke();
                    o.DeliveryClientsActions.Add(() =>
                    {
                        var deliveryCacheManager = sp.GetRequiredService <IDeliveryCacheManager>();
                        return new DeliveryClientCache(deliveryCacheManager, client);
                    });
                }));
            });

            return(services);
        }