Exemplo n.º 1
0
        public async Task <T> GetObjectSafeAsync <T>(CacheObjectsName name)
        {
            var result = default(T);

            try
            {
                var cache = await _cache.GetAsync(name.ToString());

                if (cache == null)
                {
                    _logger.LogInformation($"Cache missed for {name}");
                    return(default(T));
                }

                var obj = JsonConvert.DeserializeObject <T>(GetString(cache));

                return(obj);
            }
            catch (Exception exception)
            {
                _logger.LogError(GetHashCode(), exception, exception.Message);
            }

            return(result);
        }
Exemplo n.º 2
0
        public async Task SaveObjectSafeAsync(CacheObjectsName name, object value, DistributedCacheEntryOptions options = null)
        {
            try
            {
                var obj = JsonConvert.SerializeObject(value);

                if (options != null)
                {
                    await _cache.SetAsync(name.ToString(), GetBytes(obj), options);
                }
                else
                {
                    await _cache.SetAsync(name.ToString(), GetBytes(obj));
                }
            }
            catch (Exception exception)
            {
                _logger.LogError(GetHashCode(), exception, exception.Message);
            }
        }