public void Put_should_put_item_using_passed_sliding_ttl_zero() { #if PORTABLE MemoryCacheImplementation memoryCache = new Microsoft.Extensions.Caching.Memory.MemoryCache(new Microsoft.Extensions.Caching.Memory.MemoryCacheOptions()); #else MemoryCacheImplementation memoryCache = System.Runtime.Caching.MemoryCache.Default; #endif string key = "anything"; object value = new object(); MemoryCacheProvider provider = new MemoryCacheProvider(memoryCache); TimeSpan minExpiration = #if PORTABLE TimeSpan.FromMilliseconds(1) // This is the minimum permitted sliding ttl for .NetStandard #else TimeSpan.Zero #endif ; Ttl ttl = new Ttl(minExpiration, false); provider.Put(key, value, ttl); Thread.Sleep(TimeSpan.FromMilliseconds(10)); #if PORTABLE object got; memoryCache.TryGetValue(key, out got); #else object got = memoryCache[key]; #endif got.Should().BeNull(); }
public void Put_should_put_item_using_passed_sliding_ttl_maxvalue() { #if PORTABLE MemoryCacheImplementation memoryCache = new Microsoft.Extensions.Caching.Memory.MemoryCache(new Microsoft.Extensions.Caching.Memory.MemoryCacheOptions()); #else MemoryCacheImplementation memoryCache = System.Runtime.Caching.MemoryCache.Default; #endif string key = "anything"; object value = new object(); MemoryCacheProvider provider = new MemoryCacheProvider(memoryCache); TimeSpan maxSlidingExpiration = #if PORTABLE TimeSpan.MaxValue #else TimeSpan.FromDays(365) // This is the maximum permitted sliding ttl for .NetFramework4.0 and 4.5 MemoryCache. #endif ; Ttl ttl = new Ttl(maxSlidingExpiration, true); provider.Put(key, value, ttl); #if PORTABLE object got; memoryCache.TryGetValue(key, out got); #else object got = memoryCache[key]; #endif got.Should().BeSameAs(value); }
public void Put_should_put_item_using_passed_sliding_ttl() { #if PORTABLE MemoryCacheImplementation memoryCache = new Microsoft.Extensions.Caching.Memory.MemoryCache(new Microsoft.Extensions.Caching.Memory.MemoryCacheOptions()); #else MemoryCacheImplementation memoryCache = System.Runtime.Caching.MemoryCache.Default; #endif TimeSpan shimTimeSpan = TimeSpan.FromSeconds(1); // If test fails transiently in different environments, consider increasing shimTimeSpan. string key = "anything"; object value = new object(); // Place an item in the cache that should last for only 2x shimTimespan MemoryCacheProvider provider = new MemoryCacheProvider(memoryCache); Ttl ttl = new Ttl(shimTimeSpan + shimTimeSpan, true); provider.Put(key, value, ttl); // Prove that we can repeatedly get it from the cache over a 5x shimTimespan period, due to repeated access. for (int i = 0; i < 5; i++) { #if PORTABLE object got; memoryCache.TryGetValue(key, out got); #else object got = memoryCache[key]; #endif got.Should().BeSameAs(value, $"at iteration {i}"); Thread.Sleep(shimTimeSpan); } }
private List <string> GetCacheKeys() { const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic; Type cacheType = cache.GetType(); MemoryCache memoryCache; if (cacheType == typeof(RabbitMQSynchronizedCache)) { memoryCache = (MemoryCache)cacheType.GetField("memoryCache", flags).GetValue(cache); } else if (cacheType == typeof(MemoryCache)) { memoryCache = (MemoryCache)cache; } else { throw new ArgumentException("不支持的缓存类型"); } var field = memoryCache.GetType().GetField("memoryCache", BindingFlags.NonPublic | BindingFlags.Static); Microsoft.Extensions.Caching.Memory.MemoryCache subCache = (Microsoft.Extensions.Caching.Memory.MemoryCache)field.GetValue(memoryCache); var entries = subCache.GetType().GetField("_entries", flags).GetValue(subCache); var cacheItems = entries as IDictionary; List <string> keys = new List <string>(); if (cacheItems != null) { foreach (DictionaryEntry cacheItem in cacheItems) { string key = cacheItem.Key.ToString(); keys.Add(key); } } return(keys); }
public void OnGet() { var cache = new Microsoft.Extensions.Caching.Memory.MemoryCache(new Microsoft.Extensions.Caching.Memory.MemoryCacheOptions()); var gameFactory = new StrangerThings.Game.GameFactory(new GoogleTranslator.Translator(@"D:\Development\Redweb\StrangerThings\StrangerThings\StrangerThings.Web\strangerthings.json")); var gameService = new StrangerThings.Game.GameService(cache, gameFactory); var game = gameService.CreateNewGame(); Game = game; }
public void Get_should_return_null_on_unknown_key() { #if PORTABLE MemoryCacheImplementation memoryCache = new Microsoft.Extensions.Caching.Memory.MemoryCache(new Microsoft.Extensions.Caching.Memory.MemoryCacheOptions()); #else MemoryCacheImplementation memoryCache = System.Runtime.Caching.MemoryCache.Default; #endif MemoryCacheProvider provider = new MemoryCacheProvider(memoryCache); object got = provider.Get(Guid.NewGuid().ToString()); got.Should().BeNull(); }
public void Should_not_throw_when_MemoryCacheImplementation_is_not_null() { #if PORTABLE MemoryCacheImplementation memoryCache = new Microsoft.Extensions.Caching.Memory.MemoryCache(new Microsoft.Extensions.Caching.Memory.MemoryCacheOptions()); #else MemoryCacheImplementation memoryCache = System.Runtime.Caching.MemoryCache.Default; #endif Action configure = () => new MemoryCacheProvider(memoryCache); configure.ShouldNotThrow(); }
public StorageCache() { #if NET47 MemoryCacheProvider memoryCacheProvider = new MemoryCacheProvider(System.Runtime.Caching.MemoryCache.Default); #else Microsoft.Extensions.Caching.Memory.IMemoryCache memoryCache = new Microsoft.Extensions.Caching.Memory.MemoryCache(new Microsoft.Extensions.Caching.Memory.MemoryCacheOptions()); MemoryCacheProvider memoryCacheProvider = new MemoryCacheProvider(memoryCache); #endif Cache = Policy.Cache(memoryCacheProvider, cacheDuration); }
public AnswerResult Get(string id, string place, string time) { var cache = new Microsoft.Extensions.Caching.Memory.MemoryCache(new Microsoft.Extensions.Caching.Memory.MemoryCacheOptions()); var gameFactory = new StrangerThings.Game.GameFactory(new GoogleTranslator.Translator(@"D:\Development\Redweb\StrangerThings\StrangerThings\StrangerThings.Web\strangerthings.json")); var gameService = new StrangerThings.Game.GameService(cache, gameFactory); var game = gameService.GetGameFromId(id); var result = new AnswerResult(); result.LocationCorrect = game.Answer.Place.Name == place; result.TimeCorrect = game.Answer.Time.ToString() == time; result.Answer = $"{game.Answer.Place.Name} at {game.Answer.Time.ToString()}"; return(result); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { Microsoft.Extensions.Caching.Memory.MemoryCache memoryCache = new Microsoft.Extensions.Caching.Memory.MemoryCache(new Microsoft.Extensions.Caching.Memory.MemoryCacheOptions() { }); services.AddSingleton(typeof(Microsoft.Extensions.Caching.Memory.MemoryCache), memoryCache); services.AddControllersWithViews() .ConfigureApiBehaviorOptions(options => { options.SuppressModelStateInvalidFilter = true; }) .AddNewtonsoftJson(options => { //options.UseMemberCasing(); }).AddResultCodeConfiguration <ResultCodeSettingEnum>(new ResultDefinition(memoryCache));; services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); // In production, the React files will be served from this directory services.AddSpaStaticFiles(configuration => { configuration.RootPath = "ClientApp/build"; }); }
public void Put_should_put_item_using_passed_nonsliding_ttl() { #if PORTABLE MemoryCacheImplementation memoryCache = new Microsoft.Extensions.Caching.Memory.MemoryCache(new Microsoft.Extensions.Caching.Memory.MemoryCacheOptions()); #else MemoryCacheImplementation memoryCache = System.Runtime.Caching.MemoryCache.Default; #endif TimeSpan shimTimeSpan = TimeSpan.FromSeconds(0.1); // If test fails transiently in different environments, consider increasing shimTimeSpan. string key = "anything"; object value = new object(); MemoryCacheProvider provider = new MemoryCacheProvider(memoryCache); Ttl ttl = new Ttl(shimTimeSpan, false); provider.Put(key, value, ttl); // Initially (before ttl expires), should be able to get value from cache. #if PORTABLE object got; memoryCache.TryGetValue(key, out got); #else object got = memoryCache[key]; #endif got.Should().BeSameAs(value); // Wait until the TTL on the cache item should have expired. Thread.Sleep(shimTimeSpan + shimTimeSpan); #if PORTABLE memoryCache.TryGetValue(key, out got); #else got = memoryCache[key]; #endif got.Should().NotBeSameAs(value); got.Should().BeNull(); }
public void Get_should_return_instance_previously_stored_in_cache() { #if PORTABLE MemoryCacheImplementation memoryCache = new Microsoft.Extensions.Caching.Memory.MemoryCache(new Microsoft.Extensions.Caching.Memory.MemoryCacheOptions()); #else MemoryCacheImplementation memoryCache = System.Runtime.Caching.MemoryCache.Default; #endif string key = "anything"; object value = new object(); #if PORTABLE using (Microsoft.Extensions.Caching.Memory.ICacheEntry entry = memoryCache.CreateEntry(key)) { entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(10); entry.Value = value; } #else memoryCache[key] = value; #endif MemoryCacheProvider provider = new MemoryCacheProvider(memoryCache); object got = provider.Get(key); got.Should().BeSameAs(value); }
public void Put_should_put_item_using_passed_nonsliding_ttl_maxvalue() { #if PORTABLE MemoryCacheImplementation memoryCache = new Microsoft.Extensions.Caching.Memory.MemoryCache(new Microsoft.Extensions.Caching.Memory.MemoryCacheOptions()); #else MemoryCacheImplementation memoryCache = System.Runtime.Caching.MemoryCache.Default; #endif string key = "anything"; object value = new object(); MemoryCacheProvider provider = new MemoryCacheProvider(memoryCache); Ttl ttl = new Ttl(TimeSpan.MaxValue, false); provider.Put(key, value, ttl); #if PORTABLE object got; memoryCache.TryGetValue(key, out got); #else object got = memoryCache[key]; #endif got.Should().BeSameAs(value); }
/// <summary> /// 缓存 /// </summary> private static void Caching() { //Install-Package Microsoft.Extensions.Caching.Memory Microsoft.Extensions.Caching.Memory.IMemoryCache memoryCache = new Microsoft.Extensions.Caching.Memory.MemoryCache(new Microsoft.Extensions.Caching.Memory.MemoryCacheOptions()); //Install-Package Polly.Caching.MemoryCache Polly.Caching.MemoryCache.MemoryCacheProvider memoryCacheProvider = new Polly.Caching.MemoryCache.MemoryCacheProvider(memoryCache); CachePolicy policy = Policy.Cache(memoryCacheProvider, TimeSpan.FromSeconds(5)); Random rand = new Random(); while (true) { int i = rand.Next(5); Console.WriteLine("产生" + i); var context = new Context("doublecache" + i); int result = policy.Execute(ctx => { Console.WriteLine("Execute计算" + i); return(i * 2); }, context); Console.WriteLine("计算结果:" + result); Thread.Sleep(500); } }
public void Put_should_put_item_into_configured_MemoryCacheImplementation() { #if PORTABLE MemoryCacheImplementation memoryCache = new Microsoft.Extensions.Caching.Memory.MemoryCache(new Microsoft.Extensions.Caching.Memory.MemoryCacheOptions()); #else MemoryCacheImplementation memoryCache = System.Runtime.Caching.MemoryCache.Default; #endif string key = Guid.NewGuid().ToString(); object value = new object(); MemoryCacheProvider provider = new MemoryCacheProvider(memoryCache); Ttl ttl = new Ttl(TimeSpan.FromSeconds(10)); provider.Put(key, value, ttl); #if PORTABLE object got; memoryCache.TryGetValue(key, out got); #else object got = memoryCache[key]; #endif got.Should().BeSameAs(value); }
private static IDistributedCache CreateForNet46() { var memoryCache = new Microsoft.Extensions.Caching.Memory.MemoryCache(new Microsoft.Extensions.Caching.Memory.MemoryCacheOptions()); return(new MemoryDistributedCache(memoryCache)); }