예제 #1
0
        public static async Task <DateTime?> LoadItemsAsync <T>(CacheSettings settings, Func <Task <IEnumerable <T> > > loadDataAsync, Action <CachedContent <T> > parseItems, bool refreshForced = false)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }
            if (string.IsNullOrEmpty(settings.Key))
            {
                throw new ArgumentException("Cache key is required");
            }

            var dataInCache = await AppCache.GetItemsAsync <T>(settings.Key);

            if (dataInCache != null)
            {
                parseItems(dataInCache);
            }

            if (CanPerformLoad <T>(settings.NeedsNetwork) && (refreshForced || DataNeedToBeUpdated(dataInCache, settings.Expiration)))
            {
                dataInCache = dataInCache ?? new CachedContent <T>();

                dataInCache.Timestamp = DateTime.Now;
                dataInCache.Items     = await loadDataAsync();

                await AppCache.AddItemsAsync(settings.Key, dataInCache, settings.UseStorage);

                parseItems(dataInCache);
            }
            return(dataInCache?.Timestamp);
        }
예제 #2
0
        public static async Task <LoaderOutcome> LoadAsync <T>(LoaderSettings settings, Func <Task <IEnumerable <T> > > loadFunc, Action <IEnumerable <T> > parseItems) where T : SchemaBase
        {
            var outcome = new LoaderOutcome();

            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }
            if (loadFunc == null)
            {
                throw new ArgumentNullException("loadFunc");
            }
            if (parseItems == null)
            {
                throw new ArgumentNullException("parseItems");
            }

            CachedContent <T> dataInCache = null;

            if (!string.IsNullOrEmpty(settings.CacheKey))
            {
                dataInCache = await AppCache.GetItemsAsync <T>(settings.CacheKey);

                if (dataInCache != null)
                {
                    parseItems(dataInCache.Items);
                }
            }

            if (CanPerformLoad <T>(settings.NeedsNetwork) && (settings.ForceRefresh || DataNeedToBeUpdated(dataInCache, settings.CacheExpiration)))
            {
                dataInCache = dataInCache ?? new CachedContent <T>();

                outcome.IsFreshData   = true;
                dataInCache.Timestamp = DateTime.Now;
                dataInCache.Items     = await loadFunc();

                if (!string.IsNullOrEmpty(settings.CacheKey))
                {
                    await AppCache.AddItemsAsync(settings.CacheKey, dataInCache, settings.UseStorage);
                }

                parseItems(dataInCache.Items);
            }
            outcome.Timestamp = dataInCache?.Timestamp;
            return(outcome);
        }