public void Expires() { const string key = "key"; var target = new RedisModel(); target.Set(key, "value"); var expires = DateTime.Now; target.Expire(key, expires); Thread.Sleep(TimeSpan.FromMilliseconds(1)); var keys = target.GetExpiredKeys(); Assert.IsTrue(keys.Single() == key); var expected = target.Expires(key); Assert.IsTrue(expected.HasValue); Assert.AreEqual(expected.Value, expires); }
public void ExpiresMultiple() { var target = new RedisModel(); var expires = DateTime.Now; //add 5 keys with expiring NOW var range = Enumerable.Range(1, 5).Select(n => n.ToString()).ToArray(); foreach (var key in range) { target.Set(key, key); target.Expire(key, expires); } //wait a bit and they should all be reported as expired Thread.Sleep(TimeSpan.FromMilliseconds(10)); var expiredKeys = target.GetExpiredKeys(); Assert.IsTrue(new HashSet <string>(expiredKeys).SetEquals(range)); //check them individually foreach (var key in range) { var expected = target.Expires(key); Assert.IsTrue(expected.HasValue); Assert.AreEqual(expected.Value, expires); } //un-expire the first one and check again target.Persist(range[0]); range = range.Skip(1).ToArray(); expiredKeys = target.GetExpiredKeys(); Assert.IsTrue(new HashSet <string>(expiredKeys).SetEquals(range)); //purge and there should be no expired keys target.PurgeExpired(); expiredKeys = target.GetExpiredKeys(); Assert.AreEqual(expiredKeys.Length, 0); //there should now be a single key in the store Assert.AreEqual(target.KeyCount(), 1); Assert.AreEqual("1", target.Get("1")); }
public void ExpiresMultiple() { var target = new RedisModel(); var expires = DateTime.Now; //add 5 keys with expiring NOW var range = Enumerable.Range(1, 5).Select(n => n.ToString()).ToArray(); foreach (var key in range) { target.Set(key, key); target.Expire(key, expires); } //wait a bit and they should all be reported as expired Thread.Sleep(TimeSpan.FromMilliseconds(10)); var expiredKeys = target.GetExpiredKeys(); Assert.IsTrue(new HashSet<string>(expiredKeys).SetEquals(range)); //check them individually foreach (var key in range) { var expected = target.Expires(key); Assert.IsTrue(expected.HasValue); Assert.AreEqual(expected.Value, expires); } //un-expire the first one and check again target.Persist(range[0]); range = range.Skip(1).ToArray(); expiredKeys = target.GetExpiredKeys(); Assert.IsTrue(new HashSet<string>(expiredKeys).SetEquals(range)); //purge and there should be no expired keys target.PurgeExpired(); expiredKeys = target.GetExpiredKeys(); Assert.AreEqual(expiredKeys.Length, 0); //there should now be a single key in the store Assert.AreEqual(target.KeyCount(), 1); Assert.AreEqual("1", target.Get("1")); }