private static async Task ValidateRemove() { var opts = new DistributedCacheOptions { Configuration = new ConfigurationOptions { EndPoints = { { "127.0.0.1", 6379 } }, ClientName = "dbg-client" }, Gzip = true }; var cache = new RedisCache <TestData>(opts); var data = new TestData { LongValue = 10000, StrValue = "Hello, World" }; await cache.SetAsync("rm-key", data).ConfigureAwait(false); var result = await cache.GetAsync("rm-key").ConfigureAwait(false); if (result.StrValue != data.StrValue || result.LongValue != data.LongValue) { throw new SystemException("Invalid cache read back!"); } await cache.RemoveAsync("rm-key").ConfigureAwait(false); try { await cache.GetAsync("rm-key").ConfigureAwait(false); throw new ApplicationException("Expected an ArgumentOutOfRangeException!"); } catch (ArgumentOutOfRangeException) { } }
private static async Task ValidateTimeout() { var opts = new DistributedCacheOptions { Configuration = new ConfigurationOptions { EndPoints = { { "127.0.0.1", 6379 } }, ClientName = "dbg-client" } }; var cache = new RedisCache <TestData>(opts); var data = new TestData { LongValue = 10000, StrValue = "Hello, World" }; var options = new CacheEntryOptions { Timeout = TimeSpan.FromSeconds(1) }; await cache.SetAsync("abc", data, options).ConfigureAwait(false); Thread.Sleep(1250); try { await cache.GetAsync("abc").ConfigureAwait(false); throw new SystemException("Key should not be found but is found!"); } catch (ArgumentOutOfRangeException) { } }
public DistributedCacheWithMetrics(DistributedCacheOptions <TCacheInstance> options, IMetrics metrics, AllowedMetrics <DistributedCacheWithMetrics <TCacheInstance> > settings) : base(options) { this.Helper = new MetricsHelper <TCacheInstance>(metrics, settings.AllowedCacheMetrics); if (settings.AllowedCacheMetrics.HasFlag(CacheMetrics.HitRatio)) { this.Helper.MetricsObj.RegisterOneMinuteRate( Metrics.Distributed.HitRatio, Metrics.Distributed.HitCount, Metrics.Distributed.TotalCount, this.Helper.MetricsTags); } if (settings.AllowedCacheMetrics.HasFlag(CacheMetrics.ErrorRatio)) { this.Helper.MetricsObj.RegisterOneMinuteRate( Metrics.Distributed.ErrorRatio, Metrics.Distributed.ErrorCount, Metrics.Distributed.TotalCount, this.Helper.MetricsTags); } options.OnGetError += Options_OnGetError; }
public static IServiceCollection AddCacheStrategy(this IServiceCollection services, CacheConfig config, DatabaseConfig db) { services.AddMemoryCache(); services.AddSingleton <IMemoryCache <string, string>, MemoryCache <string, string> >(); if (config.Type == "Distributed") { services.AddSingleton <IDistributedCache <string> >(p => { var options = new DistributedCacheOptions { Configuration = new ConfigurationOptions { EndPoints = { { db.Redis.Host, 6379 } }, ClientName = "sensate-iot" } }; return(new RedisCache <string>(options)); }); services.AddScoped <ICacheStrategy <string>, DistributedCacheStrategy>(); } else { services.AddScoped <ICacheStrategy <string>, MemoryCacheStrategy>(); } return(services); }
public BulkLoadTest(IOptions <DistributedCacheOptions> options) { if (options == null) { throw new ArgumentNullException(nameof(options), "Options cannot be null!"); } this.m_options = options.Value; }
public RedisCache(IOptions <DistributedCacheOptions> options) { if (options == null) { throw new ArgumentNullException(nameof(options), "Options cannot be null!"); } this.m_options = options.Value; this.m_gzip = options.Value.Gzip; this.m_networkLock = new SemaphoreSlim(1, 1); }
public DependencyModule(ILogger <DependencyModule> logger, SharedOptions sharedOptions, StorageOptions storageOptions, DistributedCacheOptions distributedCacheOptions, DataProtectionOptions dataProtectionOptions, ResponseCachingOptions responseCachingOptions) { _logger = logger; _sharedOptions = sharedOptions; _storageOptions = storageOptions; _distributedCacheOptions = distributedCacheOptions; _dataProtectionOptions = dataProtectionOptions; _responseCachingOptions = responseCachingOptions; }
public DependencyModule( ILogger <DependencyModule> logger, SharedOptions sharedOptions, CompaniesHouseOptions coHoOptions, ResponseCachingOptions responseCachingOptions, DistributedCacheOptions distributedCacheOptions, DataProtectionOptions dataProtectionOptions) { _logger = logger; _sharedOptions = sharedOptions; _coHoOptions = coHoOptions; _responseCachingOptions = responseCachingOptions; _distributedCacheOptions = distributedCacheOptions; _dataProtectionOptions = dataProtectionOptions; }
public static IServiceCollection AddDistributedCaches <TValue>(this IServiceCollection services, string host, int port) where TValue : class { services.AddSingleton <IDistributedCache <TValue> >(p => { var opts = new DistributedCacheOptions { Configuration = new ConfigurationOptions { EndPoints = { { host, port } }, ClientName = "SensateIoT" } }; return(new RedisCache <TValue>(opts)); }); return(services); }
private static void RunRedisCache() { var opts = new DistributedCacheOptions { Configuration = new ConfigurationOptions { EndPoints = { { "localhost", 6379 } }, AbortOnConnectFail = true, ClientName = "LoadTest-01", } }; var test = new BulkLoadTest(Options.Create(opts)); test.Run(2_510_918); }
public static IServiceCollection AddRedisDistributedCache <TCacheInstance>(this IServiceCollection services, Action <RedisCacheOptions> setupInner = null, Action <DistributedCacheOptions <TCacheInstance> > setup = null) { RedisCacheOptions innerOptions = new RedisCacheOptions(); setupInner?.Invoke(innerOptions); DistributedCacheOptions <TCacheInstance> options = new DistributedCacheOptions <TCacheInstance>(new RedisCache(Options.Create(innerOptions))); setup?.Invoke(options); services.TryAddSingleton(options); services.TryAddSingleton <IDistributedCache <TCacheInstance>, DistributedCache <TCacheInstance> >(); return(services); }
private static async Task ValidateRangeSet() { var opts = new DistributedCacheOptions { Configuration = new ConfigurationOptions { EndPoints = { { "127.0.0.1", 6379 } }, ClientName = "dbg-client" }, Gzip = true }; var cache = new RedisCache <TestData>(opts); var entries = new List <Abstract.KeyValuePair <string, TestData> > { new Abstract.KeyValuePair <string, TestData> { Key = "p1", Value = new TestData { LongValue = 10001, StrValue = "Hello, World 1" } }, new Abstract.KeyValuePair <string, TestData> { Key = "p2", Value = new TestData { LongValue = 10002, StrValue = "Hello, World 2" } }, new Abstract.KeyValuePair <string, TestData> { Key = "p3", Value = new TestData { LongValue = 10003, StrValue = "Hello, World 3" } }, }; await cache.SetRangeAsync(entries).ConfigureAwait(false); var keys = new List <string> { "p1", "p2", "p3" }; var tmp = await cache.GetRangeAsync(keys).ConfigureAwait(false); var results = tmp.ToList(); if (results[0].StrValue != "Hello, World 1" || results[1].LongValue != 10002) { throw new SystemException("Invalid read back from server!"); } }
private static async Task ValidateRangeGet() { var opts = new DistributedCacheOptions { Configuration = new ConfigurationOptions { EndPoints = { { "127.0.0.1", 6379 } }, ClientName = "dbg-client" }, Gzip = true }; var cache = new RedisCache <TestData>(opts); var d1 = new TestData { LongValue = 10001, StrValue = "Hello, World 1" }; var d2 = new TestData { LongValue = 10002, StrValue = "Hello, World 2" }; var d3 = new TestData { LongValue = 10003, StrValue = "Hello, World 3" }; await Task.WhenAll( cache.SetAsync("k1", d1), cache.SetAsync("k2", d2), cache.SetAsync("k3", d3) ); var entries = new List <string> { "k1", "k2", "k3" }; var tmp = await cache.GetRangeAsync(entries).ConfigureAwait(false); var results = tmp.ToList(); if (results[0].StrValue != "Hello, World 1" || results[1].LongValue != 10002) { throw new SystemException("Invalid read back from server!"); } }
private static async Task ValidateSetGetAsync() { var opts = new DistributedCacheOptions { Configuration = new ConfigurationOptions { EndPoints = { { "127.0.0.1", 6379 } }, ClientName = "dbg-client" } }; var cache = new RedisCache <TestData>(opts); var data = new TestData { LongValue = 10000, StrValue = "Hello, World" }; await cache.SetAsync("abc", data).ConfigureAwait(false); var result = await cache.GetAsync("abc").ConfigureAwait(false); if (result.StrValue != data.StrValue || result.LongValue != data.LongValue) { throw new SystemException("Invalid cache read back!"); } }
public static IServiceCollection AddDistributedCache(this IServiceCollection services, DistributedCacheOptions option) { services.AddMemoryCache(); services.AddDistributedMemoryCache(); services.AddSingleton <IDistributedCacheSerializer, Utf8JsonDistributedCacheSerializer>(); services.AddSingleton <IDistributedCacheKeyNormalizer, DistributedCacheKeyNormalizer>(); services.AddSingleton(typeof(IDistributedCache <>), typeof(DistributedCache <>)); services.AddSingleton(typeof(IDistributedCache <,>), typeof(DistributedCache <,>)); return(services); }