Redis series (1) use of StackExchange. Redis, stackexchange. redis
1. DLL Installation
Search for StackExchange. Redis with NuGet and download it.
The ConnectionMultiplexer object is the most central object of StackExchange. Redis. Instances of this class need to be shared and reused by the entire application domain. Do not create instances of this object continuously in each operation. Therefore, use a single instance to create and store this object.
Code:
Class RedisHelper {// No port is written. The default value is 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();
Here, the db object returned by GetDatabase () is very lightweight and does not need to be cached. It can be retrieved every time. All methods of IDatabase are implemented synchronously and asynchronously. The asynchronous implementation can be await.
Ii. Strings related methods
Code:
Var db = conn. getDatabase (); # region Strings command string key = "keyTest1"; // SET command db. stringSet (key, "10"); // GET command string value = db. stringGet (key); Console. writeLine (value); // APPEND Command db. stringAppend (key, "10"); value = db. stringGet (key); Console. writeLine (value); // The second parameter (integer, see the StringIncrement method) is the DECRBY command // No second parameter is the DECR command db. stringDecrement (key); value = db. stringGet (key); Console. writeLine (value); // The second parameter is the INCRBY command // No second parameter is the INCR command db. stringIncrement (key, 2); value = db. stringGet (key); Console. writeLine (value); string key2 = "keyTest"; // SETEX command, with the expiration time db. stringSet (key2, "keyTest2", new TimeSpan (0, 0, 5); string value2 = db. stringGet (key2); Console. writeLine (value2); Thread. sleep (5*1000); // After 5s, value2 = db cannot be found. stringGet (key2); Console. writeLine ("5 s later:" + value2); // GETSET command, read the original value, and attach a new value. // The following two are test values = 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, RedisValue> ("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
Running result:
Related Hashes Methods
Code:
Var db = conn. getDatabase (); # region Hash command string key = "mykey"; // avoid duplicate keys in the database. 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 ")}); // hmet 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
Running result:
Iv. Lists related methods
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 be reduced by a string 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
Running result:
V. Final
Now, we can only learn how to use Strings, Hashes, and Lists, and there are many more that will be used later.