public void GetValue_Multithreads() { var azureManager = Substitute.For <IAzureManager>(); azureManager.FileExists(Arg.Any <string>()).Returns(x => x.Arg <string>() == "/some/key"); var cache = new AzureBlobCache <string>(azureManager, true); var results = new ConcurrentBag <string>(); void Action(int number) => results.Add(cache.GetValue("/some/key", x => $"value-{number}")); var threads = new Thread[10]; for (var i = 0; i < threads.Length; i++) { int number = i; threads[i] = new Thread(() => Action(number)); } foreach (Thread thread in threads) { thread.Start(); } foreach (Thread thread in threads) { thread.Join(); } Assert.Equal(10, results.Count); Assert.Equal(1, results.Distinct().Count()); }
static Nested() { try { var cacheConfig = App.Config.Sys.Cache; if (null == Config) { Config = new MdCacheConfig(cacheConfig.Identifier, cacheConfig.CacheName, cacheConfig.AuthorizationToken); } if (MasterConfig.ConfigStorageConnectionString != null) { Trace.TraceInformation("[MdCache] Attaching to BlobCache {0}", MasterConfig.ConfigStorageConnectionString); _instance = new AzureBlobCache(MasterConfig.ConfigStorageConnectionString); } else if (!string.IsNullOrWhiteSpace(cacheConfig.LocalCacheDir)) { Trace.TraceInformation("[MdCache] Attaching to LocalCache"); _instance = new LocalMdCache(); } else if (Config.CacheName != null) { _instance = new AzureMdCache(Config); } else { _instance = new NullCache(); } } catch (Exception e) { Trace.TraceError("[MdCache] Nested.Nested() failed. {0}", e); throw; } }
public void AddOrUpdate() { var azureManager = Substitute.For <IAzureManager>(); azureManager.FileExists(Arg.Any <string>()).Returns(x => x.Arg <string>() == "/some/key"); var cache = new AzureBlobCache <string>(azureManager, true); cache.AddOrUpdate("/some/key", "value"); string result2 = cache.GetValue("/some/key", x => "should_not_be"); Assert.Equal("value", result2); }