Exemplo n.º 1
0
        public static async Task <DataSourceContent <T> > GetItemsAsync <T>(string key) where T : BindableSchemaBase
        {
            string json = null;

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

                _memoryCache[key] = json;
            }
            if (!String.IsNullOrEmpty(json))
            {
                try
                {
                    DataSourceContent <T> records = JsonConvert.DeserializeObject <DataSourceContent <T> >(json);
                    return(records);
                }
                catch (Exception ex)
                {
                    AppLogs.WriteError("AppCache.GetItems", ex);
                }
            }
            return(null);
        }
Exemplo n.º 2
0
        internal async Task <DataSourceContent <T> > UpdateCache()
        {
            var freshData = new DataSourceContent <T>()
            {
                TimeStamp = DateTime.Now,
                Items     = await LoadDataAsync()
            };

            await AppCache.AddItemsAsync(CacheKey, freshData);

            return(freshData);
        }
Exemplo n.º 3
0
        public static async Task AddItemsAsync <T>(string key, DataSourceContent <T> data) where T : BindableSchemaBase
        {
            try
            {
                string json = JsonConvert.SerializeObject(data);
                await UserStorage.WriteText(key, json);

                if (_memoryCache.ContainsKey(key))
                {
                    _memoryCache[key] = json;
                }
                else
                {
                    _memoryCache.Add(key, json);
                }
            }
            catch (Exception ex)
            {
                AppLogs.WriteError("AppCache.AddItems", ex);
            }
        }
Exemplo n.º 4
0
        public async Task <DateTime> LoadDataAsync(ObservableCollection <T> viewItems, bool forceRefresh)
        {
            DateTime timeStamp = DateTime.Now;

            if (HasStaticData)
            {
                viewItems.AddRangeUnique(await LoadDataAsync());
            }
            else
            {
                var dataInCache = await AppCache.GetItemsAsync <T>(CacheKey);

                if (dataInCache != null)
                {
                    timeStamp = dataInCache.TimeStamp;

                    viewItems.AddRangeUnique(dataInCache.Items);
                }

                if (NetworkInterface.GetIsNetworkAvailable() && DataNeedToBeUpdated(forceRefresh, dataInCache))
                {
                    var freshData = new DataSourceContent <T>()
                    {
                        TimeStamp = DateTime.Now,
                        Items     = await LoadDataAsync()
                    };

                    timeStamp = freshData.TimeStamp;

                    viewItems.AddRangeUnique(freshData.Items);

                    await AppCache.AddItemsAsync(CacheKey, freshData);
                }
            }
            return(timeStamp);
        }
Exemplo n.º 5
0
 private bool DataNeedToBeUpdated(bool forceRefresh, DataSourceContent <T> dataInCache)
 {
     return(dataInCache == null || forceRefresh || IsContentExpirated(dataInCache.TimeStamp));
 }