/// <summary> /// Disposes the client /// </summary> public void Dispose() { MessageCounter.Dispose(); Redis.Dispose(); Discord.Dispose(); DbContext.Dispose(); }
internal async Task DeinitAsync() { await Discord.DisconnectAsync(); Redis.Dispose(); Discord.Dispose(); }
/// <summary> /// 从hash表获取数据 /// </summary> public T Get <T>(string hashId, string key) { string value = Redis.GetValueFromHash(hashId, key); Redis.Dispose(); return(JsonSerializer.DeserializeFromString <T>(value)); }
/// <summary> /// 释放回收内存 /// </summary> public virtual void Dispose() { if (!this._disposed) { Redis.Dispose(); Redis = null; GC.SuppressFinalize(this); } this._disposed = true; }
public override void OnBeforeEachTest() { if (Redis != null) { Redis.Dispose(); } Redis = new RedisClient(TestConfig.SingleHost) { Db = RedisDb }; }
public override void OnBeforeEachTest() { base.OnBeforeEachTest(); Redis?.Dispose(); Redis = new RedisClient(TestConfig.SingleHost) { NamespacePrefix = "RedisTypedClientTests:" }; RedisTyped = Redis.As <CacheRecord>(); }
protected override void Dispose(bool disposing) { if (SolutionEventsHandlerId != 0) { (GetService(typeof(SVsSolution)) as IVsSolution).UnadviseSolutionEvents(SolutionEventsHandlerId); } Redis?.Dispose(); ConnectionWrapper?.Dispose(); WindowsServiceClient?.Dispose(); RemoteModelChangeManager?.Dispose(); LocalModelChangeManager?.Dispose(); HttpClient?.Dispose(); base.Dispose(disposing); }
/// <summary> /// 获取整个hash的数据 /// </summary> public List <T> GetAll <T>(string hashId) { var result = new List <T>(); var list = Redis.GetHashValues(hashId); if (list != null && list.Count > 0) { list.ForEach(x => { var value = JsonSerializer.DeserializeFromString <T>(x); result.Add(value); }); } Redis.Dispose(); return(result); }
public void Dispose() { _redisServer.Dispose(); }
static void Main(string[] args) { Redis r; if (args.Length >= 1) { if (args.Length >= 2) { r = new Redis(args[0], Convert.ToInt16(args[1])); } else { r = new Redis(args[0]); } } else { r = new Redis(); } r.Set("foo", "bar"); r.FlushAll(); if (r.Keys.Length > 0) { Console.WriteLine("error: there should be no keys but there were {0}", r.Keys.Length); } r.Set("foo", "bar"); if (r.Keys.Length < 1) { Console.WriteLine("error: there should be at least one key"); } if (r.GetKeys("f*").Length < 1) { Console.WriteLine("error: there should be at least one key"); } if (r.TypeOf("foo") != Redis.KeyType.String) { Console.WriteLine("error: type is not string"); } r.Set("bar", "foo"); var arr = r.GetKeys("foo", "bar"); if (arr.Length != 2) { Console.WriteLine("error, expected 2 values"); } if (arr [0].Length != 3) { Console.WriteLine("error, expected foo to be 3"); } if (arr [1].Length != 3) { Console.WriteLine("error, expected bar to be 3"); } r ["one"] = "world"; if (r.GetSet("one", "newvalue") != "world") { Console.WriteLine("error: Getset failed"); } if (!r.Rename("one", "two")) { Console.WriteLine("error: failed to rename"); } if (r.Rename("one", "one")) { Console.WriteLine("error: should have sent an error on rename"); } r.Db = 10; r.Set("foo", "diez"); if (r.GetString("foo") != "diez") { Console.WriteLine("error: got {0}", r.GetString("foo")); } if (!r.Remove("foo")) { Console.WriteLine("error: Could not remove foo"); } r.Db = 0; if (r.GetString("foo") != "bar") { Console.WriteLine("error, foo was not bar"); } if (!r.ContainsKey("foo")) { Console.WriteLine("error, there is no foo"); } if (r.Remove("foo", "bar") != 2) { Console.WriteLine("error: did not remove two keys"); } if (r.ContainsKey("foo")) { Console.WriteLine("error, foo should be gone."); } r.Save(); r.BackgroundSave(); Console.WriteLine("Last save: {0}", r.LastSave); //r.Shutdown (); var info = r.GetInfo(); foreach (var k in info.Keys) { Console.WriteLine("{0} -> {1}", k, info [k]); } var dict = new Dictionary <string, byte[]>(); dict ["hello"] = Encoding.UTF8.GetBytes("world"); dict ["goodbye"] = Encoding.UTF8.GetBytes("my dear"); //r.Set (dict); // JS (10/12/2010): This code is now obsolete. A new test needs to be written. // r.RightPush("alist", "avalue"); // r.RightPush("alist", "another value"); // assert(r.ListLength("alist") == 2, "List length should have been 2"); // // var value = Encoding.UTF8.GetString(r.ListIndex("alist", 1)); // if(!value.Equals("another value")) // Console.WriteLine("error: Received {0} and should have been 'another value'", value); // value = Encoding.UTF8.GetString(r.LeftPop("alist")); // if (!value.Equals("avalue")) // Console.WriteLine("error: Received {0} and should have been 'avalue'", value); // if (r.ListLength("alist") != 1) // Console.WriteLine("error: List should have one element after pop"); // r.RightPush("alist", "yet another value"); // byte[][] values = r.ListRange("alist", 0, 1); // if (!Encoding.UTF8.GetString(values[0]).Equals("another value")) // Console.WriteLine("error: Range did not return the right values"); assert(r.AddToSet("FOO", Encoding.UTF8.GetBytes("BAR")), "Problem adding to set"); assert(r.AddToSet("FOO", Encoding.UTF8.GetBytes("BAZ")), "Problem adding to set"); assert(r.AddToSet("FOO", "Hoge"), "Problem adding string to set"); assert(r.CardinalityOfSet("FOO") == 3, "Cardinality should have been 3 after adding 3 items to set"); assert(r.IsMemberOfSet("FOO", Encoding.UTF8.GetBytes("BAR")), "BAR should have been in the set"); assert(r.IsMemberOfSet("FOO", "BAR"), "BAR should have been in the set"); byte[][] members = r.GetMembersOfSet("FOO"); assert(members.Length == 3, "Set should have had 3 members"); assert(r.RemoveFromSet("FOO", "Hoge"), "Should have removed Hoge from set"); assert(!r.RemoveFromSet("FOO", "Hoge"), "Hoge should not have existed to be removed"); assert(2 == r.GetMembersOfSet("FOO").Length, "Set should have 2 members after removing Hoge"); assert(r.AddToSet("BAR", Encoding.UTF8.GetBytes("BAR")), "Problem adding to set"); assert(r.AddToSet("BAR", Encoding.UTF8.GetBytes("ITEM1")), "Problem adding to set"); assert(r.AddToSet("BAR", Encoding.UTF8.GetBytes("ITEM2")), "Problem adding string to set"); assert(r.GetUnionOfSets("FOO", "BAR").Length == 4, "Resulting union should have 4 items"); assert(1 == r.GetIntersectionOfSets("FOO", "BAR").Length, "Resulting intersection should have 1 item"); assert(1 == r.GetDifferenceOfSets("FOO", "BAR").Length, "Resulting difference should have 1 item"); assert(2 == r.GetDifferenceOfSets("BAR", "FOO").Length, "Resulting difference should have 2 items"); byte[] itm = r.GetRandomMemberOfSet("FOO"); assert(null != itm, "GetRandomMemberOfSet should have returned an item"); assert(r.MoveMemberToSet("FOO", "BAR", itm), "Data within itm should have been moved to set BAR"); r.FlushDb(); if (r.Keys.Length > 0) { Console.WriteLine("error: there should be no keys but there were {0}", r.Keys.Length); } Redis subscriber = new Redis(r.Host, r.Port); subscriber.Subscribe("TestChannel", mesg => { var tmp = Encoding.ASCII.GetString(mesg); assert(tmp == publishTestValue, "Received message from PUBLISH should match expected value."); }); int result = r.Publish("TestChannel", publishTestValue); assert(1 == result, "There should be one subscribed client."); subscriber.Unsubscribe(); subscriber.Dispose(); r.Dispose(); }
protected override async Task TeardownCollection() { Redis?.Dispose(); await DownProjectCommand.WithFiles(_fullDockerComposeFilePath).Build().Execute(); }
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(); }
public void TearDownRedis() => _redis.Dispose();
static RedisInstance() { AppDomain.CurrentDomain.DomainUnload += (s, e) => Redis.Dispose(); }
public void TearDown() { Redis.Dispose(); redisInstance.Dispose(); }
static void Main(string[] args) { Redis r; if (args.Length >= 1) { if (args.Length >= 2) r = new Redis(args[0], Convert.ToInt16(args[1])); else r = new Redis(args[0]); } else { r = new Redis(); } r.Set("foo", "bar"); r.FlushAll(); if (r.Keys.Length > 0) Console.WriteLine("error: there should be no keys but there were {0}", r.Keys.Length); r.Set ("foo", "bar"); if (r.Keys.Length < 1) Console.WriteLine ("error: there should be at least one key"); if (r.GetKeys ("f*").Length < 1) Console.WriteLine ("error: there should be at least one key"); if (r.TypeOf ("foo") != Redis.KeyType.String) Console.WriteLine ("error: type is not string"); r.Set ("bar", "foo"); var arr = r.GetKeys ("foo", "bar"); if (arr.Length != 2) Console.WriteLine ("error, expected 2 values"); if (arr [0].Length != 3) Console.WriteLine ("error, expected foo to be 3"); if (arr [1].Length != 3) Console.WriteLine ("error, expected bar to be 3"); r ["one"] = "world"; if (r.GetSet ("one", "newvalue") != "world") Console.WriteLine ("error: Getset failed"); if (!r.Rename ("one", "two")) Console.WriteLine ("error: failed to rename"); if (r.Rename ("one", "one")) Console.WriteLine ("error: should have sent an error on rename"); r.Db = 10; r.Set ("foo", "diez"); if (r.GetString ("foo") != "diez"){ Console.WriteLine ("error: got {0}", r.GetString ("foo")); } if (!r.Remove ("foo")) Console.WriteLine ("error: Could not remove foo"); r.Db = 0; if (r.GetString ("foo") != "bar") Console.WriteLine ("error, foo was not bar"); if (!r.ContainsKey ("foo")) Console.WriteLine ("error, there is no foo"); if (r.Remove ("foo", "bar") != 2) Console.WriteLine ("error: did not remove two keys"); if (r.ContainsKey ("foo")) Console.WriteLine ("error, foo should be gone."); r.Save (); r.BackgroundSave (); Console.WriteLine ("Last save: {0}", r.LastSave); //r.Shutdown (); var info = r.GetInfo (); foreach (var k in info.Keys){ Console.WriteLine ("{0} -> {1}", k, info [k]); } var dict = new Dictionary<string, byte[]>(); dict ["hello"] = Encoding.UTF8.GetBytes ("world"); dict ["goodbye"] = Encoding.UTF8.GetBytes ("my dear"); //r.Set (dict); // JS (10/12/2010): This code is now obsolete. A new test needs to be written. // r.RightPush("alist", "avalue"); // r.RightPush("alist", "another value"); // assert(r.ListLength("alist") == 2, "List length should have been 2"); // // var value = Encoding.UTF8.GetString(r.ListIndex("alist", 1)); // if(!value.Equals("another value")) // Console.WriteLine("error: Received {0} and should have been 'another value'", value); // value = Encoding.UTF8.GetString(r.LeftPop("alist")); // if (!value.Equals("avalue")) // Console.WriteLine("error: Received {0} and should have been 'avalue'", value); // if (r.ListLength("alist") != 1) // Console.WriteLine("error: List should have one element after pop"); // r.RightPush("alist", "yet another value"); // byte[][] values = r.ListRange("alist", 0, 1); // if (!Encoding.UTF8.GetString(values[0]).Equals("another value")) // Console.WriteLine("error: Range did not return the right values"); assert(r.AddToSet("FOO", Encoding.UTF8.GetBytes("BAR")), "Problem adding to set"); assert(r.AddToSet("FOO", Encoding.UTF8.GetBytes("BAZ")),"Problem adding to set"); assert(r.AddToSet("FOO", "Hoge"),"Problem adding string to set"); assert(r.CardinalityOfSet("FOO") == 3, "Cardinality should have been 3 after adding 3 items to set"); assert(r.IsMemberOfSet("FOO", Encoding.UTF8.GetBytes("BAR")), "BAR should have been in the set"); assert(r.IsMemberOfSet("FOO", "BAR"), "BAR should have been in the set"); byte[][] members = r.GetMembersOfSet("FOO"); assert(members.Length == 3, "Set should have had 3 members"); assert(r.RemoveFromSet("FOO", "Hoge"), "Should have removed Hoge from set"); assert(!r.RemoveFromSet("FOO", "Hoge"), "Hoge should not have existed to be removed"); assert(2 == r.GetMembersOfSet("FOO").Length, "Set should have 2 members after removing Hoge"); assert(r.AddToSet("BAR", Encoding.UTF8.GetBytes("BAR")), "Problem adding to set"); assert(r.AddToSet("BAR", Encoding.UTF8.GetBytes("ITEM1")),"Problem adding to set"); assert(r.AddToSet("BAR", Encoding.UTF8.GetBytes("ITEM2")),"Problem adding string to set"); assert(r.GetUnionOfSets("FOO","BAR").Length == 4, "Resulting union should have 4 items"); assert(1 == r.GetIntersectionOfSets("FOO", "BAR").Length, "Resulting intersection should have 1 item"); assert(1 == r.GetDifferenceOfSets("FOO", "BAR").Length, "Resulting difference should have 1 item"); assert(2 == r.GetDifferenceOfSets("BAR", "FOO").Length, "Resulting difference should have 2 items"); byte[] itm = r.GetRandomMemberOfSet("FOO"); assert(null != itm, "GetRandomMemberOfSet should have returned an item"); assert(r.MoveMemberToSet("FOO","BAR", itm), "Data within itm should have been moved to set BAR"); r.FlushDb(); if (r.Keys.Length > 0) Console.WriteLine("error: there should be no keys but there were {0}", r.Keys.Length); Redis subscriber = new Redis(r.Host, r.Port); subscriber.Subscribe("TestChannel", mesg => { var tmp = Encoding.ASCII.GetString(mesg); assert(tmp == publishTestValue, "Received message from PUBLISH should match expected value."); }); int result = r.Publish("TestChannel", publishTestValue); assert(1 == result, "There should be one subscribed client."); subscriber.Unsubscribe(); subscriber.Dispose(); r.Dispose(); }