One, the NuGet package required for installation operation
Second, write
Redis only sets the expiration time by the key value, not the expiration of some values inside value
2.1 String Type
/// <summary> ///writing to Redis/// </summary> /// <param name= "key" >Key Value</param> /// <param name= "value" >value</param> /// <param name= "Date" >expiry date</param> Public Static voidSetString (stringKeystringValue,datetime? Date =NULL) { //connectionmultiplexer.connect ("localhost:6379,password=123456")) using(varRedis = Connectionmultiplexer.connect ("Localhost")) { //Write vardb =Redis. Getdatabase (); Db. Stringset ("Key","123456"); //Set Expiration Date if(Date! =NULL) {DateTime time= DateTime.Now.AddSeconds ( -); Db. Keyexpire ("Key", time); } varresult = db. Stringget ("Key"); } }
Third, read
// <summary> ///read the contents of Redis/// </summary> /// <param name= "key" ></param> /// <returns></returns> Public Static stringGetString (stringkey) { using(varRedis = Connectionmultiplexer.connect ("Localhost")) { //Read vardb =Redis. Getdatabase (); varresult =db. Stringget (key); returnresult; } }
Redis uses C # programs to operate Redis