Ejemplo n.º 1
0
 public RES SetTraverse(string dictName, string search, Func <string, string, int, int, bool> callback)
 {
     try
     {
         var count    = (int)(this.db.SetLength(dictName) % int.MaxValue);
         var resQuery = this.db.SetScan(dictName, search, count);
         var index    = 0;
         foreach (var item in resQuery)
         {
             if (null != callback)
             {
                 var _continue = callback(dictName, item, count, index++);
                 if (!_continue)
                 {
                     break;
                 }
             }
             else
             {
                 break;
             }
         }
         return(RES.OK());
     }
     catch (Exception ex)
     {
         return(RES.FAIL(ex));
     }
 }
Ejemplo n.º 2
0
        public RES SortedSetTraverse(string dictName, string search, Func <string, string, double, int, int, bool> callback)
        {
            try
            {
                var resQuery = this.db.SortedSetScan(dictName, search, int.MaxValue);
                var count    = (int)resQuery.Count();

                var index = 0;
                foreach (var item in resQuery)
                {
                    if (null != callback)
                    {
                        var _continue = callback(dictName, item.Element.ToString(), item.Score, count, index++);
                        if (!_continue)
                        {
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                return(RES.OK());
            }
            catch (Exception ex)
            {
                return(RES.FAIL(ex));
            }
        }
Ejemplo n.º 3
0
        protected RES RemoveFlat <T>(T t) where T : Item, new()
        {
            try
            {
                const string prefix = "FLAT";
                if (null != t)
                {
                    ///展开存储
                    var properties    = t.GetType().GetProperties().ToList();
                    var classFullName = $"{prefix}:{t.GetType().FullName.Replace(".", ":")}";

                    var oldVal = REDIS.Current.DictGet($"{classFullName}", t.ItemID.ToString());

                    ///检测是否存在
                    var keys = properties.Where(p => p.CanWrite && p.CanWrite).Select(p => $"{classFullName}:{t.ItemID}:{p.Name}").ToArray();

                    var res = REDIS.Current.KeyRemove(keys);
                    return(res);
                }
                return(RES.FAIL("无合法参数"));
            }
            catch (Exception ex)
            {
                return(RES.FAIL(ex));
            }
        }
Ejemplo n.º 4
0
 public RES Dequeue <T>(string queueName)
 {
     try
     {
         if (!string.IsNullOrWhiteSpace(queueName))
         {
             var val = this.db.ListLeftPop(queueName);
             if (typeof(T) == typeof(string))
             {
                 return(RES.OK(val.ToString()));
             }
             else if (typeof(T).IsClass)
             {
                 var json = JSON.ToObject <T>(val);
                 return(RES.OK(json));
             }
             else
             {
                 return(RES.OK(val.Box()));
             }
         }
         return(RES.FAIL());
     }
     catch (Exception ex)
     {
         return(RES.FAIL(ex));
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        /// 情感分析
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public RES SentimentAnalysis(string input)
        {
            try
            {
                Credential cred = new Credential
                {
                    SecretId  = secretId,
                    SecretKey = secretKey
                };

                ClientProfile clientProfile = new ClientProfile();
                HttpProfile   httpProfile   = new HttpProfile();
                httpProfile.Endpoint      = ("nlp.ap-shanghai.tencentcloudapi.com");
                clientProfile.HttpProfile = httpProfile;

                NlpClient client             = new NlpClient(cred, "ap-guangzhou", clientProfile);
                SentimentAnalysisRequest req = new SentimentAnalysisRequest();
                string strParams             = JSON.ToJson(new { Text = input });
                req = SentimentAnalysisRequest.FromJsonString <SentimentAnalysisRequest>(strParams);
                SentimentAnalysisResponse resp = client.SentimentAnalysis(req).
                                                 ConfigureAwait(false).GetAwaiter().GetResult();
                return(RES.OK(resp));
            }
            catch (Exception ex)
            {
                return(RES.FAIL(ex.Message));
            }
        }
Ejemplo n.º 6
0
        public RES CacheSet(string key, object val, TimeSpan?expiry = null)
        {
            try
            {
                var res = false;
                if (val is string)
                {
                    res = this.db.StringSet(key, val as string, expiry);
                }
                else if (val is int)
                {
                    res = this.db.StringSet(key, (int)val, expiry);
                }
                else if (val is long)
                {
                    res = this.db.StringSet(key, (long)val, expiry);
                }
                else if (val is object)
                {
                    res = this.db.StringSet(key, JSON.ToJson(val), expiry);
                }

                return(RES.OK(res));
            }
            catch (Exception ex)
            {
                return(RES.FAIL(ex));
            }
        }
Ejemplo n.º 7
0
 public RES Execute(object target)
 {
     if (null != target)
     {
         var res = target.GetType().GetMethod(this.Method).Invoke(target, this.Param) as RES;
         return(res);
     }
     return(RES.FAIL("调用的对象为空"));
 }
Ejemplo n.º 8
0
 protected RES SyncToDB <T>(T t) where T : Item, new()
 {
     try
     {
         return(RES.OK());
     }
     catch (Exception ex)
     {
         return(RES.FAIL(ex));
     }
 }
Ejemplo n.º 9
0
 protected RES FinishCheckPoint <T>(T t) where T : Item, new()
 {
     try
     {
         return(RES.OK());
     }
     catch (Exception ex)
     {
         return(RES.FAIL(ex));
     }
 }
Ejemplo n.º 10
0
 public RES Stop()
 {
     try
     {
         return(RES.OK());
     }
     catch (Exception ex)
     {
         return(RES.FAIL());
     }
 }
Ejemplo n.º 11
0
 public RES KeyRemove(string key)
 {
     try
     {
         var val = this.db.KeyDelete(key);
         return((val) ? RES.OK(val) : RES.FAIL(val));
     }
     catch (Exception ex)
     {
         return(RES.FAIL(ex));
     }
 }
Ejemplo n.º 12
0
 public RES Execute(string serverId, string command, object[] paramArr)
 {
     try
     {
         var res = (RedisValue[])REDIS.redis.GetDatabase().Execute(command, paramArr);
         return(RES.OK(res.ToStringArray()));
     }
     catch (Exception ex)
     {
         return(RES.FAIL(ex));
     }
 }
Ejemplo n.º 13
0
 public RES QueryKeys(string match, int db = 0)
 {
     try
     {
         var keys = server.Keys(db, match, int.MaxValue).Select(p => p.ToString()).ToList();
         return(RES.OK(keys));
     }
     catch (Exception ex)
     {
         return(RES.FAIL(ex));
     }
 }
Ejemplo n.º 14
0
 public RES DictRemove(string dictName, string key)
 {
     try
     {
         var val = this.db.HashDelete(dictName, key);
         return((val) ? RES.OK(val) : RES.FAIL(val));
     }
     catch (Exception ex)
     {
         return(RES.FAIL(ex));
     }
 }
Ejemplo n.º 15
0
 public RES RemoveListItem(string listName, string item, int count = 0)
 {
     try
     {
         var list     = new List <string>();
         var resCount = this.db.ListRemove(listName, item, count);
         return((0 < resCount) ? RES.OK(resCount) : RES.FAIL($"{listName}\t{resCount}\t{item}"));
     }
     catch (Exception ex)
     {
         return(RES.FAIL(ex));
     }
 }
Ejemplo n.º 16
0
 public RES Remove <T>(T t) where T : Item, new()
 {
     try
     {
         this.RemoveFlat(t);
         this.RemoveFromList(t);
         this.RemoveSearchIndex(t);
         return(RES.OK());
     }
     catch (Exception ex)
     {
         return(RES.FAIL(ex));
     }
 }
Ejemplo n.º 17
0
 public RES Save <T>(T t) where T : Item, new()
 {
     try
     {
         ///记录要保存的数据
         this.SaveFlat(t);
         this.SaveToList(t);
         this.BuildSearchIndex(t);
         return(RES.OK());
     }
     catch (Exception ex)
     {
         return(RES.FAIL(ex));
     }
 }
Ejemplo n.º 18
0
        public RES DictAdd(string dictName, string key, object val)
        {
            try
            {
                if (val is string)
                {
                    this.db.HashSet(dictName, key, val as string);
                }
                else if (val.GetType() == typeof(DateTime))
                {
                    this.db.HashSet(dictName, key, val.ToString());
                }
                else if (val.GetType() == typeof(int))
                {
                    this.db.HashSet(dictName, key, (int)val);
                }
                else if (val.GetType() == typeof(long))
                {
                    this.db.HashSet(dictName, key, (long)val);
                }
                else if (val.GetType() == typeof(decimal))
                {
                    this.db.HashSet(dictName, key, Convert.ToDouble(val));
                }
                else if (val.GetType() == typeof(float))
                {
                    this.db.HashSet(dictName, key, (float)val);
                }
                else if (val.GetType() == typeof(double))
                {
                    this.db.HashSet(dictName, key, (double)val);
                }
                else if (val.GetType() == typeof(Guid))
                {
                    this.db.HashSet(dictName, key, val.ToString());
                }
                else if (val.GetType().IsClass)
                {
                    this.db.HashSet(dictName, key, JSON.ToJson(val));
                }

                return(RES.OK());
            }
            catch (Exception ex)
            {
                return(RES.FAIL(ex));
            }
        }
Ejemplo n.º 19
0
 public RES DictGet(string dictName, string key)
 {
     try
     {
         var val = this.db.HashGet(dictName, key);
         if (val.IsNullOrEmpty)
         {
             return(RES.FAIL(null));
         }
         return(RES.OK(val.ToString()));
     }
     catch (Exception ex)
     {
         return(RES.FAIL(ex));
     }
 }
Ejemplo n.º 20
0
 public RES Enqueue(string queueName, object val)
 {
     try
     {
         if (val is string)
         {
             this.db.ListRightPush(queueName, val as string);
         }
         else if (val.GetType() == typeof(DateTime))
         {
             this.db.ListRightPush(queueName, val.ToString());
         }
         else if (val.GetType() == typeof(int))
         {
             this.db.ListRightPush(queueName, (int)val);
         }
         else if (val.GetType() == typeof(long))
         {
             this.db.ListRightPush(queueName, (long)val);
         }
         else if (val.GetType() == typeof(decimal))
         {
             this.db.ListRightPush(queueName, (double)val);
         }
         else if (val.GetType() == typeof(float))
         {
             this.db.ListRightPush(queueName, (double)val);
         }
         else if (val.GetType() == typeof(double))
         {
             this.db.ListRightPush(queueName, (double)val);
         }
         else if (val.GetType() == typeof(Guid))
         {
             this.db.ListRightPush(queueName, val.ToString());
         }
         else if (val.GetType().IsClass)
         {
             this.db.ListRightPush(queueName, JSON.ToJson(val));
         }
         return(RES.OK());
     }
     catch (Exception ex)
     {
         return(RES.FAIL(ex));
     }
 }
Ejemplo n.º 21
0
 public RES KeyRemove(string[] keys)
 {
     try
     {
         var rKeys = new List <RedisKey>();
         keys.ToList().ForEach(p =>
         {
             rKeys.Add(p);
         });
         var val = this.db.KeyDelete(rKeys.ToArray());
         return((0 < val) ? RES.OK(val) : RES.FAIL(val));
     }
     catch (Exception ex)
     {
         return(RES.FAIL(ex));
     }
 }
Ejemplo n.º 22
0
 public RES SortedSetRemoveByScore(string setName, int start = int.MinValue, int stop = int.MaxValue)
 {
     try
     {
         var list = new List <string>();
         var res  = this.db.SortedSetRangeByScore(setName, start, stop).ToList();
         res.ForEach(p =>
         {
             list.Add(p.ToString());
         });
         return(RES.OK(list));
     }
     catch (Exception ex)
     {
         return(RES.FAIL(ex));
     }
 }
Ejemplo n.º 23
0
 public RES SortedSetScan(string setName, string search, int pageSize)
 {
     try
     {
         var list = new List <string>();
         var res  = this.db.SortedSetScan(setName, search, pageSize).ToList();
         res.ForEach(p =>
         {
             list.Add(p.Element.ToString());
         });
         return(RES.OK(list));
     }
     catch (Exception ex)
     {
         return(RES.FAIL(ex));
     }
 }
Ejemplo n.º 24
0
 public RES DictScan(string dictName, string search, int count)
 {
     try
     {
         var list = new List <string>();
         var res  = this.db.HashScan(dictName, search, count).ToList();
         res.ForEach(p =>
         {
             list.Add(p.Value.ToString());
         });
         return(RES.OK(list));
     }
     catch (Exception ex)
     {
         return(RES.FAIL(ex));
     }
 }
Ejemplo n.º 25
0
 public RES GetListLastItems(string listName, int start = -20, int end = -1)
 {
     try
     {
         var list  = new List <string>();
         var items = this.db.ListRange(listName, start, end);
         items.ToList().ForEach(p =>
         {
             list.Add(p);
         });
         return((0 < list.Count) ? RES.OK(list) : RES.FAIL());
     }
     catch (Exception ex)
     {
         return(RES.FAIL(ex));
     }
 }
Ejemplo n.º 26
0
        public RES SetAdd(string setName, object val)
        {
            if (!this.server.IsConnected)
            {
                return(RES.FAIL($"Redis {nameof(this.server.IsConnected)} {this.server.IsConnected}"));
            }

            if (val is string)
            {
                this.db.SetAdd(setName, val as string);
            }
            else if (val.GetType() == typeof(DateTime))
            {
                this.db.SetAdd(setName, val.ToString());
            }
            else if (val.GetType() == typeof(int))
            {
                this.db.SetAdd(setName, (int)val);
            }
            else if (val.GetType() == typeof(long))
            {
                this.db.SetAdd(setName, (long)val);
            }
            else if (val.GetType() == typeof(decimal))
            {
                this.db.SetAdd(setName, (double)val);
            }
            else if (val.GetType() == typeof(float))
            {
                this.db.SetAdd(setName, (double)val);
            }
            else if (val.GetType() == typeof(double))
            {
                this.db.SetAdd(setName, (double)val);
            }
            else if (val.GetType() == typeof(Guid))
            {
                this.db.SetAdd(setName, val.ToString());
            }
            else if (val.GetType().IsClass)
            {
                this.db.SetAdd(setName, JSON.ToJson(val));
            }
            return(RES.OK());
        }
Ejemplo n.º 27
0
 public RES SortedSetQuery(string setName, double start, double stop, bool isDesc, int pageIndex, int pageSize)
 {
     try
     {
         var list  = new List <string>();
         var order = isDesc ? Order.Descending : Order.Ascending;
         var res   = this.db.SortedSetRangeByScore(setName, start, stop, Exclude.None, order, (pageIndex * pageSize), pageSize).ToList();
         res.ForEach(p =>
         {
             list.Add(p.ToString());
         });
         return(RES.OK(list));
     }
     catch (Exception ex)
     {
         return(RES.FAIL(ex));
     }
 }
Ejemplo n.º 28
0
 protected RES RemoveFromList <T>(T t) where T : Item, new()
 {
     try
     {
         const string prefix = "LIST";
         if (null != t)
         {
             var classFullName = $"{prefix}:{t.GetType().FullName.Replace(".", ":")}";
             REDIS.Current.DictRemove($"{classFullName}", t.ItemID.ToString());
             this.SaveDeleteNotice(t);
         }
         return(RES.OK());
     }
     catch (Exception ex)
     {
         return(RES.FAIL(ex));
     }
 }
Ejemplo n.º 29
0
        public RES QueryItem <T>(T t) where T : Item, new()
        {
            try
            {
                var classFullName = $"{typeof(T).FullName.Replace(".", ":")}";
                var list          = new List <string>();
                if (null == t || Guid.Empty == t.ItemID)
                {
                    return(RES.FAIL());
                }

                var res = REDIS.Current.DictGet($"LIST:{classFullName}", t.ItemID.ToString());
                return(res);
            }
            catch (Exception ex)
            {
                return(RES.FAIL(ex));
            }
        }
Ejemplo n.º 30
0
 public RES SortedSetGetOnceByScore(string setName, int queryScore = 0, int tempScore = 0)
 {
     try
     {
         var list = new List <string>();
         var res  = this.db.SortedSetRangeByScore(setName, queryScore, queryScore, Exclude.None, Order.Descending, 0, 1).ToList();
         if (0 < res.Count)
         {
             var data = res.First().ToString();
             var res2 = this.db.SortedSetAdd(setName, data, tempScore);
             return(RES.OK(data));
         }
         return(RES.FAIL(res.Count));
     }
     catch (Exception ex)
     {
         return(RES.FAIL(ex));
     }
 }