示例#1
0
        /// <summary>
        /// Cache tokens
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">key for token</param>
        /// <param name="days">expiration duration</param>
        /// <returns></returns>
        public async Task <T> CacheData <T>(CacheDataKey key, T data, int days)
        {
            try
            {
                return(await Task.Run <T>(() =>
                {
                    var json = data == null ? String.Empty : JsonConvert.SerializeObject(data);

                    if (Barrel.Current.IsExpired(key.ToString()))
                    {
                        Barrel.Current.EmptyExpired();
                    }

                    Barrel.Current.Add(key.ToString(), json, TimeSpan.FromDays(days));

                    if (string.IsNullOrWhiteSpace(json))
                    {
                        return default(T);
                    }

                    var settings = new JsonSerializerSettings()
                    {
                        NullValueHandling = NullValueHandling.Ignore,
                    };

                    return JsonConvert.DeserializeObject <T>(json, settings);
                }));
            }
            catch (SystemException ex)
            {
                Debug.WriteLine($"Cache Token Error: {ex}");
                throw (ex);
            }
        }
示例#2
0
        /// <summary>
        /// Retrieves the cached token
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">Token's key</param>
        /// <returns></returns>
        public async Task <T> GetData <T>(CacheDataKey key)
        {
            try
            {
                return(await Task.Run <T>(() =>
                {
                    var json = String.Empty;

                    //check cache if it is not expired and a refresh is not forced
                    if (!Barrel.Current.IsExpired(key.ToString()))
                    {
                        json = Barrel.Current.Get <string>(key.ToString());
                    }

                    if (string.IsNullOrWhiteSpace(json))
                    {
                        return default(T);
                    }

                    var settings = new JsonSerializerSettings()
                    {
                        NullValueHandling = NullValueHandling.Ignore,
                    };

                    return JsonConvert.DeserializeObject <T>(json, settings);
                }));
            }
            catch (SystemException ex)
            {
                Debug.WriteLine($"Cache Token Error: {ex}");
                throw (ex);
            }
        }
示例#3
0
 /// <summary>
 /// Clears the cache of a specific key
 /// </summary>
 /// <param name="key">The key the cache was stored with usually the url the data was pulled from</param>
 /// <returns></returns>
 public async Task EmptyCache(CacheDataKey key)
 {
     try
     {
         await Task.Run(() => Barrel.Current.Empty(key.ToString()));
     }
     catch (SystemException ex)
     {
         Debug.WriteLine($"Cache Empty {key} Error: {ex}");
         throw (ex);
     }
 }