CsRedis帮助类

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
{
    /// <summary>
    /// Redis 助手
    /// </summary>
    public class CsRedisHelper : RedisHelper
    {

        CSRedisClient redisManger = null;

        CSRedisClient GetClient()
        {
            return redisManger;
        }

        public CsRedisHelper(IConfiguration configuration)
        {
            redisManger = new CSRedisClient(null, configuration.GetSection("RedisConnectionStrings").Get<string[]>()); //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<T>(List<KeyValuePair<string, T>> 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<bool> Tube(CSRedisClientPipe<bool> pipe, KeyValuePair<string, T> 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<T>(string key, T val)
        {
            try
            {
                GetClient().Set(key, val);
            }
            catch (Exception)
            {

                throw;
            }
        }

        internal T StringGet<T>(string key)
        {
            try
            {
                return GetClient().Get<T>(key);
            }
            catch (Exception)
            {
                return default(T);
            }
        }

        internal T[] StringGetM<T>(string[] keys)
        {
            try
            {
                return GetClient().MGet<T>(keys);
            }
            catch (Exception)
            {
                return default(T[]);
            }
        }

        internal object[] StringGetTube<T>(string[] keys)
        {
            try
            {
                var ccp = GetClient().StartPipe().Get<T>(keys[0]);
                for (int i = 1; i < keys.Length; i++)
                {
                    Tube(ccp, keys[i]);
                }
                CSRedisClientPipe<T> Tube(CSRedisClientPipe<T> pipe, string key)
                {
                    pipe.Get<T>(key);
                    return pipe;
                }

                var data = ccp.EndPipe();
                return data;

            }
            catch (Exception)
            {
                return null;
            }
        }



        /// <summary>

        /// TradeManageMessage 和 TradeManageMessage:MQ队列

        /// </summary>

        /// <returns></returns>

        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;

            }

        }

        /// <summary>

        /// TradeManageMessage 和 TradeManageMessage:MQ队列

        /// </summary>

        /// <returns></returns>

        public 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 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<string> 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<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 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 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 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 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 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;

            }

        }
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值