Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added string methods

  • Loading branch information...
commit 39dd06abef208c1eac4d114c5341d53852b94aa9 1 parent 6253a26
Karl Seguin authored
View
16 Metsys.Redis.Tests/Commands/GetAndSetTests.cs
@@ -31,6 +31,22 @@ public void GetSetTest()
Assert.AreEqual(44, Redis.Get<int>("key1"));
}
+ [Test]
+ public void SetNxDoesntSetTheValueIfTheKeyAlreadyExists()
+ {
+ Assert.AreEqual(true, Redis.SetNx("key1", "old"));
+ Assert.AreEqual(false, Redis.SetNx("key1", "new"));
+ Assert.AreEqual("old", Redis.Get<string>("key1"));
+ }
+
+ [Test]
+ public void SetExSetsAKeyWithAnExpiry()
+ {
+ Redis.SetEx("key1", 5000, "value");
+ Assert.AreEqual(5000, Redis.Ttl("key1"));
+ Assert.AreEqual("value", Redis.Get<string>("key1"));
+ }
+
private void AssertEqual<T>(T i)
{
Redis.Set("get:set:test:1", i);
View
16 Metsys.Redis.Tests/Commands/MGetTests.cs
@@ -0,0 +1,16 @@
+using NUnit.Framework;
+
+namespace Metsys.Redis.Tests.Commands
+{
+ public class MGetTests : BaseCommandTests
+ {
+ [Test]
+ public void GetsMultipleValues()
+ {
+ Redis.Set("key1", "v1");
+ Redis.Set("key2", "v2");
+ Redis.Set("key3", "v3");
+ Assert.AreEqual(new[] {"v1", "v3", null}, Redis.MGet<string>("key1", "key3", "key4"));
+ }
+ }
+}
View
33 Metsys.Redis.Tests/Commands/MSetTests.cs
@@ -0,0 +1,33 @@
+using System.Collections.Generic;
+using NUnit.Framework;
+
+namespace Metsys.Redis.Tests.Commands
+{
+ public class MSetTests : BaseCommandTests
+ {
+ [Test]
+ public void SetsMultipleKeys()
+ {
+ Redis.MSet(new Dictionary<string, object>{{"key1", 9000}, {"key2", "it's over"}});
+ Assert.AreEqual(9000, Redis.Get<int>("key1"));
+ Assert.AreEqual("it's over", Redis.Get<string>("key2"));
+ }
+
+ [Test]
+ public void DoesNotSetAnyKeyIfOneExistsUsingNx()
+ {
+ Redis.Set("key1", "existing");
+ Assert.AreEqual(false, Redis.MSetNx(new Dictionary<string, object> { { "key1", 9000 }, { "key2", "it's over" } }));
+ Assert.AreEqual("existing", Redis.Get<string>("key1"));
+ Assert.AreEqual(null, Redis.Get<string>("key2"));
+ }
+
+ [Test]
+ public void SetsMultipleKeysIfNoneExistUsingNx()
+ {
+ Redis.MSetNx(new Dictionary<string, object> { { "key1", 9000 }, { "key2", "it's over" } });
+ Assert.AreEqual(9000, Redis.Get<int>("key1"));
+ Assert.AreEqual("it's over", Redis.Get<string>("key2"));
+ }
+ }
+}
View
15 Metsys.Redis.Tests/Commands/SetRangeTests.cs
@@ -0,0 +1,15 @@
+using NUnit.Framework;
+
+namespace Metsys.Redis.Tests.Commands
+{
+ public class SetRangeTests : BaseCommandTests
+ {
+ [Test]
+ public void SetsTheRange()
+ {
+ Redis.Set("key1", "it's over 9");
+ Assert.AreEqual(14, Redis.SetRange("key1", 10, "9000"));
+ Assert.AreEqual("it's over 9000", Redis.Get<string>("key1"));
+ }
+ }
+}
View
15 Metsys.Redis.Tests/Commands/StrLenTests.cs
@@ -0,0 +1,15 @@
+using NUnit.Framework;
+
+namespace Metsys.Redis.Tests.Commands
+{
+ public class StrLenTests : BaseCommandTests
+ {
+ [Test]
+ public void ReturnsTheLenghtOfTheValue()
+ {
+ Assert.AreEqual(0, Redis.StrLen("invalid"));
+ Redis.Set("key1", "this is boring");
+ Assert.AreEqual(14, Redis.StrLen("key1"));
+ }
+ }
+}
View
4 Metsys.Redis.Tests/Metsys.Redis.Tests.csproj
@@ -50,12 +50,16 @@
<Compile Include="Commands\GetRangeTests.cs" />
<Compile Include="Commands\IncrTests.cs" />
<Compile Include="Commands\KeysTests.cs" />
+ <Compile Include="Commands\MGetTests.cs" />
<Compile Include="Commands\MoveTests.cs" />
+ <Compile Include="Commands\MSetTests.cs" />
<Compile Include="Commands\PersistTests.cs" />
<Compile Include="Commands\RandomKeyTests.cs" />
<Compile Include="Commands\RenameTests.cs" />
<Compile Include="Commands\SelectTests.cs" />
<Compile Include="Commands\GetAndSetTests.cs" />
+ <Compile Include="Commands\SetRangeTests.cs" />
+ <Compile Include="Commands\StrLenTests.cs" />
<Compile Include="Commands\TtlAndExpireTests.cs" />
<Compile Include="Commands\TypeTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
View
7 Metsys.Redis/Commands.cs
@@ -18,7 +18,10 @@ public class Commands
public static readonly byte[] Incr = Helper.GetBytes("INCR");
public static readonly byte[] IncrBy = Helper.GetBytes("INCRBY");
public static readonly byte[] Keys = Helper.GetBytes("KEYS");
+ public static readonly byte[] MGet = Helper.GetBytes("MGET");
public static readonly byte[] Move = Helper.GetBytes("MOVE");
+ public static readonly byte[] MSet = Helper.GetBytes("MSET");
+ public static readonly byte[] MSetNx = Helper.GetBytes("MSETNX");
public static readonly byte[] Persist = Helper.GetBytes("PERSIST");
public static readonly byte[] RandomKey = Helper.GetBytes("RANDOMKEY");
public static readonly byte[] Rename = Helper.GetBytes("RENAME");
@@ -26,6 +29,10 @@ public class Commands
public static readonly byte[] Select = Helper.GetBytes("SELECT");
public static readonly byte[] Set = Helper.GetBytes("SET");
public static readonly byte[] SetBit = Helper.GetBytes("SETBIT");
+ public static readonly byte[] SetEx = Helper.GetBytes("SETEX");
+ public static readonly byte[] SetNx = Helper.GetBytes("SETNX");
+ public static readonly byte[] SetRange = Helper.GetBytes("SETRANGE");
+ public static readonly byte[] StrLen = Helper.GetBytes("STRLEN");
public static readonly byte[] Ttl = Helper.GetBytes("TTL");
public static readonly byte[] Type = Helper.GetBytes("TYPE");
}
View
8 Metsys.Redis/IRedis.cs
@@ -1,4 +1,5 @@
using System;
+using System.Collections.Generic;
namespace Metsys.Redis
{
@@ -20,7 +21,10 @@ public interface IRedis : IDisposable
long Incr(string key);
long IncrBy(string key, int value);
string[] Keys(string pattern = "*");
+ T[] MGet<T>(params string[] keys);
bool Move(string key, int database);
+ void MSet(ICollection<KeyValuePair<string, object>> keyAndValues);
+ bool MSetNx(ICollection<KeyValuePair<string, object>> keyAndValues);
bool Persist(string key);
string RandomKey();
void Rename(string key, string newName);
@@ -28,6 +32,10 @@ public interface IRedis : IDisposable
void Select(int database);
void Set(string key, object value);
bool SetBit(string key, int offset, bool bit);
+ void SetEx(string key, int seconds, object value);
+ bool SetNx(string key, object value);
+ int SetRange(string key, int offset, object value);
+ int StrLen(string key);
long Ttl(string key);
string Type(string key);
}
View
49 Metsys.Redis/Redis.cs
@@ -1,4 +1,5 @@
using System;
+using System.Collections.Generic;
using System.IO;
namespace Metsys.Redis
@@ -98,11 +99,39 @@ public string[] Keys(string pattern)
return Send(Writer.Serialize(Commands.Keys, _dynamicBuffer, pattern), Reader.MultiBulk<string>);
}
+ public T[] MGet<T>(params string[] keys)
+ {
+ return Send(Writer.Serialize(Commands.MGet, _dynamicBuffer, keys), Reader.MultiBulk<T>);
+ }
+
public bool Move(string key, int database)
{
return Send(Writer.Serialize(Commands.Move, _dynamicBuffer, key, database), Reader.Bool);
}
+ public void MSet(ICollection<KeyValuePair<string, object>> keyAndValues)
+ {
+ Send(Writer.Serialize(Commands.MSet, _dynamicBuffer, DictionaryToValueArray(keyAndValues)), Reader.Status);
+ }
+
+ public bool MSetNx(ICollection<KeyValuePair<string, object>> keyAndValues)
+ {
+ return Send(Writer.Serialize(Commands.MSetNx, _dynamicBuffer, DictionaryToValueArray(keyAndValues)), Reader.Bool);
+ }
+
+ private static object[] DictionaryToValueArray(ICollection<KeyValuePair<string, object>> keyAndValues)
+ {
+ var values = new object[keyAndValues.Count * 2];
+ var i = 0;
+ foreach (var kvp in keyAndValues)
+ {
+ values[i++] = kvp.Key;
+ values[i++] = kvp.Value;
+ }
+ return values;
+ }
+
+
public bool Persist(string key)
{
return Send(Writer.Serialize(Commands.Persist, _dynamicBuffer, key), Reader.Bool);
@@ -138,6 +167,26 @@ public bool SetBit(string key, int offset, bool bit)
return Send(Writer.Serialize(Commands.SetBit, _dynamicBuffer, key, offset, bit ? 1 : 0), Reader.Bool);
}
+ public void SetEx(string key, int seconds, object value)
+ {
+ Send(Writer.Serialize(Commands.SetEx, _dynamicBuffer, key, seconds, value), Reader.Status);
+ }
+
+ public bool SetNx(string key, object value)
+ {
+ return Send(Writer.Serialize(Commands.SetNx, _dynamicBuffer, key, value), Reader.Bool);
+ }
+
+ public int SetRange(string key, int offset, object value)
+ {
+ return (int) Send(Writer.Serialize(Commands.SetRange, _dynamicBuffer, key, offset, value), Reader.Integer);
+ }
+
+ public int StrLen(string key)
+ {
+ return (int) Send(Writer.Serialize(Commands.StrLen, _dynamicBuffer, key), Reader.Integer);
+ }
+
public long Ttl(string key)
{
return Send(Writer.Serialize(Commands.Ttl, _dynamicBuffer, key), Reader.Integer);
Please sign in to comment.
Something went wrong with that request. Please try again.