public void TestPersist() { Redis.Del("test"); Redis.Set("test", "t1"); Redis.Expire("test", 10); var res1 = Redis.Ttl("test"); Assert.IsTrue(res1 > 0); var res2 = Redis.Persist("test"); Assert.IsTrue(res2); var res3 = Redis.Ttl("test"); Assert.AreEqual(-1, res3); Redis.Del("test"); var res4 = Redis.Persist("test"); Assert.IsFalse(res4); }
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); }
public void ExpireWorks() { Redis redis = new Redis(); redis.Set("foo", "bar"); bool expire = redis.Expire("foo", 300); Assert.IsTrue(expire); }
public void Can_GetTimeToLive() { Redis.SetEntry("key", "val"); Redis.Expire("key", 10); var ttl = Redis.GetTimeToLive("key"); Assert.That(ttl.TotalSeconds, Is.GreaterThanOrEqualTo(9)); Thread.Sleep(1700); ttl = Redis.GetTimeToLive("key"); Assert.That(ttl.TotalSeconds, Is.LessThanOrEqualTo(9)); }
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 static void Set <T>(this Redis self, string key, DateTime expiration, T data) { self.Set <T>(key, data); self.Expire(key, (int)(expiration - DateTime.Now).TotalSeconds); }
//Setter public static void RedisExpire(string key, int time) { Redis.Expire(key, time); }
static void Main(string[] args) { //create a default connection with localhost host and 6379 port Redis redis = new Redis(); //set a foo key with bar value redis.Set("foo", "bar"); bool expire = redis.Expire("foo", 300); // timeout expire after 300s Console.WriteLine(expire); //true int ttl = redis.TimeToLive("foo"); Console.WriteLine(ttl); // 300 bool persist = redis.Persist("foo"); Console.WriteLine(persist); // true bool rename = redis.Rename("foo", "faa"); Console.WriteLine(rename); //true //get the value of foo key string foo = redis.GetString("faa"); Console.WriteLine(foo); //bar //delete the foo key bool delete = redis.Delete("faa"); Console.WriteLine(delete); // true //set a key increment with 2 as value redis.Set("increment", "2"); //increment the key "increment" int increment = redis.Increment("increment"); Console.WriteLine(increment); // 3 //Delete increment; redis.Delete("increment"); //push John Doe to list names and return the length of the list int length = redis.RPush("names", "John Doe"); Console.WriteLine(length); // 1; //Push Mr X at the first position of names redis.LPush("names", "Mr X"); //Get the length of a list int namesLength = redis.LLen("names"); Console.WriteLine(String.Format("The length of names is : {0}", namesLength)); //search for the position of a element in a key. int position = redis.LPos("names", "John Doe"); Console.WriteLine(position); //delete the list redis.Delete("names"); //new list examples with 3 John & 1 Foo redis.RPush("examples", "John"); redis.RPush("examples", "John"); redis.RPush("examples", "Foo"); redis.RPush("examples", "John"); //delete 2 John in examples int removedElements = redis.LRem("examples", 2, "John"); Console.WriteLine(String.Format("count of removed elements : {0}", removedElements)); //and get the length of the list after. int examplesLength = redis.LLen("examples"); Console.WriteLine(String.Format("The length of examples : {0}", examplesLength)); //Dispose redis.Dispose(); //wait until the end Console.ReadKey(); }
static async void Test() { // await Redis.Default.Flushall(); Write(await Redis.Get <Employee>("nonexisting")); Write(await Redis.Set("emp3", GetEmployee(3))); Write(await Redis.Get <Employee>("emp3")); Line(); Write(await Redis.GetSet <Employee>("emp1", GetEmployee(1))); Write(await Redis.Set("emp1", GetEmployee(2))); Write(await Redis.Get <Employee>("emp1")); Write(await Redis.GetSet <Employee>("emp1", GetEmployee(1))); Write(await Redis.Get <Employee>("emp1")); Line(); Write(await Redis.Set("emp1", GetEmployee(1))); Write(await Redis.Set("emp2", GetEmployee(2))); Write(await Redis.Get <Employee, Employee>("emp2", "emp1")); Line(); Write(await Redis.Set("emp1", GetEmployee(1))); Write(await Redis.Set("order1", GetOrder(1))); Write(await Redis.Set("customer1", GetCustomer(1))); Write(await Redis.Get <Employee, Order, Customer>("emp1", "order1", "customer1")); Line(); Write(await Redis.Set(("emp1", GetEmployee(1)), ("emp2", GetEmployee(2)))); Write(await Redis.Get <Employee>("emp1")); Write(await Redis.Get <Employee>("emp2")); Line(); Write(await Redis.SetNX(("key1", GetEmployee(1)), ("key2", GetEmployee(2)))); Write(await Redis.SetNX(("key2", GetEmployee(2)), ("key3", GetEmployee(3)))); var items = await Redis.Get <Employee, Employee, Employee>("key1", "key2", "key3"); Write(items.Item1); Write(items.Item2); Write(items.Item3); Line(); Write(await Redis.PSetEX("key1", 1000, GetEmployee(1))); Write(await Redis.PTtl("key1")); Write(await Redis.Get <Employee>("key1")); Line(); Write(await Redis.Set("key1", GetEmployee(4))); Write(await Redis.Get <Employee>("key1")); Line(); Write(await Redis.SetEX("key1", 10, GetEmployee(1))); Write(await Redis.Ttl("key1")); Write(await Redis.Get <Employee>("key1")); Line(); Write(await Redis.SetNX("key1", GetEmployee(1))); Write(await Redis.SetNX("key1", GetEmployee(2))); Write(await Redis.Get <Employee>("key1")); Line(); Write(await Redis.Set("aa", "sfsdfsd")); Write(await Redis.Exists("aa")); Write(await Redis.Exists("sdfsdf", "aa")); Write(await Redis.Exists("sdfsdf", "sdfsdfdsaa")); Line(); Write(await Redis.Set("mykey", "hello")); Write(await Redis.Expire("mykey", 10)); Write(await Redis.Ttl("mykey")); Write(await Redis.Set("mykey", "hello world")); Write(await Redis.Ttl("mykey")); Line(); var list = Redis.CreateList <Employee>("employees"); Write(await list.Push(GetEmployee(1))); Write(await list.Push(GetEmployee(2))); Write(await list.Index(0)); Write(await list.Index(-1)); Write(await list.Index(3)); Line(); Write(await list.RPush(GetEmployee(1))); Write(await list.RPush(GetEmployee(2))); Write(await list.Insert(true, GetEmployee(2), GetEmployee(3))); Write(await list.Range(0, -1)); Line(); Write(await list.RPush(GetEmployee(1))); Write(await list.RPush(GetEmployee(2))); Write(await list.Len()); Line(); Write(await list.RPush(GetEmployee(1))); Write(await list.RPush(GetEmployee(2))); Write(await list.RPush(GetEmployee(3))); Write(await list.RPush(GetEmployee(4))); Write(await list.Pop()); Write(await list.Range(0, -1)); Write(await list.Len()); Line(); Write(await list.Push(GetEmployee(1))); Write(await list.Push(GetEmployee(2))); Write(await list.Range(0, -1)); Line(); // await Redis.Default.Flushall(); var table = Redis.CreateHashTable("myhash"); Write(await table.MSet(("emp1", GetEmployee(1)))); Write(await table.Del("emp1")); Write(await table.Del("emp2")); Write(await table.Len()); Line(); Write(await table.MSet(("emp1", GetEmployee(1)))); Write(await table.Get <Employee>("emp1")); Write(await table.Get <Employee>("emp2")); Line(); Write(await table.MSet(("field1", GetEmployee(1)), ("field2", GetEmployee(2)))); Write(await table.Get <Employee, Employee>("field1", "field2")); Write(await table.Keys()); Line(); Write(await Redis.Publish("employees", GetEmployee(1))); Write(await Redis.Publish("employees", GetEmployee(1))); Write(await Redis.Publish("employees", GetEmployee(1))); }