Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Initial Commands Added - looking for feedback #161

Merged
merged 2 commits into from
Jun 11, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/ServiceStack.Redis/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,10 @@ public static class Commands
//public readonly static byte[] Exists = "EXISTS".ToUtf8Bytes();
public readonly static byte[] Flush = "FLUSH".ToUtf8Bytes();
public readonly static byte[] Slowlog = "SLOWLOG".ToUtf8Bytes();

public readonly static byte[] ExpireInSeconds = "EX".ToUtf8Bytes();
public readonly static byte[] ExpireInMilliseconds = "PX".ToUtf8Bytes();
public readonly static byte[] SetIfKeyDoesNotExist = "NX".ToUtf8Bytes();
public readonly static byte[] SetIfKeyExists = "XX".ToUtf8Bytes();
}
}
41 changes: 41 additions & 0 deletions src/ServiceStack.Redis/RedisClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,47 @@ public void SetEntry(string key, string value)
base.Set(key, bytesValue);
}

public void SetEntry(string key, string value, TimeSpan expireIn)
{
var bytesValue = value != null ? value.ToUtf8Bytes() : null;

if (expireIn.Milliseconds == 0)
{
base.Set(key, bytesValue, (int)expireIn.TotalSeconds);
}
else {
base.Set(key, bytesValue, null, (long)expireIn.TotalMilliseconds);
}
}

public void SetEntryIfExists(string key, string value, TimeSpan expireIn)
{
var bytesValue = value != null ? value.ToUtf8Bytes() : null;

if (expireIn.Milliseconds == 0)
{
base.Set(key, bytesValue, (int)expireIn.TotalSeconds, null, true);
}
else
{
base.Set(key, bytesValue, null, (long)expireIn.TotalMilliseconds, true);
}
}

public void SetEntryIfNotExists(string key, string value, TimeSpan expireIn)
{
var bytesValue = value != null ? value.ToUtf8Bytes() : null;

if (expireIn.Milliseconds == 0)
{
base.Set(key, bytesValue, (int)expireIn.TotalSeconds, null, false);
}
else
{
base.Set(key, bytesValue, null, (long)expireIn.TotalMilliseconds, false);
}
}

public void ChangeDb(long db)
{
Db = db;
Expand Down
25 changes: 23 additions & 2 deletions src/ServiceStack.Redis/RedisNativeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public long StrLen(string key)
return SendExpectLong(Commands.StrLen, key.ToUtf8Bytes());
}

public void Set(string key, byte[] value)
public void Set(string key, byte[] value, int? expirySeconds = null, long? expiryMs = null, bool? exists = null)
{
if (key == null)
throw new ArgumentNullException("key");
Expand All @@ -324,7 +324,28 @@ public void Set(string key, byte[] value)
if (value.Length > OneGb)
throw new ArgumentException("value exceeds 1G", "value");

SendExpectSuccess(Commands.Set, key.ToUtf8Bytes(), value);
if (!expirySeconds.HasValue && !exists.HasValue)
{
SendExpectSuccess(Commands.Set, key.ToUtf8Bytes(), value);
}
else if (!exists.HasValue && expiryMs.HasValue)
{
SendExpectSuccess(Commands.Set, key.ToUtf8Bytes(), value, Commands.ExpireInMilliseconds, expiryMs.Value.ToUtf8Bytes());
}
else if (!exists.HasValue && expirySeconds.HasValue)
{
SendExpectSuccess(Commands.Set, key.ToUtf8Bytes(), value, Commands.ExpireInSeconds, expirySeconds.Value.ToUtf8Bytes());
}
else if (exists.HasValue && !expirySeconds.HasValue && !expiryMs.HasValue)
{
SendExpectSuccess(Commands.Set, key.ToUtf8Bytes(), value, exists.Value ? Commands.SetIfKeyExists : Commands.SetIfKeyDoesNotExist);
}
else if (exists.HasValue && expirySeconds.HasValue) {
SendExpectSuccess(Commands.Set, key.ToUtf8Bytes(), value, exists.Value ? Commands.SetIfKeyExists : Commands.SetIfKeyDoesNotExist, Commands.ExpireInSeconds, expirySeconds.Value.ToUtf8Bytes());
}
else if (exists.HasValue && expiryMs.HasValue) {
SendExpectSuccess(Commands.Set, key.ToUtf8Bytes(), value, exists.Value ? Commands.SetIfKeyExists : Commands.SetIfKeyDoesNotExist, Commands.ExpireInMilliseconds, expiryMs.Value.ToUtf8Bytes());
}
}

public void SetEx(string key, int expireInSeconds, byte[] value)
Expand Down