Ejemplo n.º 1
0
        public async Task <Social> GetAsync(string culture)
        {
            var expiration = DateTime.Now.AddHours(CacheInHours).Ticks;

            int?headerId = await _cache.GetIntFromCacheAsync(CacheKey.SocialHeader);

            if (!headerId.HasValue)
            {
                var headerIdRecord = await _socialHeaderRepository
                                     .GetByDateAsync(_dateTimeProvider.Now);

                if (headerIdRecord == null)
                {
                    return(null);
                }

                if (headerIdRecord.NextStartDate.HasValue &&
                    headerIdRecord.NextStartDate.Value != default)
                {
                    expiration = Math.Min(expiration,
                                          headerIdRecord.NextStartDate.Value.Ticks);
                }

                await _cache.SaveToCacheAsync(CacheKey.SocialHeader,
                                              headerIdRecord.Id,
                                              TimeSpan.FromTicks(expiration));

                headerId = headerIdRecord.Id;
            }

            if (!headerId.HasValue)
            {
                return(null);
            }

            var languageId = await _languageService.GetLanguageIdAsync(culture);

            Social social = null;

            var cacheKey = GetCacheKey(CacheKey.Social, headerId.Value, languageId);

            var socialCache = await _cache.GetStringFromCache(cacheKey);

            if (!string.IsNullOrEmpty(socialCache))
            {
                try
                {
                    social = JsonSerializer.Deserialize <Social>(socialCache);
                }
                catch (JsonException jex)
                {
                    _logger.LogError("Unable to deserialize social object: {ErrorMessage}",
                                     jex.Message);
                }
            }

            if (social == null)
            {
                social = await _socialRepository.GetByHeaderLanguageAsync(headerId.Value, languageId);

                await _cache.SaveToCacheAsync(cacheKey,
                                              JsonSerializer.Serialize(social),
                                              TimeSpan.FromTicks(expiration));
            }

            return(social);
        }