예제 #1
0
        public async Task <IEnumerable <CountryDto> > Handle(GetCountriesQuery request, CancellationToken cancellationToken)
        {
            var    countryDtos = new List <CountryDto>();
            var    cacheKey    = nameof(GetCountriesQuery);
            string serializedCountries;

            var redisCountries = await _distributedCache.GetAsync(cacheKey);

            if (redisCountries != null)
            {
                serializedCountries = Encoding.UTF8.GetString(redisCountries);
                countryDtos         = JsonConvert.DeserializeObject <List <CountryDto> >(serializedCountries);
            }
            else
            {
                var countries = await _context.Countries.ToListAsync();

                countryDtos = countries.Select(c => new CountryDto
                {
                    Id       = c.Id,
                    SortName = c.SortName,
                    Name     = c.Name
                }).ToList();

                serializedCountries = JsonConvert.SerializeObject(countryDtos);
                redisCountries      = Encoding.UTF8.GetBytes(serializedCountries);

                //TODO: Need to move options to global of the system.
                var options = new DistributedCacheEntryOptions()
                              .SetAbsoluteExpiration(DateTime.Now.AddMinutes(10))
                              .SetSlidingExpiration(TimeSpan.FromMinutes(2));
                await _distributedCache.SetAsync(cacheKey, redisCountries, options);
            }
            return(countryDtos);
        }
예제 #2
0
        public async Task <IEnumerable <CountryDto> > Handle(GetCountriesQuery request, CancellationToken cancellationToken)
        {
            var countryDtos = new List <CountryDto>();

            countryDtos = await _redisCacheManager.GetAsync <List <CountryDto> >(nameof(GetCountriesQuery));

            if (countryDtos != null)
            {
                return(countryDtos);
            }

            var countries = await _unitOfWork.Countries.GetAllAsync();

            countryDtos = countries.Select(c => new CountryDto
            {
                Id       = c.Id,
                SortName = c.SortName,
                Name     = c.Name
            }).ToList();

            await _redisCacheManager.SetAsync <List <CountryDto> >(nameof(GetCountriesQuery), countryDtos);

            return(countryDtos);
        }