csredis帮助文档,转载请注明
1.内涵lua模糊查询
2.redis分区
3.批量查询
4.管道查询
using CSRedis;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MYHelper
{
///
/// Redis 助手
///
public class CsRedisHelper : RedisHelper
{
CSRedisClient redisManger = null;
CSRedisClient GetClient()
{
return redisManger;
}
public CsRedisHelper(IConfiguration configuration)
{
redisManger = new CSRedisClient(null, configuration.GetSection("RedisConnectionStrings").Get()); //Redis的连接字符串
}
internal object QueryKeys(string key)
{
try
{
var lau = @"local array = {" + key + "}" +
@" local t = { }" +
@" for iter, value in ipairs(array) do" +
@" local key = redis.call('keys', value);" +
@" if #key>0 then" +
@" table.insert(t,key[1])" +
@"end " +
@" end " +
// @" return t";
@" return redis.call('mget', unpack(t))";
object objs = GetClient().Eval(lau, "", "");
return objs;
}
catch (Exception)
{
return null;
}
}
internal object BatchFuzzyQuery(string key)
{
try
{
var lau = @"local array = {" + key + "}" +
@" local t = { }" +
@" for iter, value in ipairs(array) do" +
@" local key = redis.call('keys', '*_'..value..'_*');" +
@" if #key>0 then" +
@" table.insert(t,key[1])" +
@"end " +
@" end " +
// @" return t";
@" return redis.call('mget', unpack(t))";
object objs = GetClient().Eval(lau, "", "");
return objs;
}
catch (Exception)
{
return null;
}
}
internal object BatchFuzzyFilter(string key)
{
try
{
var lau = @"local array = {" + key + "}" +
@" local t = { }" +
@" for iter, value in ipairs(array) do" +
@" local key = redis.call('keys', value);" +
@" if #key>0 then" +
@" table.insert(t,key[1])" +
@"end " +
@" end " +
@" return redis.call('mget', unpack(t))";
object objs = GetClient().Eval(lau, "", "");
//T[] strs = new T[objs.Length];
//objs.CopyTo(strs, 0);
return objs;
}
catch (Exception)
{
throw;
return null;
}
}
internal object BatchFuzzy(string key)
{
try
{
var lau = @"local array = {" + key + "}" +
@" return redis.call('mget', unpack(array))";
object objs = GetClient().Eval(lau, "", "");
//T[] strs = new T[objs.Length];
//objs.CopyTo(strs, 0);
return objs;
}
catch (Exception)
{
throw;
return null;
}
}
internal object FuzzyQuery(string key)
{
try
{
var lau = $"local keys = redis.call('keys', '{key}');" +
@"return redis.call('mget', unpack(keys));";
object objs = GetClient().Eval(lau, "", "");
return objs;
}
catch (Exception e)
{
return null;
}
}
internal void StringSetTube(List> keyval)
{
try
{
var ccp = GetClient().StartPipe().Set(keyval[0].Key, keyval[0].Value);
for (int i = 1; i < keyval.Count; i++)
{
Tube(ccp, keyval[i]);
}
CSRedisClientPipe Tube(CSRedisClientPipe pipe, KeyValuePair kv)
{
pipe.Set(kv.Key, kv.Value);
return pipe;
}
ccp.EndPipe();
}
catch (Exception)
{
throw;
}
}
internal void StringSetM(object[] keyval)
{
try
{
GetClient().MSet(keyval);
}
catch (Exception)
{
throw;
}
}
internal void StringSet(string key, T val)
{
try
{
GetClient().Set(key, val);
}
catch (Exception)
{
throw;
}
}
internal T StringGet(string key)
{
try
{
return GetClient().Get(key);
}
catch (Exception)
{
return default(T);
}
}
internal T[] StringGetM(string[] keys)
{
try
{
return GetClient().MGet(keys);
}
catch (Exception)
{
return default(T[]);
}
}
internal object[] StringGetTube(string[] keys)
{
try
{
var ccp = GetClient().StartPipe().Get(keys[0]);
for (int i = 1; i < keys.Length; i++)
{
Tube(ccp, keys[i]);
}
CSRedisClientPipe Tube(CSRedisClientPipe pipe, string key)
{
pipe.Get(key);
return pipe;
}
var data = ccp.EndPipe();
return data;
}
catch (Exception)
{
return null;
}
}
///
/// TradeManageMessage 和 TradeManageMessage:MQ队列
///
///
public 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;
}
}
///
/// TradeManageMessage 和 TradeManageMessage:MQ队列
///
///
public bool EnQeenTradeManageMessage(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 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 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 void Sub(string key, Action action)
{
GetClient().Subscribe((key, msg => action(msg.Body)));
}
public 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 bool EnQueen(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 T DeQueen(string key)
{
T result = default(T);
try
{
//从尾部取值
result = GetClient().RPop(key);
return result;
}
catch (Exception e)
{
// Log.Error($"DeQueen:key={key}", e);
return result;
}
}
///
/// 设置hash值
///
///
///
///
///
public 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;
}
}
///
/// 根据表名,键名,获取hash值
///
/// 表名
/// 键名
///
public 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;
}
}
///
/// 获取指定key中所有字段
///
///
///
public Dictionary GetHashAll(string key)
{
try
{
var result = GetClient().HGetAll(key);
return result;
}
catch (Exception e)
{
// Log.Error($"GetHash:key={key}", e);
return new Dictionary();
}
}
///
/// 根据表名,键名,删除hash值
///
/// 表名
/// 键名
///
public 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;
}
}
}
}