示例#1
0
        /// <summary>
        /// This method gets called by the runtime. Use this method to add services to the container.
        /// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        /// </summary>
        /// <param name="services"></param>
        public void ConfigureServices(IServiceCollection services)
        {
            services.ConfigureMVC();

            var cacheOptions = new RedisCacheServiceOptions("127.0.0.1:6379");

            cacheOptions.ConfigurationOptions.AbortOnConnectFail = false;
            cacheOptions.ConfigurationOptions.ConnectTimeout     = 300;
            cacheOptions.ConfigurationOptions.SyncTimeout        = 300;
            cacheOptions.ConfigurationOptions.ConnectRetry       = 1;

            services.AddMilvaRedisCaching(cacheOptions);

            services.AddLocalization(options => options.ResourcesPath = "Resources");

            services.ConfigureDependencyInjection();

            services.ConfigureDatabase(Configuration);

            services.ConfigureIdentity(Configuration, JsonOperations);

            services.ConfigureVersioning();

            services.ConfigureSwagger();
        }
示例#2
0
 /// <summary>
 /// Initializes new instance of <see cref="RedisCacheService"/> with <paramref name="cacheServiceOptions"/>.
 /// </summary>
 /// <param name="cacheServiceOptions"></param>
 /// <param name="connectionMultiplexer"></param>
 public RedisCacheService(RedisCacheServiceOptions cacheServiceOptions, IConnectionMultiplexer connectionMultiplexer)
 {
     _options            = cacheServiceOptions.ConfigurationOptions;
     _useUtcForDateTimes = cacheServiceOptions.UseUtcForExpirationDates;
     _client             = connectionMultiplexer;
     _database           = _client?.GetDatabase();
 }
    /// <summary>
    /// Adds <see cref="IRedisCacheService"/> to <see cref="IServiceCollection"/> by singleton.
    /// </summary>
    /// <param name="services"></param>
    /// <param name="options"></param>
    /// <returns></returns>
    public static IServiceCollection AddMilvaRedisCaching(this IServiceCollection services, RedisCacheServiceOptions options)
    {
        services.AddSingleton(options);

        //Configure other services up here
        var multiplexer = ConnectionMultiplexer.ConnectAsync(options.ConfigurationOptions).Result;

        services.AddSingleton <IConnectionMultiplexer>(multiplexer);

        return(options.Lifetime switch
        {
            ServiceLifetime.Singleton => services.AddSingleton <IRedisCacheService, RedisCacheService>(),
            ServiceLifetime.Scoped => services.AddScoped <IRedisCacheService, RedisCacheService>(),
            ServiceLifetime.Transient => services.AddTransient <IRedisCacheService, RedisCacheService>(),
            _ => services,
        });