Browse Source

同时消费测试redis

lcl 3 months ago
parent
commit
9ab0dc33b7
4 changed files with 243 additions and 0 deletions
  1. 1 0
      AppStart/StartHelper.cs
  2. 1 0
      Startup.cs
  3. 30 0
      Util/Queue/PosPushHelper.cs
  4. 211 0
      Util/RedisDbconnTest.cs

+ 1 - 0
AppStart/StartHelper.cs

@@ -25,6 +25,7 @@
             else
             {
                 PosPushHelper.Instance.Start(); //推送机具数据
+                // PosPushHelper.Instance.StartTest(); //推送机具数据
             }
         }
     }

+ 1 - 0
Startup.cs

@@ -61,6 +61,7 @@ namespace MySystem
 
             MySystemLib.SystemPublicFuction.appcheck = "success";
             RedisDbconn.csredis = new CSRedis.CSRedisClient(Configuration["Setting:RedisConnStr"]);
+            RedisDbconnTest.csredis = new CSRedis.CSRedisClient(Configuration["Setting:RedisConnStrTest"]);
         }
 
         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

+ 30 - 0
Util/Queue/PosPushHelper.cs

@@ -49,6 +49,36 @@ namespace MySystem
             }
         }
 
+        public void StartTest()//启动
+        {
+            Thread thread = new Thread(threadStartTest);
+            thread.IsBackground = true;
+            thread.Start();
+        }
+
+        private void threadStartTest()
+        {
+            while (true)
+            {
+                string content = RedisDbconnTest.Instance.RPop<string>("KxsPosDataQueue");
+                if (!string.IsNullOrEmpty(content))
+                {
+                    try
+                    {
+                        DoSomeThing(content);
+                    }
+                    catch (Exception ex)
+                    {
+                        LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + content + "\n" + ex, "推送机具数据异常");
+                    }
+                }
+                else
+                {
+                    Thread.Sleep(5000);
+                }
+            }
+        }
+
         //要执行的方法
         public void DoSomeThing(string content)
         {

+ 211 - 0
Util/RedisDbconnTest.cs

@@ -0,0 +1,211 @@
+using System.Collections.Generic;
+using Library;
+
+namespace MySystem
+{
+    public class RedisDbconnTest
+    {
+        public readonly static RedisDbconnTest Instance = new RedisDbconnTest();
+        public static CSRedis.CSRedisClient csredis;
+        private RedisDbconnTest()
+        {
+        }
+
+        #region 设置单个字段
+        public bool Set(string key, object value)
+        {
+            return csredis.Set(key, value);
+            // return false;
+        }
+        #endregion
+
+        #region 整数累加
+        public long AddInt(string key, long value = 1)
+        {
+            return csredis.IncrBy(key, value);
+            // return 0;
+        }
+        #endregion
+
+        #region 数字累加
+        public decimal AddNumber(string key, decimal value = 1)
+        {
+            return csredis.IncrByFloat(key, value);
+            // return 0;
+        }
+        #endregion
+
+        #region 获取单个字段
+        public T Get<T>(string key)
+        {
+            return csredis.Get<T>(key);
+        }
+        #endregion
+
+        #region 设置散列字段
+        public bool HSet(string key, string field, object value)
+        {
+            return csredis.HSet(key, field, value);
+            // return false;
+        }
+        #endregion
+
+        #region 散列整数累加
+        public long HAddInt(string key, string field, long value = 1)
+        {
+            return csredis.HIncrBy(key, field, value);
+            // return 0;
+        }
+        #endregion
+
+        #region 散列数字累加
+        public decimal HAddNumber(string key, string field, decimal value = 1)
+        {
+            return csredis.HIncrByFloat(key, field, value);
+            // return 0;
+        }
+        #endregion
+
+        #region 获取散列元素
+        public T HGet<T>(string key, string field)
+        {
+            return csredis.HGet<T>(key, field);
+        }
+        #endregion
+
+        #region 获取散列所有元素
+        public Dictionary<string, T> HGetAll<T>(string key)
+        {
+            return csredis.HGetAll<T>(key);
+        }
+        #endregion
+
+        #region 添加列表对象
+        public long AddList(string key, object value)
+        {
+            return csredis.LPush(key, value);
+            // return 0;
+        }
+        public long AddList(string key, object[] value)
+        {
+            return csredis.LPush(key, value);
+            // return 0;
+        }
+        public T RPop<T>(string key)
+        {
+            return csredis.RPop<T>(key);
+        }
+        #endregion
+
+        #region 添加集合对象
+        public long SAdd(string key, object value)
+        {
+            return csredis.SAdd(key, value);
+            // return 0;
+        }
+        public long SAdd(string key, object[] value)
+        {
+            return csredis.SAdd(key, value);
+            // return 0;
+        }
+        #endregion
+
+        #region 获取集合对象
+        public T[] SGetList<T>(string key)
+        {
+            return csredis.SMembers<T>(key);
+        }
+        #endregion
+
+        #region 修改列表对象
+        public bool SetList(string key, int index, object value)
+        {
+            long itemindex = csredis.LLen(key) - index - 1;
+            return csredis.LSet(key, itemindex, value);
+            // return false;
+        }
+        #endregion
+
+        #region 获取列表
+        public List<T> GetList<T>(string key, int pageNum = 1, int pageSize = 10)
+        {
+            int start = (pageNum - 1) * pageSize;
+            int end = start + pageSize - 1;
+            string[] list = csredis.LRange(key, start, end);
+            List<T> lists = new List<T>();
+            foreach (string record in list)
+            { 
+                lists.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<T>(record));
+            }
+            return lists;
+        }
+        #endregion
+
+        #region 添加排序列表对象
+        public long AddSort(string key, object value, decimal score)
+        {
+            return csredis.ZAdd(key, (score, value));
+            // return 0;
+        }
+        #endregion
+
+        #region 获取排序列表
+        public List<T> GetSort<T>(string key, int pageNum = 1, int pageSize = 10)
+        {
+            int start = (pageNum - 1) * pageSize;
+            int end = start + pageSize;
+            string[] list = csredis.ZRangeByScore(key, start, end);
+            List<T> lists = new List<T>();
+            foreach (string record in list)
+            { 
+                lists.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<T>(record));
+            }
+            return lists;
+        }
+        public List<T> GetSortDesc<T>(string key, int pageNum = 1, int pageSize = 10)
+        {
+            int start = (pageNum - 1) * pageSize;
+            int end = start + pageSize;
+            string[] list = csredis.ZRevRangeByScore(key, start, end);
+            List<T> lists = new List<T>();
+            foreach (string record in list)
+            { 
+                lists.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<T>(record));
+            }
+            return lists;
+        }
+        #endregion
+
+        public bool Remove(string key, long start, long end)
+        {
+            return csredis.LTrim(key, start, end);
+        }
+
+        public bool RemoveTop(string key, long count)
+        {
+            return RedisDbconn.Instance.Remove(key, count, RedisDbconn.Instance.Count(key) - 1);;
+        }
+
+        public long Count(string key)
+        {
+            return csredis.LLen(key);
+        }
+
+        public void Clear(string pattern)
+        {
+            string[] keys = csredis.Keys(pattern);
+            csredis.Del(keys);
+        }
+
+        public string[] GetKeys(string pattern)
+        { 
+            string[] keys = csredis.Keys(pattern);
+            return keys;
+        }
+
+        public void SetExpire(string key, int expire)
+        { 
+            csredis.Expire(key, expire); //秒为单位
+        }
+    }
+}