public void Can_save_via_types() { var dtos = 10.Times(i => new Dummy { Id = i, Name = "Name" + i }); Redis.Set("dummy:strings", dtos); var fromDtos = Redis.Get <List <Dummy> >("dummy:strings"); Assert.That(fromDtos.Count, Is.EqualTo(10)); }
public void TestGetRange() { Redis.Del("test"); Redis.Set("test", "This is a string"); Assert.AreEqual("This", Redis.GetRange("test", 0, 3)); Assert.AreEqual("ing", Redis.GetRange("test", -3, -1)); Assert.AreEqual("This is a string", Redis.GetRange("test", 0, -1)); Assert.AreEqual("string", Redis.GetRange("test", 10, 100)); Redis.Del("test"); }
public void TestIncrByFloat() { Redis.Del("test"); Redis.Set("test", 10.50); Assert.AreEqual(10.6, Redis.IncrByFloat("test", 0.1)); Redis.Set("test", "5.0e3"); Assert.AreEqual(5200, Redis.IncrByFloat("test", 200)); Redis.Del("test"); }
public void TestDiscard() { using (new RedisTestKeys(Redis, "test1")) { Redis.Multi(); Assert.IsNull(Redis.Echo("asdf")); Assert.AreEqual(default(DateTime), Redis.Time()); Assert.AreEqual(0, Redis.StrLen("test1")); Assert.IsNull(Redis.Set("test1", "asdf")); Assert.AreEqual("OK", Redis.Discard()); Assert.IsFalse(Redis.Exists("test1")); } }
/// <summary> /// 更新文件流位置 /// </summary> /// <param name="position"></param> /// <returns></returns> public bool SetFilePosition(string position) { lock (this) { string key = "LogPosition"; int exists = Redis.Exists(key); if (exists > 0) { Redis.Del(key); } return(Redis.Set(key, position)); } }
public void TestBitOp() { Redis.Del("test1", "test2", "test3"); Redis.Set("test1", "foobar"); Redis.Set("test2", "abcdef"); var resp1 = Redis.BitOp(RedisBitOp.And, "test3", "test1", "test2"); Assert.AreEqual(6, resp1); Redis.Del("test1", "test2", "test3"); }
public void TestMGet() { Redis.Del("test1", "test2", "test3"); Redis.Set("test1", 1); Redis.Set("test2", 2); var resp1 = Redis.MGet("test1", "test2", "test3"); Assert.AreEqual("1", resp1[0]); Assert.AreEqual("2", resp1[1]); Assert.IsNull(resp1[2]); Redis.Del("test1", "test2", "test3"); }
public void TestLargeBulkStream() { string random = GetRandomString(1048576); using (new RedisTestKeys(Redis, "test")) { Assert.AreEqual("OK", Redis.Set("test", random)); using (var ms = new MemoryStream()) { Redis.StreamTo(ms, 1024, x => x.Get("test")); string result = Encoding.UTF8.GetString(ms.ToArray()); Assert.AreEqual(random, result); } } }
public void TestSelect() { string test_key = Guid.NewGuid().ToString(); string test_value = "1"; using (new RedisTestKeys(Redis, test_key)) { Redis.Set(test_key, test_value); Assert.AreEqual(test_value, Redis.Get(test_key)); Assert.AreEqual("OK", Redis.Select(1)); Assert.IsNull(Redis.Get(test_key)); Assert.AreEqual("OK", Redis.Select(0)); Assert.AreEqual(test_value, Redis.Get(test_key)); } }
public void TestPipelineTransactionDiscard() { using (new RedisTestKeys(Redis, "test1")) { Redis.StartPipeTransaction(); Assert.AreEqual(default(DateTime), Redis.Time()); Assert.IsNull(Redis.Discard()); Assert.IsNull(Redis.Set("test1", "value")); var result = Redis.EndPipe(); Assert.IsNotNull(result); Assert.AreEqual(2, result.Length); Assert.AreEqual("OK", result[0]); Assert.AreEqual("OK", result[1]); } }
public void TestBitPos() { using (new RedisTestKeys(Redis, "test1")) { Redis.Set("test1", new byte[] { 0xff, 0xf0, 0x00 }); Assert.AreEqual(12, Redis.BitPos("test1", 0)); /*Redis.Set("test1", "\x00\xff\xf0"); * Assert.AreEqual(8, Redis.BitPos("test1", 1, 0)); * Assert.AreEqual(8, Redis.BitPos("test1", 1, 1)); * * Redis.Set("test1", "\x00\x00\x00"); * Assert.AreEqual(-1, Redis.BitPos("test1", 1));*/ } }
public void TestSet() { using (new RedisTestKeys(Redis, "test")) { Assert.AreEqual("OK", Redis.Set("test", 1)); Assert.AreEqual("1", Redis.Get("test")); } using (new RedisTestKeys(Redis, "test")) { Assert.AreEqual("OK", Redis.Set("test", 1, 10)); var pttl = Redis.PTtl("test"); Assert.IsTrue(pttl > 0); Assert.IsTrue(pttl <= 10000L); } using (new RedisTestKeys(Redis, "test")) { Assert.AreEqual("OK", Redis.Set("test", 1, 10000L)); var ttl = Redis.Ttl("test"); Assert.IsTrue(ttl > 0); Assert.IsTrue(ttl <= 10); } using (new RedisTestKeys(Redis, "test")) { Assert.AreEqual("OK", Redis.Set("test", 1, null, RedisExistence.Nx)); Assert.IsNull(Redis.Set("test", 2, null, RedisExistence.Nx)); Assert.AreEqual("1", Redis.Get("test")); } using (new RedisTestKeys(Redis, "test")) { Assert.IsNull(Redis.Set("test", 1, null, RedisExistence.Xx)); Assert.AreEqual("OK", Redis.Set("test", 2, null, RedisExistence.Nx)); Assert.AreEqual("2", Redis.Get("test")); } using (new RedisTestKeys(Redis, "test")) { Assert.AreEqual("OK", Redis.Set("test", 1, TimeSpan.FromSeconds(10), RedisExistence.Nx)); Assert.IsNull(Redis.Set("test", 2, null, RedisExistence.Nx)); Assert.AreEqual("1", Redis.Get("test")); var pttl = Redis.PTtl("test"); Assert.IsTrue(pttl > 0); Assert.IsTrue(pttl <= 10000L); } }
public void TestUnwatch() { using (new RedisTestKeys(Redis, "test1")) { Redis.Watch("test1"); Assert.AreEqual("OK", Redis.Unwatch()); Redis.Multi(); using (var otherClient = new RedisClient(Host, Port, 0)) { otherClient.Auth(Password); otherClient.Set("test1", "other"); } Redis.Set("test1", "multi"); Assert.IsNotNull(Redis.Exec()); } }
public static void Set <T>(this Redis self, string key, T data) { IFormatter bFormatter = new BinaryFormatter(); MemoryStream memStream = new MemoryStream(); bFormatter.Serialize(memStream, data); memStream.Position = 0; byte[] serliazedData = memStream.ToArray(); memStream.Close(); memStream.Dispose(); self.Set(key, serliazedData); }
public void TestExists() { Redis.Del("test"); var res1 = Redis.Exists("test"); Assert.IsFalse(res1); Redis.Set("test", 1); var res2 = Redis.Exists("test"); Assert.IsTrue(res2); Redis.Del("test"); }
public void Can_Set_and_Get_key_with_all_byte_values() { const string key = "bytesKey"; var value = new byte[256]; for (var i = 0; i < value.Length; i++) { value[i] = (byte)i; } Redis.Set(key, value); var resultValue = Redis.Get(key); Assert.That(resultValue, Is.EquivalentTo(value)); }
public void TestKeys() { string prefix = Guid.NewGuid().ToString(); string[] keys = new[] { "test:" + prefix + ":1", "test:" + prefix + ":2", "test:" + prefix + ":3" }; using (new RedisTestKeys(Redis, keys)) { Redis.Del(keys); foreach (var key in keys) { Redis.Set(key, 1); } Assert.AreEqual(keys.Length, Redis.Keys("test:" + prefix + ":*").Length); } }
public void TestRawBytes() { using (new RedisTestKeys(Redis, "test1")) { double pi = Math.PI; byte[] bytes = BitConverter.GetBytes(pi); Redis.Set("test1", bytes); byte[] buffer = new byte[sizeof(double)]; Redis.BufferFor(x => x.Get("test1")); Redis.Read(buffer, 0, buffer.Length); for (int i = 0; i < bytes.Length; i++) { Assert.AreEqual(bytes[i], buffer[i]); } } }
public void TestDump() { Redis.Del("test"); Redis.Set("test", 10); var res = Redis.Dump("test"); byte[] expected = new byte[] { 0x0, 0xC0, 0xA, 0x06, 0x0, 0xF8, 0x72, 0x3F, 0xC5, 0xFB, 0xFB, 0x5F, 0x28 }; for (int i = 0; i < res.Length; i++) { Assert.AreEqual(expected[i], res[i]); } Redis.Del("test"); }
public T GetOrCreateByUuid <T>(string uuid, string index = "") { var type = typeof(T); if (!RedisTypes.Contains(type)) { throw new ArgumentException($"Type {type} is not a uuid referenced Redis type"); } // Check for existence of object locally first if (TryGetValue(uuid, out T obj)) { return(obj); } // Check for existence of object in Redis var storageKey = $"{type.FullName}:{uuid}"; if (Redis.KeyExists(storageKey)) { obj = Redis.Get <T>(storageKey); } // Fall back to creating it if needed if (obj == null) { obj = (T)Activator.CreateInstance(typeof(T), new object[1] { uuid }); Redis.Set(storageKey, obj); } // Now that we have the object, update the store if (string.IsNullOrEmpty(index)) { Set(uuid, obj); } else { SetWithIndex(uuid, obj, index); } return(obj); }
public void TestExpire() { Redis.Del("test"); var res1 = Redis.Expire("test", 10); Assert.IsFalse(res1); Redis.Set("test", "t1"); var res2 = Redis.Expire("test", 10); Assert.IsTrue(res2); var res3 = Redis.Ttl("test"); Assert.IsTrue(res3 > 0); Redis.Del("test"); }
public void TestBitCount() { Redis.Del("test"); Redis.Set("test", "foobar"); var resp1 = Redis.BitCount("test"); Assert.AreEqual(26, resp1); var resp2 = Redis.BitCount("test", 0, 0); Assert.AreEqual(4, resp2); var resp3 = Redis.BitCount("test", 1, 1); Assert.AreEqual(6, resp3); Redis.Del("test"); }
public void TestRenameX() { Redis.Del("test", "test2", "test3"); string guid = Guid.NewGuid().ToString(); Redis.Set("test", guid); Redis.Set("test2", 1); var resp1 = Redis.RenameNx("test", "test2"); Assert.IsFalse(resp1); var resp2 = Redis.RenameNx("test", "test3"); Assert.IsTrue(resp2); Redis.Del("test", "test2", "test3"); }
protected void LoadDifferentKeyTypes(IRedisClient client) { var items = new List <string> { "one", "two", "three", "four" }; var map = new Dictionary <string, string> { { "A", "one" }, { "B", "two" }, { "C", "three" }, { "D", "four" }, }; items.ForEach(x => Redis.Set("urn:testkeytypes:string:" + x, x)); items.ForEach(x => Redis.AddItemToList("urn:testkeytypes:list", x)); items.ForEach(x => Redis.AddItemToSet("urn:testkeytypes:set", x)); var i = 0; items.ForEach(x => Redis.AddItemToSortedSet("urn:testkeytypes:zset", x, i++)); Redis.SetRangeInHash("urn:testkeytypes:hash", map); }
public void TestUTF8() { using (new RedisTestKeys(Redis, "test1")) { string bytes1 = Encoding.UTF8.GetString(new byte[] { 0x24 }); Redis.Set("test1", bytes1); Assert.AreEqual("$", Redis.Get("test1")); string bytes2 = Encoding.UTF8.GetString(new byte[] { 0xc2, 0xa2 }); Redis.Set("test1", bytes2); Assert.AreEqual("¢", Redis.Get("test1")); string bytes3 = Encoding.UTF8.GetString(new byte[] { 0xe2, 0x82, 0xac }); Redis.Set("test1", bytes3); Assert.AreEqual("€", Redis.Get("test1")); string bytes4 = Encoding.UTF8.GetString(new byte[] { 0xf0, 0xa4, 0xad, 0xa2 }); Redis.Set("test1", bytes4); Assert.AreEqual("𤭢", Redis.Get("test1")); } }
public void TestRename() { Redis.Del("test", "test2"); string guid = Guid.NewGuid().ToString(); Redis.Set("test", guid); var resp1 = Redis.Rename("test", "test2"); Assert.AreEqual("OK", resp1); var resp2 = Redis.Exists("test"); Assert.IsFalse(resp2); var resp3 = Redis.Get("test2"); Assert.AreEqual(guid, resp3); Redis.Del("test", "test2"); }
public void Can_GetValues_and_Remove_multiple_keys_in_same_transaction() { 5.Times(x => Redis.Set("foo" + x, x)); var keys = Redis.SearchKeys("foo*"); Assert.That(keys, Has.Count.EqualTo(5)); var values = new List <string>(); using (var transaction = Redis.CreateTransaction()) { transaction.QueueCommand(x => x.GetValues(keys), val => values = val); transaction.QueueCommand(x => x.RemoveAll(keys)); transaction.Commit(); } Assert.That(values, Has.Count.EqualTo(5)); keys = Redis.SearchKeys("foo*"); Assert.That(keys, Has.Count.EqualTo(0)); }
public void Watch_aborts_transaction() { Assert.That(Redis.GetValue(Key), Is.Null); const string value1 = "value1"; try { Redis.Watch(Key); Redis.Set(Key, value1); using (var trans = Redis.CreateTransaction()) { trans.QueueCommand(r => r.Set(Key, value1)); var success = trans.Commit(); Assert.False(success); Assert.AreEqual(value1, Redis.Get <string>(Key)); } } catch (NotSupportedException) { Assert.That(Redis.GetValue(Key), Is.Null); } }
public void Transaction_can_issue_watch() { Redis.Del(Key); Assert.That(Redis.GetValue(Key), Is.Null); string KeySquared = Key + Key; Redis.Del(KeySquared); Redis.Watch(Key, KeySquared); Redis.Set(Key, 7); using (var trans = Redis.CreateTransaction()) { trans.QueueCommand(r => r.Set(Key, 1)); trans.QueueCommand(r => r.Set(KeySquared, 2)); trans.Commit(); } Assert.That(Redis.GetValue(Key), Is.EqualTo("7")); Assert.That(Redis.GetValue(KeySquared), Is.Null); }
public void TestExpireAt() { Redis.Del("test"); var server_time = Redis.Time(); var res1 = Redis.ExpireAt("test", server_time + TimeSpan.FromSeconds(10)); Assert.IsFalse(res1); Redis.Set("test", 1); var res2 = Redis.ExpireAt("test", server_time + TimeSpan.FromSeconds(10)); Assert.IsTrue(res2); var res3 = Redis.Ttl("test"); Assert.IsTrue(res3 > 0); Redis.Del("test"); }