Exemplo n.º 1
0
        private async Task <string> GetPageContent(string url)
        {
            IWebRequestHelper webRequestHelper = ServiceLocator.Current.GetInstance <IWebRequestHelper>();
            IWebCache         webCache         = ServiceLocator.Current.GetInstance <IWebCache>();

            ICacheItem cacheItem = await webCache.Get(url, TimeSpan.FromDays(CacheDurationDays), k =>
            {
                return(webRequestHelper.GetDataAsync(url, null, null, 30000, null, new Dictionary <HttpRequestHeader, string>
                {
                    { HttpRequestHeader.Accept, "application/json" }
                }));
            });

            if (cacheItem == null)
            {
                return(null);
            }

            if (cacheItem.Data == null)
            {
                return(null);
            }

            return(cacheItem.GetUTF8());
        }
Exemplo n.º 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BlogController" /> class.
 /// </summary>
 /// <param name="blogRepository">The blog repository.</param>
 /// <param name="tempDataProvider">The temporary data provider</param>
 /// <param name="webCache">Web cache to clear when modifying posts</param>
 /// <param name="comments">Disqus comments service</param>
 public BlogController(IBlogRepository blogRepository, ITempDataProvider tempDataProvider, IWebCache webCache, IDisqusComments comments)
 {
     _blogRepository = blogRepository;
     _webCache       = webCache;
     _comments       = comments;
     // TODO: This shouldn't be required to be passed in the constructor - Can set it as a property.
     TempDataProvider = tempDataProvider;
 }
 protected BaseAsyncWebClient([NotNull] IWebCache cache)
 {
     if (cache == null)
     {
         throw new ArgumentNullException("cache");
     }
     _cache = cache;
 }
Exemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BlogController" /> class.
 /// </summary>
 /// <param name="blogRepository">The blog repository.</param>
 /// <param name="tempDataProvider">The temporary data provider</param>
 /// <param name="webCache">Web cache to clear when modifying posts</param>
 /// <param name="comments">Disqus comments service</param>
 public BlogController(IBlogRepository blogRepository, ITempDataProvider tempDataProvider, IWebCache webCache, IDisqusComments comments)
 {
     _blogRepository = blogRepository;
     _webCache = webCache;
     _comments = comments;
     // TODO: This shouldn't be required to be passed in the constructor - Can set it as a property.
     TempDataProvider = tempDataProvider;
 }
Exemplo n.º 5
0
 public DataUploader(
     IWebRequest webRequest,
     IWebCache webCache,
     IInternetConnectivity internetConnectivity)
 {
     _webRequest           = webRequest ?? throw new ArgumentNullException(nameof(webRequest));
     _webCache             = webCache ?? throw new ArgumentNullException(nameof(webCache));
     _internetConnectivity = internetConnectivity ?? throw new ArgumentNullException(nameof(internetConnectivity));
 }
Exemplo n.º 6
0
 private void UpdateSubPlaylistCache(Uri playlist)
 {
     if (_subPlaylistCache != null && !(_subPlaylistCache.WebReader.BaseAddress != playlist))
     {
         return;
     }
     _subPlaylistCache?.WebReader.Dispose();
     _subPlaylistCache = _webReader.CreateWebCache(playlist, ContentKind.Playlist);
 }
Exemplo n.º 7
0
 public OfflineUnsafeWebRequest(
     IWebRequest webRequest,
     IWebCache webCache,
     IInternetConnectivity internetConnectivity)
 {
     _webRequest           = webRequest;
     _webCache             = webCache;
     _internetConnectivity = internetConnectivity;
 }
 protected BaseAsyncWebClient([NotNull] IWebCache cache)
 {
     if (cache == null)
     {
         throw new ArgumentNullException("cache");
     }
     CallFactory = new RestfulCallFactory();
     _cache      = cache;
 }
Exemplo n.º 9
0
 public AsyncDataProvider([NotNull] IWebCache cache, [NotNull] UniversitiesCache universitiesCache) : base(cache)
 {
     if (universitiesCache == null)
     {
         throw new ArgumentNullException("universitiesCache");
     }
     _universitiesCache = universitiesCache;
     _universitiesCache.Load();
 }
Exemplo n.º 10
0
 private void UpdateSubPlaylistCache(Uri playlist)
 {
     if (this._subPlaylistCache != null && !(this._subPlaylistCache.WebReader.BaseAddress != playlist))
     {
         return;
     }
     if (null != this._subPlaylistCache)
     {
         this._subPlaylistCache.WebReader.Dispose();
     }
     this._subPlaylistCache = WebReaderExtensions.CreateWebCache(this._webReader, playlist, ContentKind.Playlist, (ContentType)null);
 }
 public static IWebRequest WithOffline(
     this IWebRequest webRequest,
     IWebCache webCache,
     IInternetConnectivity internetConnectivity)
 {
     return(new OfflineUnsafeWebRequest(
                new OfflineSafeWebRequest(
                    webRequest,
                    webCache,
                    internetConnectivity),
                webCache,
                internetConnectivity));
 }
 public CacheAbstraction(IWebCache iwebc)
 {
     _iwebCache = iwebc;
 }
Exemplo n.º 13
0
 public AsyncDataProvider([NotNull] IWebCache cache)
     : base(cache)
 {
 }
Exemplo n.º 14
0
 public CacheAbstraction(IWebCache webCache)
 {
     _iwebCache = webCache;
 }
Exemplo n.º 15
0
        void UpdateSubPlaylistCache(Uri playlist)
        {
            if (null == _subPlaylistCache || _subPlaylistCache.WebReader.BaseAddress != playlist)
            {
                if (null != _subPlaylistCache)
                    _subPlaylistCache.WebReader.Dispose();

                _subPlaylistCache = _webReader.CreateWebCache(playlist, ContentKind.Playlist, ContentType);
            }
        }
Exemplo n.º 16
0
 public QuizService(IWebCache webCache)
 {
     _webCache = webCache;
     LoadQuizzes();
 }
Exemplo n.º 17
0
 public CacheAbstraction(IWebCache iCache)
 {
     iWebCache = iCache;
 }
Exemplo n.º 18
0
        public Task <TCached> ReadAsync <TCached>(Uri uri, Func <Uri, byte[], TCached> factory, ContentKind contentKind, ContentType contentType, CancellationToken cancellationToken) where TCached : class
        {
            TaskCompletionSource <TCached> tcs = (TaskCompletionSource <TCached>)null;
            bool lockTaken = false;
            Dictionary <Uri, WebCacheManager.CacheEntry> dictionary;

            WebCacheManager.CacheEntry cacheEntry;
            try
            {
                Monitor.Enter((object)(dictionary = this._cache), ref lockTaken);
                if (this._cache.TryGetValue(uri, out cacheEntry))
                {
                    if (cacheEntry.ReadTask.IsCompleted && cacheEntry.Age > TimeSpan.FromSeconds(5.0))
                    {
                        tcs = new TaskCompletionSource <TCached>();
                        cacheEntry.ReadTask = (Task)tcs.Task;
                    }
                }
                else
                {
                    IWebCache webCache = WebReaderExtensions.CreateWebCache(this._webReader, uri, contentKind, contentType);
                    tcs        = new TaskCompletionSource <TCached>();
                    cacheEntry = new WebCacheManager.CacheEntry()
                    {
                        WebCache = webCache,
                        ReadTask = (Task)tcs.Task
                    };
                    this._cache[uri] = cacheEntry;
                }
            }
            finally
            {
                if (lockTaken)
                {
                    Monitor.Exit((object)dictionary);
                }
            }
            if (null == tcs)
            {
                return((Task <TCached>)cacheEntry.ReadTask);
            }
            Task <TCached> task = cacheEntry.WebCache.ReadAsync <TCached>(factory, cancellationToken, (WebResponse)null);

            task.ContinueWith((Action <Task <TCached> >)(t =>
            {
                cacheEntry.ResetTime();
                if (null != t.Exception)
                {
                    tcs.TrySetCanceled();
                }
                else if (t.IsCanceled)
                {
                    tcs.TrySetCanceled();
                }
                else
                {
                    tcs.TrySetResult(task.Result);
                }
            }), cancellationToken);
            return(tcs.Task);
        }
Exemplo n.º 19
0
 public AsyncWebClient([NotNull] IWebCache cache) : base(cache)
 {
 }