Esempio n. 1
0
        public async Task <object> Handle(NumeroCommand message, CancellationToken cancellationToken)
        {
            if (message.NumeroBase <= 0)
            {
                return new ValidationResultModel
                       {
                           IsValid = false,
                           Errors  =
                               new List <ValidationFailure> {
                               new ValidationFailure("NumeroBase", "O número base deve ser maior que zero.")
                           }
                       }
            }
            ;

            DivisorPrimoModel divisorPrimoModel = new DivisorPrimoModel(message.NumeroBase);

            divisorPrimoModel.TraceId = message.TraceId;

            try
            {
                if (!string.IsNullOrEmpty(message.TraceId))
                {
                    var numerosPrimosCache = await _redisClient.RetornaDivisorPrimo(message.NumeroBase);

                    if (numerosPrimosCache == null)
                    {
                        Task.Run(() => _numeroBusiness.CalculaDivisoresPrimos(message.NumeroBase, message.TraceId));
                    }
                    else
                    {
                        return(numerosPrimosCache);
                    }
                }
                else
                {
                    divisorPrimoModel = _numeroBusiness.RetornaDivisoresPrimos(message.NumeroBase);
                }
            }

            catch (Exception ex)
            {
                AddError(ex.Message);

                var errors = new List <ValidationFailure>();
                errors.Add(new ValidationFailure("create", ex.Message));

                return(new ValidationResultModel {
                    IsValid = false, Errors = errors
                });
            }

            return(new ValidationResultModel
            {
                ObjectResult = divisorPrimoModel
            });
        }
Esempio n. 2
0
        public async Task AdicionaDivisorPrimo(int chave, int valor, bool primo = true)
        {
            var existe = await VerificaExisteValor(chave, valor, primo);

            if (!existe.Item1)
            {
                DivisorPrimoModel divisorPrimoModel = existe.Item2;
                if (existe.Item2 == null)
                {
                    divisorPrimoModel = new DivisorPrimoModel(Convert.ToInt32(chave));
                }

                var cacheExpirationOptions = new MemoryCacheEntryOptions
                {
                    AbsoluteExpiration = DateTime.MaxValue,
                    Priority           = CacheItemPriority.High,
                    SlidingExpiration  = TimeSpan.MaxValue
                };

                _memoryCache.Set(chave, divisorPrimoModel, cacheExpirationOptions);
            }
        }