private static async Task CacheSingleTest() { using (var cachePool = new CacheClientPoolAsync("Pool Test") { Serializer = GlobalSerializer }) { try { var cacheClient = await CacheClientProxy.GetClientAsync(new DefaultTransportClient("127.0.0.1", 20051, 1, GlobalSerializer)).ConfigureAwait(false); cachePool.Add("localhost:20051", cacheClient, StorageItemMode.ReadAndWrite); for (var i = 0; i < 15; i++) { await cachePool.GetKeysAsync().ConfigureAwait(false); } for (var i = 0; i < 500; i++) { var key = "test-" + i; await cachePool.GetAsync(key).ConfigureAwait(false); await cachePool.SetAsync(key, "bla bla bla bla bla bla").ConfigureAwait(false); } } catch (Exception ex) { Core.Log.Write(ex); } } }
protected override async Task OnHandlerAsync(ParameterHandlerInfo info) { Core.DebugMode = false; Core.Log.Warning("Starting CACHE Async TEST"); var cacheService = new TestCacheService(); cacheService.OnStart(null); using (var cachePool = new CacheClientPoolAsync("Pool Test") { Serializer = GlobalSerializer, ForceAtLeastOneNetworkItemEnabled = false, WriteNetworkItemsToMemoryOnGet = true }) { var cacheClient = await CacheClientProxy.GetClientAsync(new DefaultTransportClient("127.0.0.1", 20051, 3, GlobalSerializer)).ConfigureAwait(false); cachePool.Add("localhost:20051", cacheClient, StorageItemMode.ReadAndWrite); //cachePool.Add("memory", new LRU2QStorage(2000), StorageItemMode.ReadAndWrite); var res = await cachePool.GetMetaAsync("metaValue").ConfigureAwait(false); await cachePool.GetKeysAsync().ConfigureAwait(false); using (var watch = Watch.Create("GetKeysAsync")) { for (var i = 0; i < 1000; i++) { await cachePool.GetKeysAsync().ConfigureAwait(false); } Core.Log.InfoBasic("Time Per Item: {0}ms", watch.GlobalElapsedMilliseconds / 1000); } Console.ReadLine(); using (var watch = Watch.Create("Get And Sets")) { for (var i = 0; i < 5000; i++) { var key = "test-" + (i % 500); await cachePool.GetAsync(key).ConfigureAwait(false); await cachePool.SetAsync(key, "bla bla bla bla bla").ConfigureAwait(false); } Core.Log.InfoBasic("Time Per Item: {0}ms", watch.GlobalElapsedMilliseconds / 5000); } } cacheService.OnStop(); Console.ReadLine(); }
/// <summary> /// Gets the cache client connection pool /// </summary> /// <param name="name">Cache name</param> /// <returns>Cache client pool</returns> public async Task <CacheClientPoolAsync> GetCacheClientAsync(string name) { if (Caches?.Contains(name) != true) { return(null); } var cacheConfig = Caches[name]; var cConfig = cacheConfig.ClientOptionsList?.FirstOf( c => c.EnvironmentName?.SplitAndTrim(",").Contains(Core.EnvironmentName) == true && c.MachineName?.SplitAndTrim(",").Contains(Core.MachineName) == true, c => c.EnvironmentName?.SplitAndTrim(",").Contains(Core.EnvironmentName) == true, c => c.EnvironmentName.IsNullOrWhitespace()); if (cConfig?.Pool == null) { return(null); } var pingDelay = cConfig.Pool.PingDelay.ParseTo(5000); var pingDelayOnError = cConfig.Pool.PingDelayOnError.ParseTo(30000); var readMode = cConfig.Pool.ReadMode; var writeMode = cConfig.Pool.WriteMode; var selectionOrder = cConfig.Pool.SelectionOrder; var forceNetworkItem = cConfig.Pool.ForceAtLeastOneNetworkItemEnabled; ISerializer serializer; if (cConfig.Pool.SerializerMimeType.IsNotNullOrEmpty()) { serializer = SerializerManager.GetByMimeType(cConfig.Pool.SerializerMimeType); Ensure.ReferenceNotNull(serializer, $"The Serializer \"{cConfig.Pool.SerializerMimeType}\" couldn't be loaded."); if (cConfig.Pool.CompressorEncoding.IsNotNullOrEmpty()) { serializer.Compressor = CompressorManager.GetByEncodingType(cConfig.Pool.CompressorEncoding); } } else { serializer = SerializerManager.DefaultBinarySerializer; } var ccp = new CacheClientPoolAsync(name, pingDelay, pingDelayOnError, readMode, writeMode, selectionOrder) { Serializer = serializer, ForceAtLeastOneNetworkItemEnabled = forceNetworkItem }; if (cConfig.Pool.Items?.Any() != true) { return(ccp); } var idx = 0; foreach (var pitem in cConfig.Pool.Items) { idx++; if (!pitem.Enabled) { continue; } var objType = pitem.CreateInstance <object>(); switch (objType) { case ITransportClient transport: { var hostParam = pitem.Parameters?.FirstOrDefault(p => p.Key == "Host"); var portParam = pitem.Parameters?.FirstOrDefault(p => p.Key == "Port"); var proxy = await CacheClientProxy.GetClientAsync(transport).ConfigureAwait(false); var cppName = Core.EnvironmentName + "." + Core.MachineName + "." + name + ".Storage(" + transport.GetType().Name + "-" + hostParam?.Value + "-" + portParam?.Value + ")." + idx; ccp.Add(cppName, (IStorageAsync)proxy, pitem.Mode); break; } case StorageBase sto: { var cppName = Core.EnvironmentName + "." + Core.MachineName + "." + name + ".Storage(" + sto.Type + ")." + idx; ccp.Add(cppName, sto); break; } } } return(ccp); }
protected override async Task OnHandlerAsync(ParameterHandlerInfo info) { Core.DebugMode = true; Core.Log.Warning("Starting CACHE TEST"); var cacheService = new TestCacheService(); cacheService.OnStart(null); using (var cachePool = new CacheClientPoolAsync("Pool Test") { Serializer = GlobalSerializer }) { var cacheClient = await CacheClientProxy.GetClientAsync(new DefaultTransportClient("127.0.0.1", 20051, 3, GlobalSerializer)).ConfigureAwait(false); cachePool.Add("localhost:20051", cacheClient, StorageItemMode.ReadAndWrite); await cachePool.SetAsync("expTest", "testData", TimeSpan.FromSeconds(20), new[] { "test" }).ConfigureAwait(false); Console.ReadLine(); var asto = await cachePool.GetAsync("test").ConfigureAwait(false); if (asto is null) { await cachePool.SetAsync("test", "value").ConfigureAwait(false); asto = await cachePool.GetAsync("test").ConfigureAwait(false); } var guid = Guid.NewGuid(); await cachePool.CopyAsync("test", guid.ToString("N")).ConfigureAwait(false); var asto2 = await cachePool.GetAsync(guid.ToString("N")).ConfigureAwait(false); var guid2 = Guid.NewGuid(); await cachePool.CopyAsync("test", guid2.ToString("N")).ConfigureAwait(false); var asto3 = await cachePool.GetAsync(guid2.ToString("N")).ConfigureAwait(false); var guid3 = Guid.NewGuid(); await cachePool.CopyAsync("test", guid3.ToString("N")).ConfigureAwait(false); var asto4 = await cachePool.GetAsync(guid3.ToString("N")).ConfigureAwait(false); Console.ReadLine(); var tagValue1 = await cachePool.GetByTagAsync(new [] { "test" }).ConfigureAwait(false); await Task.Delay(600).ConfigureAwait(false); var tagValue2 = await cachePool.GetByTagAsync(new [] { "test" }).ConfigureAwait(false); await Task.Delay(600).ConfigureAwait(false); await cachePool.SetAsync("expTest2", "testData2", TimeSpan.FromMinutes(1), new[] { "test" }).ConfigureAwait(false); await Task.Delay(600).ConfigureAwait(false); var tagValue3 = await cachePool.GetByTagAsync(new[] { "test" }).ConfigureAwait(false); await Task.Delay(600).ConfigureAwait(false); var tagValue4 = await cachePool.SetMultiAsync(new[] { "testa", "testb" }, "Hola mundo", TimeSpan.FromSeconds(10)).ConfigureAwait(false); await Task.Delay(600).ConfigureAwait(false); var response = await cachePool.ExecuteExtensionAsync("SampleExtension", "Echo", new object[] { "value1", "value2" }); Core.Log.Warning("Extension Response: {0}", response); Console.ReadLine(); try { for (var i = 0; i < 15; i++) { await cachePool.GetKeysAsync().ConfigureAwait(false); } Console.ReadLine(); for (var i = 0; i < 100; i++) { var key = "test-" + i; await cachePool.GetAsync(key).ConfigureAwait(false); await cachePool.SetAsync(key, "bla bla bla bla bla").ConfigureAwait(false); } Console.ReadLine(); for (var i = 0; i < 100; i++) { var key = "test-" + i; await cachePool.GetAsync(key).ConfigureAwait(false); await cachePool.SetAsync(key, "bla bla bla bla bla").ConfigureAwait(false); } } catch (Exception ex) { Core.Log.Write(ex); } } cacheService.OnStop(); }