public async Task <UrlCacheInfo> Fetch(Uri uri, long freshnessTreshold)
        {
            _log.Log("Fetching for uri: " + uri.OriginalString);
            var realm = Realm.GetInstance();

            var needle   = uri.OriginalString;
            var cacheHit = realm.All <UrlCacheInfo>().Where(x => x.Url == needle);

            if (cacheHit.Count() != 0)
            {
                // Hit
                _log.Log("Cache hit");
                var hero = cacheHit.First();
                realm.Write(() => hero.LastAccessed = DateTime.Now.Ticks);

                if (ShouldInvalidate(hero, freshnessTreshold))
                {
                    // Cache needs refreshing
                    _log.Log("Refreshing cache");
                    await UpdateUrl(uri, hero);
                }

                return(hero);
            }
            else
            {
                // Nothing in cache, get it fresh
                return(await InsertUrl(uri));
            }
        }
Example #2
0
        private async Task <List <DreamsWebServiceDTO> > DreamsWebServiceDataFactory(string input)
        {
            var result = new List <DreamsWebServiceDTO>();

            try
            {
                result = await Task.Run(() => JsonConvert.DeserializeObject <List <DreamsWebServiceDTO> >(input));
            }
            catch (JsonException je)
            {
                _log.Log("Failed to deserialize", DreamsLogSeverityLevel.Error, je);
            }

            return(result);
        }