/// <summary> /// 从缓存中删除具有指定键的值 /// </summary> /// <param name="key">键名</param> public virtual void Remove(string key) { if (IsSet(key)) { Redis.Remove(key); } }
public void Can_Set_and_Get_key_with_space() { Redis.SetEntry("key with space", Value); var valueBytes = Redis.Get("key with space"); var valueString = GetString(valueBytes); Redis.Remove("key with space"); Assert.That(valueString, Is.EqualTo(Value)); }
public void Can_Set_and_Get_string() { Redis.SetValue("key", Value); var valueBytes = Redis.Get("key"); var valueString = GetString(valueBytes); Redis.Remove("key"); Assert.That(valueString, Is.EqualTo(Value)); }
public void Can_Set_Expire_Seconds_if_not_exists() { Redis.SetEntryIfNotExists("key", "val", expireIn: TimeSpan.FromMilliseconds(1000)); Assert.That(Redis.ContainsKey("key"), Is.True); Thread.Sleep(2000); Assert.That(Redis.ContainsKey("key"), Is.False); Redis.Remove("key"); Redis.SetEntryIfNotExists("key", "val", expireIn: TimeSpan.FromMilliseconds(1000)); Assert.That(Redis.ContainsKey("key"), Is.True); }
public void Can_AcquireLock_TimeOut() { // guid here is to prevent competition between concurrent runtime tests var key = PrefixedKey("AcquireLockKeyTimeOut:" + Guid.NewGuid()); var lockKey = PrefixedKey("Can_AcquireLock_TimeOut:" + Guid.NewGuid()); Redis.IncrementValue(key); //1 var acquiredLock = Redis.AcquireLock(lockKey); var waitFor = TimeSpan.FromMilliseconds(1000); var now = DateTime.Now; try { using (var client = new RedisClient(TestConfig.SingleHost)) { using (client.AcquireLock(lockKey, waitFor)) { Redis.IncrementValue(key); //2 } } } catch (TimeoutException) { var val = Redis.Get <int>(key); Assert.That(val, Is.EqualTo(1)); var timeTaken = DateTime.Now - now; Assert.That(timeTaken.TotalMilliseconds > waitFor.TotalMilliseconds, Is.True); Assert.That(timeTaken.TotalMilliseconds < waitFor.TotalMilliseconds + 3000, Is.True); return; } finally { Redis.Remove(key); Redis.Remove(lockKey); } Assert.Fail("should have Timed out"); }
public void VerifyPerformanceImprovement() { int asyncTimer, sync, op = 0, asyncFaF, syncFaF; using (var muxer= Config.GetUnsecuredConnection()) { // do these outside the timings, just to ensure the core methods are JITted etc for (int db = 0; db < 5; db++) { muxer.GetDatabase(db).KeyDeleteAsync("perftest"); } var timer = Stopwatch.StartNew(); for (int i = 0; i < 100; i++) { // want to test multiplex scenario; test each db, but to make it fair we'll // do in batches of 10 on each for (int db = 0; db < 5; db++) { var conn = muxer.GetDatabase(db); for (int j = 0; j < 10; j++) conn.StringIncrementAsync("perftest"); } } asyncFaF = (int)timer.ElapsedMilliseconds; Task<RedisValue>[] final = new Task<RedisValue>[5]; for (int db = 0; db < 5; db++) final[db] = muxer.GetDatabase(db).StringGetAsync("perftest"); muxer.WaitAll(final); timer.Stop(); asyncTimer = (int)timer.ElapsedMilliseconds; Console.WriteLine("async to completion (local): {0}ms", timer.ElapsedMilliseconds); for (int db = 0; db < 5; db++) Assert.AreEqual(1000, (long)final[db].Result, "async, db:" + db); } using (var conn = new Redis(Config.LocalHost, 6379)) { // do these outside the timings, just to ensure the core methods are JITted etc for (int db = 0; db < 5; db++) { conn.Db = db; conn.Remove("perftest"); } var timer = Stopwatch.StartNew(); for (int i = 0; i < 100; i++) { // want to test multiplex scenario; test each db, but to make it fair we'll // do in batches of 10 on each for (int db = 0; db < 5; db++) { conn.Db = db; op++; for (int j = 0; j < 10; j++) { conn.Increment("perftest"); op++; } } } syncFaF = (int)timer.ElapsedMilliseconds; string[] final = new string[5]; for (int db = 0; db < 5; db++) { conn.Db = db; final[db] = Encoding.ASCII.GetString(conn.Get("perftest")); } timer.Stop(); sync = (int)timer.ElapsedMilliseconds; Console.WriteLine("sync to completion (local): {0}ms", timer.ElapsedMilliseconds); for (int db = 0; db < 5; db++) Assert.AreEqual("1000", final[db], "async, db:" + db); } int effectiveAsync = ((10 * asyncTimer) + 3) / 10; int effectiveSync = ((10 * sync) + (op * 3)) / 10; Console.WriteLine("async to completion with assumed 0.3ms LAN latency: " + effectiveAsync); Console.WriteLine("sync to completion with assumed 0.3ms LAN latency: " + effectiveSync); Console.WriteLine("fire-and-forget: {0}ms sync vs {1}ms async ", syncFaF, asyncFaF); Assert.Less(effectiveAsync, effectiveSync, "Everything"); Assert.Less(asyncFaF, syncFaF, "Fire and Forget"); }
public void VerifyPerformanceImprovement() { int asyncTimer, sync, op = 0, asyncFaF, syncFaF; using (var conn = Config.GetUnsecuredConnection()) { // do these outside the timings, just to ensure the core methods are JITted etc for (int db = 0; db < 5; db++) { conn.Keys.Remove(db, "perftest"); } var timer = Stopwatch.StartNew(); for (int i = 0; i < 100; i++) { // want to test multiplex scenario; test each db, but to make it fair we'll // do in batches of 10 on each for (int db = 0; db < 5; db++) { for (int j = 0; j < 10; j++) { conn.Strings.Increment(db, "perftest"); } } } asyncFaF = (int)timer.ElapsedMilliseconds; Task <string>[] final = new Task <string> [5]; for (int db = 0; db < 5; db++) { final[db] = conn.Strings.GetString(db, "perftest"); } conn.WaitAll(final); timer.Stop(); asyncTimer = (int)timer.ElapsedMilliseconds; Console.WriteLine("async to completion (local): {0}ms", timer.ElapsedMilliseconds); for (int db = 0; db < 5; db++) { Assert.AreEqual("1000", final[db].Result, "async, db:" + db); } } using (var conn = new Redis("127.0.0.1", 6379)) { // do these outside the timings, just to ensure the core methods are JITted etc for (int db = 0; db < 5; db++) { conn.Db = db; conn.Remove("perftest"); } var timer = Stopwatch.StartNew(); for (int i = 0; i < 100; i++) { // want to test multiplex scenario; test each db, but to make it fair we'll // do in batches of 10 on each for (int db = 0; db < 5; db++) { conn.Db = db; op++; for (int j = 0; j < 10; j++) { conn.Increment("perftest"); op++; } } } syncFaF = (int)timer.ElapsedMilliseconds; string[] final = new string[5]; for (int db = 0; db < 5; db++) { conn.Db = db; final[db] = Encoding.ASCII.GetString(conn.Get("perftest")); } timer.Stop(); sync = (int)timer.ElapsedMilliseconds; Console.WriteLine("sync to completion (local): {0}ms", timer.ElapsedMilliseconds); for (int db = 0; db < 5; db++) { Assert.AreEqual("1000", final[db], "async, db:" + db); } } int effectiveAsync = ((10 * asyncTimer) + 3) / 10; int effectiveSync = ((10 * sync) + (op * 3)) / 10; Console.WriteLine("async to completion with assumed 0.3ms LAN latency: " + effectiveAsync); Console.WriteLine("sync to completion with assumed 0.3ms LAN latency: " + effectiveSync); Console.WriteLine("fire-and-forget: {0}ms sync vs {1}ms async ", syncFaF, asyncFaF); Assert.Less(effectiveAsync, effectiveSync, "Everything"); Assert.Less(asyncFaF, syncFaF, "Fire and Forget"); }
/// <summary> /// 移除整个hash /// </summary> public bool Remove(string key) { return(Redis.Remove(key)); }
public bool Remove(string hashId) { return(Redis.Remove(hashId)); }
public async Task <string> Stop(string param = "") { try { var taskPlands = await _sqliteFSql.Select <TaskPlan>().ToListAsync(); if (!string.IsNullOrEmpty(param)) { var tk = await _sqliteFSql.Select <TaskPlan>().Where(o => o.GUID == param).FirstAsync(); if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { if (rds.ContainsKey(param)) { if (await _scheduler.DeleteJob(rds.Get <JobKey>(param))) { rds.Remove(param); return($"定时任务({ tk.Name})已结束"); } } else { return("还未开始,怎谈得上关闭呢?"); } } else { if (tk.Status == "1" && jobKeys.ContainsKey(param)) { if (await _scheduler.DeleteJob(jobKeys.GetValueOrDefault(param))) { jobKeys.Remove(param); tk.Status = "0"; _sqliteFSql.Update <TaskPlan>() .Set(a => a.Status, tk.Status) .Where(a => a.GUID == tk.GUID) .ExecuteAffrows(); return(await Task.FromResult($"定时任务({ tk.Name})已结束")); } } else { tk.Status = "0"; _sqliteFSql.Update <TaskPlan>() .Set(a => a.Status, tk.Status) .Where(a => a.GUID == tk.GUID) .ExecuteAffrows(); return("还未开始,怎谈得上关闭呢?"); } } } await _scheduler.Shutdown(); if (jobKeys.Count == 0 && taskPlands.Where(o => o.Status == "1").Count() == 0) { return("还未开始,怎谈得上关闭呢?"); } for (int i = 0; i < taskPlands.Count; i++) { if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { rds.Remove(taskPlands[i].GUID); } else { jobKeys.Remove(taskPlands[i].GUID); taskPlands[i].Status = "0"; _sqliteFSql.Update <TaskPlan>() .Set(a => a.Status, taskPlands[i].Status) .Where(a => a.GUID == taskPlands[i].GUID) .ExecuteAffrows(); } } return("定时任务已全部结束"); } catch (Exception ex) { return(ex.Message); } }