Permalink
Browse files

Use more precise redis-server operations where supported

  • Loading branch information...
1 parent 1373bae commit f6401739931c5b7e4a01fd66342fa68444d6ff32 @mythz mythz committed Mar 3, 2014
@@ -283,7 +283,7 @@ public string GetRandomKey()
public bool ExpireEntryIn(string key, TimeSpan expireIn)
{
- return client.Expire(key, (int)expireIn.TotalSeconds);
+ return client.ExpireEntryIn(key, expireIn);
}
public bool ExpireEntryAt(string key, DateTime expireAt)
@@ -294,7 +294,7 @@ public bool ExpireEntryAt(string key, DateTime expireAt)
public bool ExpireIn(object id, TimeSpan expireIn)
{
var key = client.UrnKey<T>(id);
- return client.Expire(key, (int)expireIn.TotalSeconds);
+ return client.ExpireEntryIn(key, expireIn);
}
public bool ExpireAt(object id, DateTime expireAt)
@@ -101,7 +101,14 @@ public bool Set<T>(string key, T value, TimeSpan expiresIn)
var bytesValue = value as byte[];
if (bytesValue != null)
{
- base.SetEx(key, (int)expiresIn.TotalSeconds, bytesValue);
+ if (ServerVersionNumber >= 2600)
+ {
+ base.PSetEx(key, (long)expiresIn.TotalMilliseconds, bytesValue);
+ }
+ else
+ {
+ base.SetEx(key, (int)expiresIn.TotalSeconds, bytesValue);
+ }
return true;
}
@@ -136,13 +136,17 @@ public void SetEntry(string key, string value, TimeSpan expireIn)
? value.ToUtf8Bytes()
: null;
- SetEx(key, (int)expireIn.TotalSeconds, bytesValue);
-
- //New in 2.6.x - TODO change when 2.6 is most popular
- //if (expireIn.Milliseconds > 0)
- // base.Set(key, bytesValue, 0, (long)expireIn.TotalMilliseconds);
- //else
- // base.Set(key, bytesValue, (int)expireIn.TotalSeconds, 0);
+ if (ServerVersionNumber >= 2600)
+ {
+ if (expireIn.Milliseconds > 0)
+ base.Set(key, bytesValue, 0, (long)expireIn.TotalMilliseconds);
+ else
+ base.Set(key, bytesValue, (int)expireIn.TotalSeconds, 0);
+ }
+ else
+ {
+ SetEx(key, (int)expireIn.TotalSeconds, bytesValue);
+ }
}
public void SetEntryIfExists(string key, string value, TimeSpan expireIn)
@@ -309,12 +313,27 @@ public string GetRandomKey()
public bool ExpireEntryIn(string key, TimeSpan expireIn)
{
+ if (ServerVersionNumber >= 2600)
+ {
+ if (expireIn.Milliseconds > 0)
+ {
+ return PExpire(key, (long)expireIn.TotalMilliseconds);
+ }
+ }
+
return Expire(key, (int)expireIn.TotalSeconds);
}
public bool ExpireEntryAt(string key, DateTime expireAt)
{
- return ExpireAt(key, ConvertToServerDate(expireAt).ToUnixTime());
+ if (ServerVersionNumber >= 2600)
+ {
+ return PExpireAt(key, ConvertToServerDate(expireAt).ToUnixTimeMs());
+ }
+ else
+ {
+ return ExpireAt(key, ConvertToServerDate(expireAt).ToUnixTime());
+ }
}
public TimeSpan GetTimeToLive(string key)
@@ -162,7 +162,7 @@ public DateTime LastSave
get
{
var t = SendExpectLong(Commands.LastSave);
- return DateTimeExtensions.FromUnixTime(t);
+ return t.FromUnixTime();
}
}
@@ -161,15 +161,25 @@ public void Can_RenameKey()
Assert.That(Redis.ContainsKey("newkey"), Is.True);
}
- [Test]
- public void Can_Expire()
- {
- Redis.SetEntry("key", "val");
- Redis.Expire("key", 1);
- Assert.That(Redis.ContainsKey("key"), Is.True);
- Thread.Sleep(2000);
- Assert.That(Redis.ContainsKey("key"), Is.False);
- }
+ [Test]
+ public void Can_Expire()
+ {
+ Redis.SetEntry("key", "val");
+ Redis.Expire("key", 1);
+ Assert.That(Redis.ContainsKey("key"), Is.True);
+ Thread.Sleep(2000);
+ Assert.That(Redis.ContainsKey("key"), Is.False);
+ }
+
+ [Test]
+ public void Can_Expire_Ms()
+ {
+ Redis.SetEntry("key", "val");
+ Redis.ExpireEntryIn("key", TimeSpan.FromMilliseconds(100));
+ Assert.That(Redis.ContainsKey("key"), Is.True);
+ Thread.Sleep(500);
+ Assert.That(Redis.ContainsKey("key"), Is.False);
+ }
[Test]
public void Can_ExpireAt()

0 comments on commit f640173

Please sign in to comment.