예제 #1
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));
            }
        }
예제 #2
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));
     }
 }
예제 #3
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));
            }
        }
예제 #4
0
        public static RES OK(object data, 数据类型 dataType, string msg = "成功")
        {
            var res = new RES();

            res.SUCCESS = true;
            if (null != data && dataType == 数据类型.ByteArray)
            {
                var dataStr = string.Empty;
                if (typeof(string) == data.GetType())
                {
                    res.DATA = Encoding.UTF8.GetBytes(data.ToString());
                }
                else
                {
                    res.DATA = Encoding.UTF8.GetBytes(JSON.ToJson(data));
                }
            }
            else if (null == data && dataType == 数据类型.ByteArray)
            {
                res.DATA = new byte[1];
            }
            res.MESSAGE  = msg;
            res.DataType = (int)dataType;
            return(res);
        }
예제 #5
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));
            }
        }
예제 #6
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));
     }
 }
예제 #7
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));
            }
        }
예제 #8
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("调用的对象为空"));
 }
예제 #9
0
        /// <summary>
        /// 失败结果
        /// </summary>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static RES FAIL(object data = null, string msg = "失败")
        {
            var res = new RES();

            res.SUCCESS = false;
            res.DATA    = data;
            res.MESSAGE = msg;
            return(res);
        }
예제 #10
0
        /// <summary>
        /// 成功结果
        /// </summary>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static RES OK(object data = null, string msg = "成功")
        {
            var res = new RES();

            res.SUCCESS = true;
            res.DATA    = data;
            res.MESSAGE = msg;
            return(res);
        }
예제 #11
0
 public RES RedisExecute(string connection, string command, object[] paramArr)
 {
     if (!string.IsNullOrWhiteSpace(connection))
     {
         return(RES.OK(REDIS.GetInst(0, connection).Execute(null, command, paramArr)));
     }
     else
     {
         return(RES.OK(REDIS.Current.Execute(null, command, paramArr)));
     }
 }
예제 #12
0
 public RES Stop()
 {
     try
     {
         return(RES.OK());
     }
     catch (Exception ex)
     {
         return(RES.FAIL());
     }
 }
예제 #13
0
 protected RES FinishCheckPoint <T>(T t) where T : Item, new()
 {
     try
     {
         return(RES.OK());
     }
     catch (Exception ex)
     {
         return(RES.FAIL(ex));
     }
 }
예제 #14
0
 protected RES SyncToDB <T>(T t) where T : Item, new()
 {
     try
     {
         return(RES.OK());
     }
     catch (Exception ex)
     {
         return(RES.FAIL(ex));
     }
 }
예제 #15
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));
     }
 }
예제 #16
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));
     }
 }
예제 #17
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));
     }
 }
예제 #18
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));
     }
 }
예제 #19
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));
     }
 }
예제 #20
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));
     }
 }
예제 #21
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));
     }
 }
예제 #22
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));
            }
        }
예제 #23
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));
     }
 }
예제 #24
0
        /// <summary>
        /// 失败结果
        /// </summary>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static RES FAIL(Exception ex)
        {
            var res = new RES();

            res.SUCCESS = false;
            if (ex is Exception)
            {
                res.DATA = $"{ex.Message}\t{((null == ex.InnerException) ? string.Empty : ex.InnerException.Message)}";
            }
            else
            {
                res.DATA = null;
            }
            res.MESSAGE = "发生异常";
            return(res);
        }
예제 #25
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));
     }
 }
예제 #26
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));
     }
 }
예제 #27
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));
     }
 }
예제 #28
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));
     }
 }
예제 #29
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));
     }
 }
예제 #30
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));
     }
 }