Exemple #1
0
 public async Task ClearAsync(string key)
 {
     if (!await _client.ExistsAsync(key))
     {
         return;
     }
     await _client.DelAsync(key);
 }
Exemple #2
0
        /// <summary>
        /// 取出队列,全部处理,为空后退出
        /// </summary>
        /// <returns></returns>
        private async Task <bool> Read()
        {
            var id = client.RPopLPush(jobList, bakList);

            if (string.IsNullOrEmpty(id))
            {
                return(false);
            }

            var key   = $"msg:{Service.ServiceName}:{id}";
            var guard = $"guard:{Service.ServiceName}:{id}";

            if (!await client.SetNxAsync(guard, "Guard"))
            {
                await Task.Delay(RedisOption.Instance.MessageLockTime);

                return(true);
            }
            client.Expire(guard, RedisOption.Instance.MessageLockTime);

            var str = client.Get(key);

            if (string.IsNullOrEmpty(str))
            {
                logger.Warning(() => $"ReadList key empty.{key}");
                await client.DelAsync(key);

                await client.LRemAsync(bakList, 0, id);

                await client.DelAsync(guard);

                return(true);
            }
            IInlineMessage item;

            try
            {
                item = SmartSerializer.ToMessage(str);
            }
            catch (Exception ex)
            {
                logger.Warning(() => $"ReadList deserialize error.{ex.Message }.{key} =>{str}");
                await client.DelAsync(key);

                await client.LRemAsync(bakList, 0, id);

                await client.DelAsync(guard);

                return(true);
            }
            //item.Trace ??= TraceInfo.New(item.ID);
            item.Service = Service.ServiceName;

            _ = MessageProcessor.OnMessagePush(Service, item, true, null);

            return(true);
        }
Exemple #3
0
        public async Task <int> PurgeAllAgents()
        {
            var keys = csredis.Keys($"{prefix}*");

            await csredis.DelAsync(keys.Select(x => x.Substring(prefix.Length)).ToArray());

            return(keys.Count());
        }
Exemple #4
0
        public async Task RemoveAsync(string key, CancellationToken token = default(CancellationToken))
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            await _redisClient.DelAsync(key.Split('|'));

            // TODO: Error handling
        }
Exemple #5
0
        /// <summary>
        /// 异常守护
        /// </summary>
        private async Task Guard()
        {
            logger.Information("异常消息守卫已启动");
            using var client = new CSRedisClient(RedisOption.Instance.ConnectionString);
            //处理错误重新入列
            while (true)
            {
                var key = client.LPop(errList);
                if (string.IsNullOrEmpty(key))
                {
                    break;
                }

                logger.Debug(() => $"异常消息重新入列:{key}");
                client.LPush(jobList, key);
            }
            //非正常处理还原
            while (ZeroAppOption.Instance.IsAlive)
            {
                await Task.Delay(RedisOption.Instance.GuardCheckTime, token);

                try
                {
                    var key = await client.LPopAsync(bakList);

                    if (string.IsNullOrEmpty(key))
                    {
                        continue;
                    }

                    var guard = $"guard:{Service.ServiceName}:{key}";
                    if (await client.SetNxAsync(guard, "Guard"))
                    {
                        client.Expire(guard, RedisOption.Instance.MessageLockTime);
                        logger.Debug(() => $"超时消息重新入列:{key}");
                        await client.LPushAsync(jobList, key);

                        await client.DelAsync(guard);
                    }
                }
                catch (Exception ex)
                {
                    logger.Information(() => $"异常消息守卫错误.{ex.Message }");
                }
            }
        }
Exemple #6
0
 public async Task <long> GeoBatchDeleteAsync(string Key)
 {
     return(await client.DelAsync(Key));
 }
 public override Task ClearAsync()
 {
     return(_client.DelAsync(_redisKey));
 }
Exemple #8
0
 /// <summary>
 /// 删除指定的缓存项
 /// </summary>
 public void Delete(string key)
 {
     _csredis.DelAsync(key);
 }
Exemple #9
0
 /// <summary>
 /// 用于在 key 存在时删除 key
 /// </summary>
 /// <param name="key">缓存键</param>
 /// <returns></returns>
 public async Task <long> DeleteAsync(string key)
 {
     return(await _redisClient.DelAsync(key));
 }