Redis Series (i) Use of Stackexchange.redis

Source: Internet
Author: User

One, DLL installation

Search for Stackexchange.redis with nuget, then download it.

The Connectionmultiplexer object is the most central object of the Stackexchange.redis. Instances of this class need to be shared and reused throughout the application domain, and do not keep creating instances of the object in each operation, so use a singleton to create and store the object.

Code:

    

Class Redishelper    {//No write port, default 6397        static configurationoptions configurationoptions = Configurationoptions.parse ("127.0.0.1" + ":" + "6379");        Static Connectionmultiplexer Redisconn;        public static Connectionmultiplexer redisconn        {            get            {                return Connectionmultiplexer.connect ( configurationoptions);}}}    

  

  IDatabase db = redis.GetDatabase();

The DB object returned by Getdatabase () Here is lightweight and does not need to be cached for each fetch. All methods of idatabase have synchronous and asynchronous implementations. Asynchronous implementations of these can be await.

Ii. related methods of strings

Specific code:          

         var db = conn.                Getdatabase ();                #region Strings command String key = "KeyTest1"; SET command db.                Stringset (Key, "10"); The Get command string value = db.                Stringget (key);                Console.WriteLine (value); Append command db.                Stringappend (Key, "10"); Value = db.                Stringget (key);                Console.WriteLine (value); There is a second parameter (integer, see Stringincrement method) for Decrby command//No second argument for DECR command db.                Stringdecrement (key); Value = db.                Stringget (key);                Console.WriteLine (value); There is a second argument for the Incrby command//No second argument for INCR command db.                Stringincrement (key, 2); Value = db.                Stringget (key);                Console.WriteLine (value);                String key2 = "Keytest"; The Setex command, with an expiry time of DB.                Stringset (Key2, "KeyTest2", New TimeSpan (0, 0, 5)); StRing value2 = db.                Stringget (Key2);                Console.WriteLine (value2);                Thread.Sleep (5 * 1000); After 5s, the value value2 = db is not found.                Stringget (Key2);                Console.WriteLine ("5s later:" + value2); Getset command, read the original value, and attach a new value//The following two is the test value = db.                Stringgetset (Key, "2000");                Console.WriteLine (value); Value = db.                Stringget (key);                Console.WriteLine (value); Mset Command db. Stringset (New Keyvaluepair<rediskey, redisvalue>[] {new Keyvaluepair<rediskey, Redis Value> ("Key1", "value1"), New Keyvaluepair<rediskey, redisvalue> ("Key2", "value2"),}                ); Mget command redisvalue[] values = db.                Stringget (new rediskey[] {"Key1", "Key2"});                Console.WriteLine (Values[0] + "&&" + values[1]); #endregion

Operation Result:

  

ThreeHashesThe relevant methods

Specific code:

    

var db = conn. Getdatabase ();                #region Hash command                string key = "MyKey";                Avoid key repeat                db. Keydelete (key);                Hset command                DB. HashSet (Key, "a", "1");                Hget command                String value = db. Hashget (Key, "a");                Console.WriteLine (value);                Hmset                db. HashSet (Key, new hashentry[] {new Hashentry ("B", "2"), New Hashentry ("C", "3")});                Hmget                hashentry[] values = db. Hashgetall (key);                Console.WriteLine (Values[0]. Name + "//" + values[0]. Value);                Hdel                db. Hashdelete (Key, "C");                String VALUEC = db. Hashget (Key, "C");                Console.WriteLine ("C:" + VALUEC);                Hexists                Console.WriteLine (db. Hashexists (Key, "a");                #endregion

Operation Result:

Iv. related methods of lists

Specific code:

    

          var db = conn.                Getdatabase ();                #region List command string key = "MyKey"; Db.                Keydelete (key); Lpush Long index = db.                Listleftpush (Key, "test"); Lindex,index returns the total length, index must subtract one string of value = db.                Listgetbyindex (key, index-1);                Console.WriteLine (value); Linster Long index2 = db.                Listinsertafter (Key, "test", "testright"); String value2 = db.                Listgetbyindex (key, index2-1);                Console.WriteLine (value2); Long index3 = db.                Listinsertbefore (Key, "test", "Testleft"); String value3 = db.                Listgetbyindex (key, index-1); Lrange redisvalue[] values = db.                ListRange (key);                Console.WriteLine ("Values:begin"); Values. ToList ().                ForEach ((v) = {Console.WriteLine (v);                }); ConsolE.writeline ("Values:end"); Lrem long index4 = db.                Listremove (Key, "test"); Values = db.                ListRange (key);                Console.WriteLine ("Values2:begin"); Values. ToList ().                ForEach ((v) = {Console.WriteLine (v);                });                Console.WriteLine ("Values2:end"); Lpop string value5 = db.                Listleftpop (key);                Console.WriteLine (VALUE5); Values = db.                ListRange (key);                Console.WriteLine ("Values3:begin"); Values. ToList ().                ForEach ((v) = {Console.WriteLine (v);                });                Console.WriteLine ("Values3:end");                Console.WriteLine (VALUE3); #endregion

  

Operation Result:

V. Last

Now it's only easy to learn to use strings, hashes and lists, and a lot more to study later.

Redis Series (i) Use of Stackexchange.redis

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.