Пример #1
0
        public static async Task <CachedContent <T> > GetItemsAsync <T>(string key)
        {
            string json = null;

            if (_memoryCache.ContainsKey(key))
            {
                json = _memoryCache[key];
            }
            else
            {
                json = await UserStorage.ReadTextFromFile(key);

                _memoryCache[key] = json;
            }
            if (!String.IsNullOrEmpty(json))
            {
                try
                {
                    CachedContent <T> records = JsonConvert.DeserializeObject <CachedContent <T> >(json);
                    return(records);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);
                }
            }
            return(null);
        }
Пример #2
0
        public static async Task AddItemsAsync <T>(string key, CachedContent <T> data, bool useStorageCache)
        {
            try
            {
                string json = JsonConvert.SerializeObject(data);

                if (useStorageCache)
                {
                    await UserStorage.WriteText(key, json);
                }

                if (_memoryCache.ContainsKey(key))
                {
                    _memoryCache[key] = json;
                }
                else
                {
                    _memoryCache.Add(key, json);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }
Пример #3
0
 private static bool DataNeedToBeUpdated <T>(CachedContent <T> dataInCache, TimeSpan expiration)
 {
     return(dataInCache == null || (DateTime.Now - dataInCache.Timestamp > expiration));
 }
Пример #4
0
 public static async Task AddItemsAsync <T>(string key, CachedContent <T> data)
 {
     await AddItemsAsync <T>(key, data, true);
 }