.NETcore使用CSRedisCore操作Redis
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了.NETcore使用CSRedisCore操作Redis,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含10227字,纯文字阅读大概需要15分钟。
内容图文
![.NETcore使用CSRedisCore操作Redis](/upload/InfoBanner/zyjiaocheng/876/fac857274ce1415e8f703965dc23f749.jpg)
因为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;
???????? //??? }
???? }
}
|
内容总结
以上是互联网集市为您收集整理的.NETcore使用CSRedisCore操作Redis全部内容,希望文章能够帮你解决.NETcore使用CSRedisCore操作Redis所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。