Using system;using system.collections.generic;using system.linq;using system.text;using ServiceStack.Redis; Namespace com.qfgame.qnx.community.redis{public class Redisbase {private static string Redispath = S Ystem. configuration.configurationsettings.appsettings["Redispath"]; #region--Connection information--//10.0.18.8:6379 public static Pooledredisclientmanager PRCM = Createmanager (new string[] {Redispath}, new string[] {redispath}); private static Pooledredisclientmanager Createmanager (string[] readwritehosts, string[] readonlyhosts) { Supports read/write separation, balanced load return new Pooledredisclientmanager (Readwritehosts, readonlyhosts, new redisclientmanagerconf IG {maxwritepoolsize = 5,//"write" link pool link Number maxreadpoolsize = 5,//"read" Link Pool link number AutoStart = True,}); } #endregion #region-Item--//<summary>///Set monomer///</summary>/<typeparam NA Me= "T" ></typeparam>//<param name= "key" ></param>//<param name= "T" ></param >//<param name= "TimeSpan" ></param>//<returns></returns> public Stati c bool Item_set<t> (string key, T-T) {try {using (iredisclient Redis = PRCM. Getclient ()) {return Redis. Set<t> (Key, T, new TimeSpan (1, 0, 0)); }} catch (Exception ex) {//Loginfo} return False ; }///<summary>//Get monomer///</summary>//<typeparam name= "T" ></ typeparam>//<param name= "key" ></param>///<returns></returns> public s Tatic T ItEm_get<t> (string key) where T:class {using (iredisclient Redis = prcm. Getclient ()) {return Redis. Get<t> (key); }}///<summary>//Remove monomer///</summary>//<param name= "key" &G t;</param> public static bool Item_remove (string key) {using (iredisclient Redis = prcm. Getclient ()) {return Redis. Remove (key); }} #endregion #region--List--public static void List_add<t> (ST Ring key, T T) {using (iredisclient Redis = prcm. Getclient ()) {var redistypedclient = Redis. Gettypedclient<t> (); Redistypedclient.additemtolist (Redistypedclient.lists[key], T); }} public static bool List_remove<t> (string key, T t) { using (iredisclient Redis = prcm. Getclient ()) {var redistypedclient = Redis. Gettypedclient<t> (); Return Redistypedclient.removeitemfromlist (Redistypedclient.lists[key], T) > 0; }} public static void List_removeall<t> (string key) {using (iredisclient Redis = P Rcm. Getclient ()) {var redistypedclient = Redis. Gettypedclient<t> (); Redistypedclient.lists[key]. RemoveAll (); }} public static int List_count (string key) {using (iredisclient Redis = prcm. Getclient ()) {return Redis. GetListCount (key); }} public static list<t> list_getrange<t> (string key, int start, int count) { using (iredisclient Redis = prcm. Getclient ()) {var c = Redis. Gettypedclient<t> (); ReturN C.lists[key]. GetRange (Start, start + count-1); }} public static list<t> list_getlist<t> (string key) {using (I Redisclient Redis = prcm. Getclient ()) {var c = Redis. Gettypedclient<t> (); Return C.lists[key]. GetRange (0, C.lists[key]. Count); }} public static list<t> list_getlist<t> (string key, int pageIndex, int pageSize) {int start = PageSize * (pageIndex-1); Return list_getrange<t> (Key, start, pageSize); }///<summary>//Set cache expires///</summary>//<param name= "key" ></ param>//<param name= "datetime" ></param> public static void List_setexpire (string key, Date Time datetime) {using (iredisclient Redis = prcm. Getclient ()) {Redis. ExpireenTryat (Key, DateTime); }} #endregion #region--Set--public static void set_add<t> (string key, T t) {using (iredisclient Redis = prcm. Getclient ()) {var redistypedclient = Redis. Gettypedclient<t> (); Redistypedclient.sets[key]. ADD (t); }} public static bool Set_contains<t> (string key, T t) {using (Iredisclient Redis = PRCM. Getclient ()) {var redistypedclient = Redis. Gettypedclient<t> (); Return Redistypedclient.sets[key]. Contains (t); }} public static bool Set_remove<t> (string key, T t) {using (Iredisclient Redis = PRCM. Getclient ()) {var redistypedclient = Redis. Gettypedclient<t> (); Return Redistypedclient.sets[key]. Remove (t); }} #endregion #region--Hash--///<summary>///To determine if a data has been cached///</summary>// <typeparam name= "T" ></typeparam>//<param name= "key" ></param>//<param name= "DataKey" ></param>///<returns></returns> public static bool Hash_exist<t> (Strin G key, String DataKey) {using (iredisclient Redis = prcm. Getclient ()) {return Redis. Hashcontainsentry (key, DataKey); }}///<summary>///Storage data to hash table///</summary>//<typeparam NA Me= "T" ></typeparam>//<param name= "key" ></param>//<param name= "DataKey" >< /param>//<returns></returns> public static bool Hash_set<t> (string key, String Datak EY, T t) {using (iredisclient Redis = prcm. Getclient ()) { String value = Servicestack.text.jsonserializer.serializetostring<t> (T); Return Redis. Setentryinhash (Key, DataKey, value); }}///<summary>///Remove a value from the hash///</summary>//<typeparam name= "T" & gt;</typeparam>//<param name= "key" ></param>///<param name= "DataKey" ></param& Gt <returns></returns> public static bool Hash_remove (string key, String DataKey) { using (iredisclient Redis = prcm. Getclient ()) {return Redis. Removeentryfromhash (key, DataKey); }}///<summary>//Remove entire hash///</summary>//<typeparam name= "T" ;</typeparam>//<param name= "key" ></param>///<param name= "DataKey" ></param> ; <returns></returns> public static bool Hash_remove(String key) {using (iredisclient Redis = prcm. Getclient ()) {return Redis. Remove (key); }}///<summary>///From the hash Table///</summary>//<typeparam name= "T" & gt;</typeparam>//<param name= "key" ></param>///<param name= "DataKey" ></param& Gt <returns></returns> public static T hash_get<t> (string key, String DataKey) { using (iredisclient Redis = prcm. Getclient ()) {String value = Redis. Getvaluefromhash (key, DataKey); return servicestack.text.jsonserializer.deserializefromstring<t> (value); }}///<summary>///Get the entire hash data///</summary>//<typeparam name= "T" ></typeparam>//<param name= "key" ></param>//<returns></returns> public static list<t> hash_getall<t> (string key) {using (iredisclient Redis = prcm. Getclient ()) {var list = Redis. Gethashvalues (key); if (list! = null && list. Count > 0) {list<t> result = new list<t> (); foreach (var item in list) {var value = ServiceStack.Text.JsonSerializer.Deseri Alizefromstring<t> (item); Result. ADD (value); } return result; } return null; }}///<summary>//Set cache expires///</summary>//<param name= "key" >< ;/param>//<param name= "datetime" ></param> public static void Hash_setexpire (string key, Da Tetime datetime) {using (iredisclient Redis = prcm. Getclient ()) { Redis. Expireentryat (Key, DateTime); }} #endregion #region--SortedSet--//<summary>///Add Data to SortedSet//</summary>//<typeparam name= "T" ></typeparam>//<param Nam E= "key" ></param>//<param name= "T" ></param>//<param name= "Score" ></param& Gt public static bool Sortedset_add<t> (string key, T T, double score) {using (iredisclient Redis = P Rcm. Getclient ()) {String value = servicestack.text.jsonserializer.serializetostring<t> (T); Return Redis. Additemtosortedset (key, value, score); }}///<summary>//Remove data from SortedSet///</summary>/<typeparam name= "T" ></typeparam>//<param name= "key" ></param>//<param name= "T" ></param>//<returns></returns> public static bool Sortedset_remove<t> (string key, T t) {using (iredisclient Redis = prcm. Getclient ()) {String value = servicestack.text.jsonserializer.serializetostring<t> (T); Return Redis. Removeitemfromsortedset (key, value); }}///<summary>//Trim SortedSet//</summary>/<param name= "key" &G t;</param>//<param name= "size" > retained number of bars </param>//<returns></returns> public static int Sortedset_trim (string key, int size) {using (iredisclient Redis = prcm. Getclient ()) {return Redis. Removerangefromsortedset (key, size, 9999999); }}///<summary>//Get the length of sortedset//</summary>//<param name= "key "></param>//<returns></returns> public static int Sortedset_count (string key) {using (iredisclient Redis = P Rcm. Getclient ()) {return Redis. Getsortedsetcount (key); }}///<summary>//Get SortedSet data from///</summary>//<typepa Ram Name= "T" ></typeparam>//<param name= "key" ></param>//<param name= "PageIndex" ></param>//<param name= "pageSize" ></param>//<returns></returns> public static list<t> sortedset_getlist<t> (string key, int pageIndex, int pageSize) {usin G (iredisclient Redis = prcm. Getclient ()) {var list = Redis. Getrangefromsortedset (Key, (pageIndex-1) * pageSize, PageIndex * pageSize-1); if (list! = null && list. Count > 0) {list<t> result = new LiSt<t> (); foreach (var item in list) {var data = ServiceStack.Text.JsonSerializer.Deseria Lizefromstring<t> (item); Result. ADD (data); } return result; }} return null; }///<summary>//Get all sortedset data///</summary>//<typepara M name= "T" ></typeparam>//<param name= "key" ></param>//<param name= "PageIndex" &G t;</param>//<param name= "pageSize" ></param>///<returns></returns> public static list<t> sortedset_getlistall<t> (string key) {using (iredisclient Redis = PRCM . Getclient ()) {var list = Redis. Getrangefromsortedset (key, 0, 9999999); if (list! = null && list. Count > 0) {list<t> result = new list<t> (); foreach (var item in list) {var data = ServiceStack.Text.JsonSerializer.Deseria Lizefromstring<t> (item); Result. ADD (data); } return result; }} return null; }///<summary>//Set cache expires///</summary>//<param name= "key" ></ param>//<param name= "datetime" ></param> public static void Sortedset_setexpire (string key, datetime datetime) {using (iredisclient Redis = prcm. Getclient ()) {Redis. Expireentryat (Key, DateTime); }}//public static double sortedset_getitemscore<t> (String key,t T)//{//US ing (iredisclient redis = prcm. Getclient ())//{// var data = servicestack.text.jsonserializer.serializetostring<t> (T); Return Redis. Getitemscoreinsortedset (key, data); }//return 0; } #endregion}}
You can use the Redis Desktop Manager management tool to view data in the server cache
Encapsulation classes for REDIS operations