Here are two things that Redis can do:
1. Provide the cache function, as the cache server;
2. Lightweight Message Queuing (MQ) for use.
<summary>///Redis Help class///</summary> public class Redishelper {//<summary> Put data into Message queue///</summary>//<param name= "listId" > Queue id</param>//<p Aram Name= "value" > Data </param> public static void Enqueue (string listId, String value) {u Sing (iredisclient client = RedisManager.ClientManager.GetClient ()) {client. Enqueueitemonlist (listId, value); } return; }///<summary>///Data extraction from Message queue///</summary>//<param name= "listId" > Queue id& lt;/param>//<returns></returns> public static string Dequeue (String listId) { using (iredisclient client = RedisManager.ClientManager.GetClient ()) {return client. Dequeueitemfromlist (LISTID); }}///<summary>//Out of message queueData//</summary>//<param name= "redisclient" ></param>//<param name= "listId "></param>///<returns></returns> public static string Dequeue (Iredisclient Redisclien T, string listId) {return redisclient.dequeueitemfromlist (listId); }///<summary>//Redis Write///</summary>//<typeparam name= "T" ></typep aram>//<param name= "key" > Keys </param>///<param Name= "value" > Value </param>// /<param name= "CacheTime" > Cache time (minutes) </param>//<returns></returns> public static Boo L set<t> (string key, T value, int cacheTime = 0) {using (iredisclient client = Redismanager.clien Tmanager.getclient ()) {if (cacheTime <= 0) {return client . Set<t> (key, value); } Return client. Set<t> (key, Value, DateTime.Now.AddMinutes (CacheTime)); }}///<summary>//Redis read//</summary>//<typeparam name= "T" >& lt;/typeparam>//<param name= "key" > Keys </param>//<returns></returns> Pub Lic static T get<t> (string key) {using (iredisclient client = RedisManager.ClientManager.GetClien T ()) {return client. Get<t> (key); }}}///<summary> Redis Management class///</summary> public class Redismanager {PU Blic static Pooledredisclientmanager Clientmanager {get; private set;} Static Redismanager () {redisclientmanagerconfig redisconfig = new Redisclientmanagerconfig (); redisconfig.maxwritepoolsize = 128; redisconfig.maxreadpoolsize = 128; Clientmanager = new Pooledredisclientmanager (New string[] {"127.0.0.1"}, new string[] {"127.0.0.1"}, Redisconfig); } }
Message Queuing Data consumer Sample code:
JavaScriptSerializer serializer = new JavaScriptSerializer (); Data consumer while (true) { String data = Redishelper.dequeue ("Sendactiveemail"); if (data! = null) { Emailmodel Emailmodel = (Emailmodel) serializer. Deserialize (data, typeof (Emailmodel)); Console.WriteLine ("Send mail:" + emailmodel.mailto); Commonhelper.sendemail (Emailmodel.mailto, Emailmodel.subject, Emailmodel.body, true); Console.WriteLine ("Send mail Complete"); } else { thread.sleep; } }
What can Redis do?