public async Task <IEnumerable <PlayerDTO> > GetPlayers(string team) { var key = $"{this.ToString()}/{nameof(GetPlayers)}/{team}"; if (!_barrel.IsExpired(key: key)) { return(_barrel.Get <IEnumerable <PlayerDTO> >(key: key)); } var data = await _playersService.GetPlayers(team); AddToCache(_barrel, key, data); return(data); }
/// <summary> /// Find element in file store /// </summary> /// <typeparam name="T">type element savind in file store</typeparam> /// <param name="nameMonkey">name with the save element in file store</param> /// <returns></returns> public ConfigurationItem <T> GetMonkey <T>(string nameMonkey) { if (file.Exists(nameMonkey)) { return(new ConfigurationItem <T>() { IsExpired = file.IsExpired(nameMonkey), Item = file.Get <T>(nameMonkey) }); } return(null); }
private async Task <string> GetJsonAsync(string resource, Xamarin.Essentials.NetworkAccess networkAccess, int cacheDuration = 24) { var cleanCacheKey = resource.CleanCacheKey(); //Try Get data from Cache var cachedData = _cacheBarrel.Get <string>(cleanCacheKey); if (cacheDuration > 0 && cachedData != null) { //If the cached data is still valid if (!_cacheBarrel.IsExpired(cleanCacheKey)) { return(cachedData); } } //Check for internet connection and return cached data if possible if (networkAccess != NetworkAccess.Internet) { if (cachedData != null) { return(cachedData); } else { throw new InternetConnectionException(); } } //No Cache Found, or Cached data was not required, or Internet connection is also available //Extract response from URI var response = await _httpClient.GetAsync(new Uri(_httpClient.BaseAddress, resource)); response.EnsureSuccessStatusCode(); //Read Response string json = await response.Content.ReadAsStringAsync(); //Save to Cache if required if (cacheDuration > 0) { try { _cacheBarrel.Add(cleanCacheKey, json, TimeSpan.FromHours(cacheDuration)); } catch { } } //Return the result return(json); }
public override User GetUser(string userPid) { User user = null; if (!_barrel.IsExpired(userPid)) { user = _barrel.Get <User>(userPid); } else { user = base.GetUser(userPid); _barrel.Add(userPid, user, TimeSpan.FromDays(_expiryLengthDays)); } return(user); }
/// <summary> /// WebAPIから県別のデータを取得する. /// </summary> /// <returns></returns> public async Task <IEnumerable <Prefecture> > GetPrefectures() { try { //キャッシュが存在して,有効期間内の場合はキャッシュから if (!_barrel.IsExpired(key: CachePrefecturesKey)) { await Task.Yield(); return(_barrel.Get <IEnumerable <Prefecture> >(key: CachePrefecturesKey)); } //インターネットに接続してれば取得 if (Connectivity.NetworkAccess == NetworkAccess.Internet) { //WebAPIからデータ取得 var prefecturesData = await _httpClient.GetFromJsonAsync <IEnumerable <Prefecture> >("prefectures"); //キャッシュを更新.キャッシュの有効期限を設定 _barrel.Add(key: CachePrefecturesKey, data: prefecturesData, expireIn: TimeSpan.FromMinutes(10)); return(prefecturesData); } //インターネットにも接続してないし,キャッシュも有効期限切れだった場合 //それでも有効期限切れのキャッシュデータを返すか. if (_barrel.Exists(key: CachePrefecturesKey)) { //とりあえず返すことにした. return(_barrel.Get <IEnumerable <Prefecture> >(key: CachePrefecturesKey)); } return(new List <Prefecture>()); } catch { throw; } }
/// <summary> /// Send a cached requests /// </summary> /// <param name="http">Http client ot use</param> /// <param name="barrel">Barrel to use for cache</param> /// <param name="req">request to send</param> /// <param name="expireIn">expire in</param> /// <param name="forceUpdate">If we should force the update or not</param> /// <param name="throttled">If throttled or not</param> /// <returns>The new or cached response.</returns> public static async Task <string> SendCachedAsync(this HttpClient http, IBarrel barrel, HttpRequestMessage req, TimeSpan expireIn, bool forceUpdate = false, bool throttled = true) { var url = req.RequestUri.ToString(); var contents = barrel.Get <string>(url); var eTag = barrel.GetETag(url); if (!forceUpdate && !string.IsNullOrEmpty(contents) && !barrel.IsExpired(url)) { return(contents); } var etag = eTag ?? null; if (throttled) { await getThrottle.WaitAsync(); } HttpResponseMessage r; string c = null; try { if (!forceUpdate && !string.IsNullOrEmpty(etag) && !string.IsNullOrEmpty(contents)) { req.Headers.IfNoneMatch.Clear(); req.Headers.IfNoneMatch.Add(new EntityTagHeaderValue(etag)); } r = await http.SendAsync(req); if (r.StatusCode == HttpStatusCode.NotModified) { if (string.IsNullOrEmpty(contents)) { throw new IndexOutOfRangeException($"Cached value missing for HTTP request: {url}"); } return(contents); } c = await r.Content.ReadAsStringAsync(); } finally { if (throttled) { getThrottle.Release(); } } if (r.StatusCode == HttpStatusCode.OK) { // Cache it? var newEtag = r.Headers.ETag != null ? r.Headers.ETag.Tag : null; if (!string.IsNullOrEmpty(newEtag) && newEtag != etag) { barrel.Add(url, c, expireIn, newEtag); } return(c); } else { throw new HttpCacheRequestException(r.StatusCode, "HTTP Cache Request Failed"); } }
public void IsExpiredNullTest() { Assert.IsTrue(barrel.IsExpired(url)); }
public bool IsExpired(string videoId) { return(_barrel.IsExpired(videoId + "_c")); }