/// <summary> /// Initializes a new instance of L1L2RedisCache. /// </summary> public L1L2RedisCache( IMemoryCache l1Cache, IOptions <L1L2RedisCacheOptions> l1l2RedisCacheOptionsAccessor, Func <IDistributedCache> l2CacheAccessor, IMessagePublisher messagePublisher, IMessageSubscriber messageSubscriber) { L1Cache = l1Cache; L1L2RedisCacheOptions = l1l2RedisCacheOptionsAccessor.Value; L2Cache = l2CacheAccessor(); MessagePublisher = messagePublisher; MessageSubscriber = messageSubscriber; Database = new Lazy <IDatabase>(() => L1L2RedisCacheOptions .ConnectionMultiplexerFactory() .GetAwaiter() .GetResult() .GetDatabase( L1L2RedisCacheOptions .ConfigurationOptions? .DefaultDatabase ?? -1)); MessageSubscriber.Subscribe(); }
/// <summary> /// Adds L1L2RedisCache distributed caching services to the specified <c>IServiceCollection</c>. /// </summary> /// <returns>The <c>IServiceCollection</c> so that additional calls can be chained.</returns> public static IServiceCollection AddL1L2RedisCache( this IServiceCollection services, Action <L1L2RedisCacheOptions> setupAction) { var l1L2RedisCacheOptions = new L1L2RedisCacheOptions(); setupAction.Invoke(l1L2RedisCacheOptions); services.AddOptions(); services.Configure(setupAction); services.Configure <L1L2RedisCacheOptions>( (options) => { if (options.ConnectionMultiplexerFactory == null) { if (options.ConfigurationOptions != null) { options.ConnectionMultiplexerFactory = () => Task.FromResult( ConnectionMultiplexer.Connect( options.ConfigurationOptions) as IConnectionMultiplexer); } else { options.ConnectionMultiplexerFactory = () => Task.FromResult( ConnectionMultiplexer.Connect( options.Configuration) as IConnectionMultiplexer); } } }); services.AddMemoryCache(); services.AddSingleton( provider => new Func <IDistributedCache>( () => new RedisCache( provider.GetService <IOptions <L1L2RedisCacheOptions> >()))); services.AddSingleton <IDistributedCache, L1L2RedisCache.L1L2RedisCache>(); services.AddSingleton <IConfigurationVerifier, ConfigurationVerifier>(); services.AddSingleton <DefaultMessagePublisher>(); services.AddSingleton <NoopMessagePublisher>(); services.AddSingleton <IMessagePublisher>( serviceProvider => { var options = serviceProvider .GetRequiredService <IOptions <L1L2RedisCacheOptions> >() .Value; return(options.MessagingType switch { MessagingType.Default => serviceProvider.GetRequiredService <DefaultMessagePublisher>(), _ => serviceProvider.GetRequiredService <NoopMessagePublisher>(), }); });
public DefaultMessagePublisher( IOptions <JsonSerializerOptions> jsonSerializerOptions, IOptions <L1L2RedisCacheOptions> l1L2RedisCacheOptionsOptionsAccessor) { JsonSerializerOptions = jsonSerializerOptions.Value; L1L2RedisCacheOptions = l1L2RedisCacheOptionsOptionsAccessor.Value; Subscriber = new Lazy <ISubscriber>(() => L1L2RedisCacheOptions .ConnectionMultiplexerFactory() .GetAwaiter() .GetResult() .GetSubscriber()); }
public KeyeventMessageSubscriber( IConfigurationVerifier configurationVerifier, IMemoryCache l1Cache, IOptions <L1L2RedisCacheOptions> l1L2RedisCacheOptionsOptionsAccessor, ILogger <KeyeventMessageSubscriber>?logger = null) { ConfigurationVerifier = configurationVerifier; L1Cache = l1Cache; L1L2RedisCacheOptions = l1L2RedisCacheOptionsOptionsAccessor.Value; Logger = logger; Subscriber = new Lazy <ISubscriber>(() => L1L2RedisCacheOptions .ConnectionMultiplexerFactory() .GetAwaiter() .GetResult() .GetSubscriber()); }