static async Task TestLockAsyncThread(RedisConnection redisConnection, string name = "test") { try { var rnd = new Random((int)DateTime.Now.Ticks); var db = redisConnection.GetDatabase(); var lockAcquisition = new RedisDLM(); for (var i = 0; i < 5; i++) { using (var dbLock = await lockAcquisition.AcquireLockAsync(db, "testkey")) { if (dbLock.Locked) { var lockFor = rnd.Next(1, 2); Console.WriteLine($"{name}: Lock obtained for: {lockFor} seconds"); Thread.Sleep(TimeSpan.FromSeconds(lockFor)); // Thread.Sleep(TimeSpan.FromSeconds(20)); } else { Console.WriteLine($"{name} Couldn't obtained lock"); } } } } catch (Exception ex) { Console.WriteLine(ex); } }
static async Task TestCacheAsync(RedisConnection redisConnection, string threadName) { try { var rnd = new Random((int)DateTime.Now.Ticks); var json = File.ReadAllText($"{AppDomain.CurrentDomain.BaseDirectory}sample.json"); var lockAcquisition = new RedisDLM(); var cache = new RedisCache(redisConnection, lockAcquisition); while (!TerminateTestCacheAsync) { var data = await cache.GetAsync("data1", async (name, args) => { Console.WriteLine($"{threadName} locked and creating data **************"); var myData = new MySerializeData2 { Data = json, List = new List <TestClass>(), }; for (var i = 0; i < 10; i++) { myData.List.Add(new TestClass { Id = i, Name = $"Ross: {i}" }); } return(await Task.FromResult(new CacheDataInfo <MySerializeData2> { NewCacheData = myData, KeepAlive = TimeSpan.FromSeconds(5) })); }); Console.WriteLine($"{threadName} received data"); Thread.Sleep(rnd.Next(500)); } Console.WriteLine($"{threadName} terminated successfully!"); } catch (Exception ex) { Console.WriteLine(ex); } }
static public async Task RunAsync() { // string connectionString = "127.0.0.1:30001,127.0.0.1:30002,127.0.0.1:30003,127.0.0.1:30004,127.0.0.1:30005,127.0.0.1:30006"; string connectionStrings = "localhost:6379,abortConnect=False"; try { // var redisNamespace = "dev:hubster:identity"; using (var redisConnection = new RedisConnection(connectionStrings)) { var lockAcquisition = new RedisDLM(); // var container = new HashRedisCacheContainer(redisConnection, $"{redisNamespace}:cache"); var container = new StringRedisCacheContainer(redisConnection, $"dev:hubster:identity:cache"); var cache = new RedisCache(redisConnection, lockAcquisition, container); var t1 = await cache.GetAsync("test1", (name, args) => { return(Task.FromResult(new CacheDataInfo <string> { NewCacheData = "Ross1", KeepAlive = TimeSpan.FromSeconds(60) })); }); //var d = new RedisDictionary<string, Crap<TestClass>>(redisConnection.GetDatabase(), $"{redisNamespace}:dick", new JsonRedisSerializer()); // new MsgPackRedisSerializer()); //var t1 = d.GetOrAdd("test", name => //{ // return new Crap<TestClass> { Value = new TestClass { Id = 123, Name = "Hello" } }; //}); //t1 = await d.GetAsync("test"); // var d = new RedisDictionary<string, Crap>(redisConnection.GetDatabase(), "test", new JsonRedisSerializer()); // new MsgPackRedisSerializer()); // var dtest = new Dictionary<int, string>(); //dtest.Add(1, "Ross"); //dtest.Add(2, "Tammy"); // var v1 = await GetAsync(d, "age", name => 2); //await SetAsync(d, "age", (v1 + 2)); //v1 = await GetAsync(d, "age", name => 3); //var v2 = await GetAsync(d, "dtest", name => dtest); //v2 = await GetAsync(d, "dtest", name => dtest); ////var v1 = await GetAsync(d, "age", name => (decimal)2); ////await SetAsync(d, "age", (v1 + 2)); ////v1 = await GetAsync(d, "age", name => (decimal)3); //return; //var lockAcquisition = new RedisCacheKeyLockAcquisition(); //var cacheContainer = new StringRedisCacheContainer(redisConnection, "RedisCache"); // new HashRedisCacheContainer(redisConnection); //var cacheSeriizer = new MsgPackRedisSerializer(); // new JsonRedisSerializer(); //var cache = new RedisCache(redisConnection, lockAcquisition, cacheContainer, cacheSeriizer); //var xxx = await cache.GetAsync("test", async (name, args) => //{ // var app = new AppEntity // { // AppId = Guid.NewGuid(), // CreatedDateTime = DateTime.Now, // ModifiedDateTime = DateTime.Now, // Description = "Hello", // Enabled = true, // Name = "test", // TenantId = Guid.Empty, // }; // await Task.Delay(0); // return new CacheDataInfo<AppEntity> // { // KeepAlive = TimeSpan.FromHours(1), // NewCacheData = app, //"Hello, Ross" // }; // // return (CacheDataInfo<string>)null; //}); //await cache.RemoveAsync("test"); //CacheThreadTest(redisConnection); //LockThreadTest(redisConnection); //SerializerTests(redisConnection); //Test4(redisConnection); //Test1(redisConnection); //Test3(redisConnection); } await Task.CompletedTask; } catch (Exception ex) { Console.WriteLine(ex); } }