.NETcore使用CSRedisCore操作Redis


因為Servicestack.Redies免費每天只能調用6000次,所以找了個免費的能在.NETcore使用的插件CSRedisCore,封裝了一下。

redies訂閱模式的調用:RedisCoreHelper.Sub(“quote”, action);

1
2
3
4
5
6
7
8
9
10
11
12
13
public  void  action( string  message)
{
     if  (!message.IsNullOrEmpty() && ! "null" .Equals(message))
     {
         
//dosomething
                
     }
     else
     {
         //Thread.Sleep(200);
     }
}       

  

封裝如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
internal  class  RedisConfigManager
{
     /// <summary>
     /// 獲取配置
     /// </summary>
     /// <returns></returns>
     public  static  RedisConfig GetConfig()
     {
         var  path = WorkPath.CurrentDirectory +  "\\redis.config.json" ;
         Log.Info( "path:" + path);
         var  json = FileManager.GetTextFromPath(path);
         if  (json.IsNullOrEmpty())
             return  new  RedisConfig();
         var  config = JsonConvert.Deserialize<RedisConfig>(json);
         return  config;
     }
}

  

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
namespace  LT.Cache
{
     public  class  RedisCoreHelper
     {
 
         static  CSRedisClient redisManger =  null ;
         static  CSRedisClient GetClient()
         {
             return  redisManger;
         }
         static  RedisCoreHelper()
         {
             var  redisconfig = RedisConfigManager.GetConfig();
             redisManger =  new  CSRedisClient(redisconfig.CoreRedisServer);       //Redis的連接字符串
         }
 
         /// <summary>
         /// TradeManageMessage 和 TradeManageMessage:MQ隊列
         /// </summary>
         /// <returns></returns>
         public  static  bool  EnQeenTradeManageMessage( string  value)
         {
             try
             {
                 Log.Info( "yinzhou--EnQeenTradeManageMessage:"  + value);
                 //從頭部插入
                 GetClient().LPush( "TradeManageMessage" , value);
                 GetClient().LPush( "TradeManageMessage:MQ" , value);
                 return  true ;
             }
             catch  (Exception e)
             {
                 Log.Error($ "EnQeenTradeManageMessage:key=TradeManageMessage:MQ,value={value}" , e);
                 return  false ;
             }
         }
         /// <summary>
         /// TradeManageMessage 和 TradeManageMessage:MQ隊列
         /// </summary>
         /// <returns></returns>
         public  static  bool  EnQeenTradeManageMessage<T>(T value)
         {
             try
             {
                 //從頭部插入
                 GetClient().LPush( "TradeManageMessage" , value);
                 GetClient().LPush( "TradeManageMessage:MQ" , value);
                 return  true ;
             }
             catch  (Exception e)
             {
                 Log.Error($ "EnQeenTradeManageMessage:key=TradeManageMessage:MQ,value={value}" , e);
                 return  false ;
             }
         }
 
         public  static  bool  EnQueen( string  key,  string  value)
         {
             try
             {
                 //從頭部插入
                 GetClient().LPush(key, value);
                 return  true ;
             }
             catch  (Exception e)
             {
                 Log.Error($ "EnQueen:key={key},value={value}" , e);
                 return  false ;
             }
         }
 
         public  static  string  DeQueen( string  key)
         {
             string  result =  "" ;
             try
             {
                 //從尾部取值
                 result = GetClient().RPop(key);
                 return  result;
             }
             catch  (Exception e)
             {
                 Log.Error($ "DeQueen:key={key}" , e);
                 return  result;
             }
         }
     //redis訂閱模式
         public   static  void  Sub( string  key,Action< string > action)
         {
             GetClient().Subscribe((key, msg => action(msg.Body)));
         }
 
         public  static  string [] DeQueenAll( string  key)
         {
             string [] result = { };
             try
             {
                 long  len = GetClient().LLen(key);
             
                 //取出指定數量數據
                 result = GetClient().LRange(key,0, len-1);
                 //刪除指定數據
                 bool  res=GetClient().LTrim(key, len, -1);
   
                 return  result;
             }
             catch  (Exception e)
             {
                 Log.Error($ "DeQueen:key={key}" , e);
                 return  result;
             }
         }
 
         public  static  bool  EnQueen<T>( string  key, T value)
         {
             try
             {
                 //從頭部插入
                 long  len= GetClient().LPush(key, value);
                 if (len>0)
                     return  true ;
                 else
                     return  false ;
             }
             catch  (Exception e)
             {
                 Log.Error($ "EnQueenObj:key={key},value={value}" , e);
                 return  false ;
             }
         }
 
         public  static  T DeQueen<T>( string  key)
         {
             T result= default (T);
             try
             {
                 //從尾部取值
                 result = GetClient().RPop<T>(key);
                 return  result;
             }
             catch  (Exception e)
             {
                 Log.Error($ "DeQueen:key={key}" , e);
                 return  result;
             }
         }
 
         /// <summary>
         /// 設置hash值
         /// </summary>
         /// <param name="key"></param>
         /// <param name="field"></param>
         /// <param name="value"></param>
         /// <returns></returns>
         public  static  bool  SetHash( string  key,  string  field, string  value)
         {
             try
             {
                 GetClient().HSet(key, field, value);
                 return  true ;
             }
             catch  (Exception e)
             {
                 Log.Error($ "SetHash:key={key},value={value}" , e);
                 return  false ;
             }
         }
 
         /// <summary>
         /// 根據表名,鍵名,獲取hash值
         /// </summary>
         /// <param name="key">表名</param>
         /// <param name="field">鍵名</param>
         /// <returns></returns>
         public  static  string  GetHash( string  key, string  field)
         {
             string  result =  "" ;
             try
             {
           
                 result = GetClient().HGet(key,field);
                 return  result;
             }
             catch  (Exception e)
             {
                 Log.Error($ "GetHash:key={key}" , e);
                 return  result;
             }
         }
 
         /// <summary>
         /// 獲取指定key中所有字段
         /// </summary>
         /// <param name="key"></param>
         /// <returns></returns>
         public  static  Dictionary< string , string > GetHashAll( string  key)
         {
             try
             {
 
                 var  result = GetClient().HGetAll(key);
                 return  result;
             }
             catch  (Exception e)
             {
                 Log.Error($ "GetHash:key={key}" , e);
                 return  new  Dictionary< string string >();
             }
         }
 
         /// <summary>
         /// 根據表名,鍵名,刪除hash值
         /// </summary>
         /// <param name="key">表名</param>
         /// <param name="field">鍵名</param>
         /// <returns></returns>
         public  static  long  DeleteHash( string  key,  string  field)
         {
             long  result = 0;
             try
             {
                 result = GetClient().HDel(key, field);
                 return  result;
             }
             catch  (Exception e)
             {
                 Log.Error($ "GetHash:key={key}" , e);
                 return  result;
             }
         }
 
 
         //private object deleteCache( Method method, Object[] args)
         //{
         //    object flag = false;
 
         //    String fieldkey = parseKey(method, args);
         //    try
         //    {
         //        if (fieldkey.equals(""))
         //        {
         //            cacheClient.del(cache.key());
         //        }
         //        else
         //        {
         //            cacheClient.hdel(cache.key(), fieldkey);
         //        }
         //        flag = true;
         //    }
         //    catch (Exception e)
         //    {
         //        //System.out.println(e.getMessage());
         //        flag = false;
         //    }
         //    return flag;
         //}
 
 
         /**
          * 獲取值field
          * @param key
          * @param method
          * @param args
          * @return
          * @throws Exception
          */
         //        public string parseKey(Method method, Object[] args)
         //        {
         //            string fieldKey = "";
         //            for (int i = 0; i < method.getParameters().length; i++)
         //            {
         //                Parameter p = method.getParameters()[i];
         //                FieldAnnotation f = p.getAnnotation(FieldAnnotation.class);
         //          if(f!=null) {
         //              fieldKey+=args[i].toString()+":";
         //          }else {
         //              FieldOnlyKeyAnnotation fo = p.getAnnotation(FieldOnlyKeyAnnotation.class);
         //              if(fo != null) {
         //                  fieldKey+=args[i].toString();
         //}
         //          }
         //      }
 
         //      return fieldKey;
         //    }
     }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM