因為Servicestack.Redies免費每天只能呼叫6000次,所以找了個免費的能在.NETcore使用的外掛CSRedisCore,封裝了一下。
redies訂閱模式的呼叫:RedisCoreHelper.Sub(“quote”, action);
public void action(string message)
{
if (!message.IsNullOrEmpty() && !"null".Equals(message))
{
//dosomething
}
else
{
//Thread.Sleep(200);
}
}
封裝如下:
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;
}
}
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;
// }
}
}