CsRedisヘルプクラス

13235 ワード

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;

            }

        }
    }
}