Example #1
0
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <typeparam name="TMessage">消息的类型</typeparam>
        /// <param name="messageBus">消息总线</param>
        /// <param name="message">消息</param>
        public static void Send <TMessage>(this IMessageBus messageBus, TMessage message)
        {
            Throws.ArgumentNullException(messageBus, nameof(messageBus));
            var queue = typeof(TMessage).Name;

            messageBus.Send(queue, message);
        }
Example #2
0
        /// <summary>
        /// 锁住资源,并在操作执行完或者过期时间后释放锁
        /// </summary>
        /// <param name="name">锁的名字</param>
        /// <param name="action">执行的操作</param>
        /// <param name="timeout">锁定时间,单位秒</param>
        /// <param name="throwIfLockFail">如果锁定失败则抛出异常</param>
        public void Lock(
            string name,
            Action action,
            int timeout          = 60,
            bool throwIfLockFail = true)
        {
            Throws.ArgumentNullException(name, nameof(name));
            Throws.ArgumentNullException(action, nameof(action));

            var lockObject = new LockObject(name);

            if (this._distributedCache.Lock(lockObject, TimeSpan.FromSeconds(timeout)))
            {
                try
                {
                    action();
                }
                finally
                {
                    this._distributedCache.UnLock(lockObject);
                }
            }
            else
            {
                if (throwIfLockFail)
                {
                    throw new TipException("请求的资源处于繁忙中,请稍后重试。");
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public IList <string> HashGetKeys(CacheKey key)
        {
            Throws.ArgumentNullException(key, nameof(key));
            var hashTable = GetHashTable(key);

            return(hashTable.Keys.ToList());
        }
Example #4
0
        /// <summary>
        /// 接收一个消息
        /// </summary>
        /// <typeparam name="TMessage">消息的类型</typeparam>
        /// <param name="messageBus">消息总线</param>
        /// <param name="callback">回调函数</param>
        public static void Receive <TMessage>(this IMessageBus messageBus, Func <TMessage, bool> callback)
        {
            Throws.ArgumentNullException(messageBus, nameof(messageBus));
            var queue = typeof(TMessage).Name;

            messageBus.Receive(queue, callback);
        }
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TCache"></typeparam>
        /// <param name="key"></param>
        /// <param name="hashField"></param>
        /// <param name="hashValue"></param>
        /// <returns></returns>
        public bool HashSet <TCache>(CacheKey key, string hashField, TCache hashValue)
        {
            Throws.ArgumentNullException(hashField, nameof(hashField));
            Throws.ArgumentNullException(hashValue, nameof(hashValue));
            var hashTable = GetHashTable(key);

            hashTable.Add(hashField, hashValue);
            return(true);
        }
 /// <summary>
 ///
 /// </summary>
 /// <param name="lockObject"></param>
 /// <param name="expiry"></param>
 /// <returns></returns>
 public bool Lock(LockObject lockObject, TimeSpan expiry)
 {
     Throws.ArgumentNullException(lockObject, nameof(lockObject));
     if (this._memoryCache.Get(lockObject.Key) != null)
     {
         return(false);
     }
     return(this._memoryCache.Set(lockObject.Key, lockObject.Value, expiry));
 }
Example #7
0
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name='options'></param>
        public AliyunIDCardClient(AliyunOCROptions options)
        {
            Throws.ArgumentNullException(options, nameof(options));
            Throws.ArgumentNullException(options.Url, nameof(options.Url));
            Throws.ArgumentNullException(options.Appcode, nameof(options.Appcode));
            Throws.ArgumentNullException(options.HttpClient, nameof(options.HttpClient));

            this._options = options;
        }
Example #8
0
 /// <summary>
 /// 获取字符串
 /// </summary>
 /// <param name="bytes">原始byte数组</param>
 /// <param name="encoding">编码格式,默认采用UTF8编码</param>
 /// <returns></returns>
 public string GetString(byte[] bytes, Encoding encoding = null)
 {
     Throws.ArgumentNullException(bytes, nameof(bytes));
     if (encoding == null)
     {
         encoding = DefaultEncoding;
     }
     return(encoding.GetString(bytes));
 }
Example #9
0
 /// <summary>
 /// 构造key
 /// </summary>
 /// <param name="key">动态参数的key</param>
 /// <param name="value">动态参数的value</param>
 /// <returns>当前CacheKey自身</returns>
 protected void SetParams(string key, string value)
 {
     Throws.ArgumentNullException(key, nameof(key));
     Throws.ArgumentNullException(value, nameof(value));
     this._params = new Dictionary <string, string>
     {
         [key] = value
     };
 }
Example #10
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="contentType">Content-Type</param>
 /// <param name="fileName">文件名(包含扩展名)</param>
 /// <param name="fileBytes">文件的原始字节</param>
 public DownloadResult(string contentType, string fileName, byte[] fileBytes)
 {
     Throws.ArgumentNullException(contentType, nameof(contentType));
     Throws.ArgumentNullException(fileName, nameof(fileName));
     Throws.ArgumentNullException(fileBytes, nameof(fileBytes));
     this._contentType = contentType;
     this._fileName    = fileName;
     this._fileBytes   = fileBytes;
 }
Example #11
0
 /// <summary>
 /// base64字符串转bytes
 /// </summary>
 /// <param name="base64">base64字符串</param>
 /// <returns>原始bytes数组</returns>
 public byte[] GetBytesFromBase64(string base64)
 {
     Throws.ArgumentNullException(base64, nameof(base64));
     if (base64.IndexOf('-') != -1)
     {
         base64 = base64.Replace('-', '+').Replace('_', '/');
     }
     return(Convert.FromBase64String(base64));
 }
Example #12
0
        /// <summary>
        /// 获取SHA1
        /// </summary>
        /// <param name="value">原始字符串</param>
        /// <param name="encoding">字符串编码,默认UTF8。</param>
        /// <param name="lowerCase">小写,默认false。</param>
        /// <returns>SHA1的16进制字符串,40位</returns>
        // ReSharper disable once InconsistentNaming
        public static string GetSHA1(this string value, Encoding encoding = null, bool lowerCase = false)
        {
            Throws.ArgumentNullException(value, nameof(value));

            return(value
                   .GetBytes(encoding)
                   .GetSHA1()
                   .GetHex(false, lowerCase));
        }
Example #13
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TCache"></typeparam>
        /// <param name="key"></param>
        /// <param name="hashField"></param>
        /// <param name="hashValue"></param>
        /// <returns></returns>
        public async Task <bool> HashSetAsync <TCache>(CacheKey key, string hashField, TCache hashValue)
        {
            Throws.ArgumentNullException(key, nameof(key));
            Throws.ArgumentNullException(hashField, nameof(hashField));
            Throws.ArgumentNullException(hashValue, nameof(hashValue));

            await this.ConnectAsync();

            return(await this._db.HashSetAsync(key.ToRedisKey(), hashField, hashValue.ToRedisValue()));
        }
Example #14
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TCache"></typeparam>
        /// <param name="key"></param>
        /// <param name="hashField"></param>
        /// <param name="hashValue"></param>
        /// <returns></returns>
        public bool HashSet <TCache>(CacheKey key, string hashField, TCache hashValue)
        {
            Throws.ArgumentNullException(key, nameof(key));
            Throws.ArgumentNullException(hashField, nameof(hashField));
            Throws.ArgumentNullException(hashValue, nameof(hashValue));

            this.Connect();

            return(this._db.HashSet(key.ToRedisKey(), hashField, hashValue.ToRedisValue()));
        }
Example #15
0
        /// <summary>
        /// 字符串转bytes
        /// </summary>
        /// <param name="value">原始字符串</param>
        /// <param name="encoding">编码格式,默认UTF8。</param>
        /// <returns></returns>
        public byte[] GetBytes(string value, Encoding encoding = null)
        {
            Throws.ArgumentNullException(value, nameof(value));

            if (encoding == null)
            {
                encoding = DefaultEncoding;
            }
            return(encoding.GetBytes(value));
        }
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TCache"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public IList <TCache> HashGetValues <TCache>(CacheKey key)
        {
            Throws.ArgumentNullException(key, nameof(key));
            var hashTable = GetHashTable(key);

            return(hashTable
                   .Values
                   .Select(value => (TCache)value)
                   .ToList());
        }
Example #17
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TCache"></typeparam>
        /// <param name="key"></param>
        /// <param name="hashField"></param>
        /// <returns></returns>
        public async Task <TCache> HashGetValueAsync <TCache>(CacheKey key, string hashField)
        {
            Throws.ArgumentNullException(key, nameof(key));
            Throws.ArgumentNullException(hashField, nameof(hashField));

            await this.ConnectAsync();

            var redisValue = await this._db.HashGetAsync(key.ToRedisKey(), hashField);

            return(redisValue.JsonToObject <TCache>());
        }
Example #18
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TCache"></typeparam>
        /// <param name="key"></param>
        /// <param name="hashField"></param>
        /// <returns></returns>
        public TCache HashGetValue <TCache>(CacheKey key, string hashField)
        {
            Throws.ArgumentNullException(key, nameof(key));
            Throws.ArgumentNullException(hashField, nameof(hashField));

            this.Connect();

            var redisValue = this._db.HashGet(key.ToRedisKey(), hashField);

            return(redisValue.JsonToObject <TCache>());
        }
Example #19
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TCache"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public async Task <IList <TCache> > HashGetValuesAsync <TCache>(CacheKey key)
        {
            Throws.ArgumentNullException(key, nameof(key));

            await this.ConnectAsync();

            var redisValues = await this._db.HashValuesAsync(key.ToRedisKey());

            return(redisValues
                   .Select(value => value.JsonToObject <TCache>())
                   .ToList());
        }
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TCache"></typeparam>
        /// <param name="key"></param>
        /// <param name="hashField"></param>
        /// <returns></returns>
        public TCache HashGetValue <TCache>(CacheKey key, string hashField)
        {
            Throws.ArgumentNullException(key, nameof(key));
            Throws.ArgumentNullException(hashField, nameof(hashField));
            var hashTable = GetHashTable(key);

            if (hashTable.ContainsKey(hashField))
            {
                return((TCache)hashTable[hashField]);
            }
            return(default(TCache));
        }
Example #21
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public IList <string> HashGetKeys(CacheKey key)
        {
            Throws.ArgumentNullException(key, nameof(key));

            this.Connect();

            var redisKeys = this._db.HashKeys(key.ToRedisKey());

            return(redisKeys
                   .Select(value => (string)value)
                   .ToList());
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="key"></param>
        /// <param name="hashFields"></param>
        /// <returns></returns>
        public bool HashDelete(CacheKey key, params string[] hashFields)
        {
            Throws.ArgumentNullException(key, nameof(key));
            Throws.ArgumentNullException(hashFields, nameof(hashFields));
            var hashTable = GetHashTable(key);

            foreach (var hashField in hashFields)
            {
                hashTable.Remove(hashField);
            }
            return(true);
        }
Example #23
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TCache"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public IList <TCache> HashGetValues <TCache>(CacheKey key)
        {
            Throws.ArgumentNullException(key, nameof(key));

            this.Connect();

            var redisValues = this._db.HashValues(key.ToRedisKey());

            return(redisValues
                   .Select(value => value.JsonToObject <TCache>())
                   .ToList());
        }
Example #24
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public async Task <IList <string> > HashGetKeysAsync(CacheKey key)
        {
            Throws.ArgumentNullException(key, nameof(key));

            await this.ConnectAsync();

            var redisKeys = await this._db.HashKeysAsync(key.ToRedisKey());

            return(redisKeys
                   .Select(value => (string)value)
                   .ToList());
        }
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TCache"></typeparam>
        /// <param name="key"></param>
        /// <param name="hashItems"></param>
        /// <returns></returns>
        public bool HashSet <TCache>(CacheKey key, IReadOnlyDictionary <string, TCache> hashItems)
        {
            Throws.ArgumentNullException(key, nameof(key));
            Throws.ArgumentNullException(hashItems, nameof(hashItems));
            var hashTable = GetHashTable(key);

            foreach (var hashItem in hashItems)
            {
                hashTable.Add(hashItem.Key, hashItem.Value);
            }
            return(true);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public long Decrement(CacheKey key, long value = 1)
        {
            Throws.ArgumentNullException(key, nameof(key));

            var cacheValue = this._memoryCache.Get <long>(key);

            cacheValue = cacheValue - value;

            this._memoryCache.Set(key, cacheValue);

            return(cacheValue);
        }
Example #27
0
        /// <summary>
        /// 接收一个消息
        /// </summary>
        /// <typeparam name="TMessage"></typeparam>
        /// <param name="queue"></param>
        /// <param name="callback"></param>
        public void Receive <TMessage>(string queue, Func <TMessage, bool> callback)
        {
            Throws.ArgumentNullException(queue, nameof(queue));
            Throws.ArgumentNullException(callback, nameof(callback));
            var channel = this.CreateChannel();

            //声明队列
            channel.QueueDeclare(queue, true, false, false, null);
            //每次取一条消息
            channel.BasicQos(0, 1, false);

            Received(channel, queue, callback);
        }
Example #28
0
        /// <summary>
        /// 构造函数
        /// </summary>
        public TencentIDCardClient(TencentOCROptions options, IClock clock)
        {
            Throws.ArgumentNullException(options, nameof(options));
            Throws.ArgumentNullException(options.Apiurl, nameof(options.Apiurl));
            Throws.ArgumentNullException(options.AppId, nameof(options.AppId));
            Throws.ArgumentNullException(options.SecretId, nameof(options.SecretId));
            Throws.ArgumentNullException(options.SecretKey, nameof(options.SecretKey));
            Throws.ArgumentNullException(options.Bucket, nameof(options.Bucket));
            Throws.ArgumentNullException(options.HttpClient, nameof(options.HttpClient));

            _options = options;
            _clock   = clock;
        }
Example #29
0
        /// <summary>
        /// 身份证识别
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task <IDCardResponse> DetectAsync(IDCardRequest request)
        {
            Throws.ArgumentNullException(request, nameof(request));
            Throws.ArgumentNullException(request.ImgUrl, nameof(request.ImgUrl));
            try
            {
                var httpClient = this._options.HttpClient;

                var httpRequest = BuildHttpRequestMessage(request);

                var response = await httpClient.RequestJsonAsync <TencentOCRResponse>(httpRequest);

                var    result     = response.Result_List.First();
                var    resultData = result.Data;
                var    validDate  = resultData.Valid_date.Default("").Split('-');
                string startDate  = null;
                string endDate    = null;
                if (validDate?.Length == 2)
                {
                    startDate = validDate[0];
                    endDate   = validDate[1];
                }
                return(new IDCardResponse
                {
                    Success = result.Code == 0,
                    Message = result.Message,
                    Result = new IDCard
                    {
                        //正面
                        Address = resultData.Address,
                        Birth = resultData.Birth,
                        Id = resultData.Id,
                        Name = resultData.Name,
                        Nation = resultData.Nation,
                        Gender = EnumObject.GetNullableEnumFromDescription <Gender>(resultData.Sex),
                        //反面
                        Authority = resultData.Authority,
                        StartDate = startDate,
                        EndDate = endDate
                    }
                });
            }
            catch (Exception ex)
            {
                return(new IDCardResponse
                {
                    Success = false,
                    Message = ex.Message
                });
            }
        }
Example #30
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="key"></param>
        /// <param name="hashFields"></param>
        /// <returns></returns>
        public async Task <bool> HashDeleteAsync(CacheKey key, params string[] hashFields)
        {
            Throws.ArgumentNullException(key, nameof(key));
            Throws.ArgumentNullException(hashFields, nameof(hashFields));

            await this.ConnectAsync();

            var redisHashFields = hashFields
                                  .Select(item => (RedisValue)item)
                                  .ToArray();

            await this._db.HashDeleteAsync(key.ToRedisKey(), redisHashFields);

            return(true);
        }