public async Task <IEnumerable <Message> > GetChatMessagesAsync(int id) { await _semaphore.WaitAsync(); try { if (_messageCache.ContainsKey(id)) { _messageCache[id].RemoveAll(x => x.ExpirationDate != null && x.ExpirationDate < DateTime.Now); return(_messageCache[id]); } var response = await _client.GetAsync($"messages/chats/{id}").ConfigureAwait(false); if (!response.IsSuccessStatusCode) { return(null); } var ret = await response.Content.ReadAsAsync <List <Message> >(); _messageCache.Add(id, new List <Message>(ret)); return(ret); } catch (Exception e) { Trace.Write(e.ToString()); throw; } finally { _semaphore.Release(); } }
public void ReturnsRightValueForExistingKey() { var cache = new CacheStorage<string, int>(); cache.Add("1", 1); cache.Add("2", 2); Assert.AreEqual(2, cache.Get("2")); }
public void DoesNotAddExistingValueForFalseOverride() { var cache = new CacheStorage <string, int>(); cache.Add("1", 1); cache.Add("1", 2, false); Assert.AreEqual(1, cache["1"]); }
public void AddsNonExistingValueForTrueOverride() { var cache = new CacheStorage <string, int>(); cache.Add("1", 1); cache.Add("1", 2, true); Assert.AreEqual(2, cache["1"]); }
public void ReturnsRightValueForExistingKey() { var cache = new CacheStorage <string, int>(); cache.Add("1", 1); cache.Add("2", 2); Assert.AreEqual(2, cache.Get("2")); }
public void ReturnsTrueForExistingKey() { var cache = new CacheStorage <string, int>(); cache.Add("1", 1); cache.Add("2", 2); Assert.IsTrue(cache.Contains("2")); }
public void RaisesExpiringEventWithCorrectEventArgsWhenItemExpires() { var key = "1"; var expirationPolicy = new SlidingExpirationPolicy(TimeSpan.FromMilliseconds(250)); var value = 1; var evKey = (string)null; var evExpirationPolicy = default(ExpirationPolicy); var evValue = 0; var cache = new CacheStorage <string, int>(); cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250); cache.Expiring += (sender, e) => { evKey = e.Key; evExpirationPolicy = e.ExpirationPolicy; evValue = e.Value; }; cache.Add(key, value, expirationPolicy); ThreadHelper.Sleep(750); Assert.AreEqual(key, evKey); Assert.AreEqual(expirationPolicy, evExpirationPolicy); Assert.AreEqual(value, evValue); }
public async Task <Chat> GetDialogChatAsync(int otherId) { if (_dialogCache.ContainsKey(otherId)) { var chatId = _dialogCache[otherId]; lock (_lock) if (_poller != null) { lock (_poller.ChatsStorage) if (_poller.ChatsStorage.ContainsKey(chatId)) { return(_poller.ChatsStorage[chatId]); } } } var response = await _client.GetAsync($"chats/dialog/{UserId}/{otherId}").ConfigureAwait(false); if (response.IsSuccessStatusCode) { var ret = await response.Content.ReadAsAsync <Chat>(); if (!_dialogCache.ContainsKey(otherId)) { _dialogCache.Add(otherId, ret.Id); } return(ret); } return(null); }
public void RaisesExpiredEventWithCorrectEventArgsWhenItemExpires() { var dispose = true; var key = "1"; var value = 1; var evDispose = false; var evKey = (string)null; var evValue = 0; var cache = new CacheStorage <string, int>(); cache.DisposeValuesOnRemoval = dispose; cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250); cache.Expired += (sender, e) => { evDispose = e.Dispose; evKey = e.Key; evValue = e.Value; }; cache.Add(key, value, expiration: new TimeSpan(0, 0, 0, 0, 250)); ThreadHelper.Sleep(750); Assert.AreEqual(dispose, evDispose); Assert.AreEqual(key, evKey); Assert.AreEqual(value, evValue); }
public static string AddSearchVM(SearchVM searchVM) { var query = searchVM.SearchQuery.ToString(); AddHistory(searchVM.Keyword); srCache.Add(query, searchVM); return(query); }
public static string AddSearchVM(FavoritesVM searchVM) { var query = searchVM.SearchQuery; AddHistory(searchVM.Keyword); srCache.Add(query, searchVM); return(query); }
public void AddsNonExistingValue() { var cache = new CacheStorage <string, int>(); cache.Add("1", 1); Assert.AreEqual(1, cache["1"]); }
public void DoesNotRaiseExpiredEventOnClearStorage() { var counter = 0; var cache = new CacheStorage <string, int>(); cache.Add("1", 1); cache.Expired += (sender, e) => counter++; Assert.AreEqual(0, counter); }
public void ReturnsCachedItem() { var cache = new CacheStorage <string, int>(); cache.Add("1", 1); var value = cache.GetFromCacheOrFetch("1", () => 2); Assert.IsTrue(cache.Contains("1")); Assert.AreEqual(1, cache["1"]); Assert.AreEqual(1, value); }
public void AddsItemToCacheWithOverrideAndReturnsIt() { var cache = new CacheStorage <string, int>(); cache.Add("1", 1); var value = cache.GetFromCacheOrFetch("1", () => 2, true); Assert.IsTrue(cache.Contains("1")); Assert.AreEqual(2, cache["1"]); Assert.AreEqual(2, value); }
public void AutomaticallyRemovesExpiredItems() { var cache = new CacheStorage <string, int>(); cache.Add("1", 1, expiration: new TimeSpan(0, 0, 1)); Assert.IsTrue(cache.Contains("1")); ThreadHelper.Sleep(2000); Assert.IsFalse(cache.Contains("1")); }
public void AutomaticallyRemovesExpiredItemsOfACacheStorageWithDefaultExpirationPolicyInitializationCode() { var cache = new CacheStorage <string, int>(() => ExpirationPolicy.Duration(TimeSpan.FromMilliseconds(500))); cache.Add("1", 1); Assert.IsTrue(cache.Contains("1")); ThreadHelper.Sleep(2000); Assert.IsFalse(cache.Contains("1")); }
public void RemovesExistingValue() { var cache = new CacheStorage <string, int>(); cache.Add("1", 1); Assert.IsTrue(cache.Contains("1")); cache.Remove("1"); Assert.IsFalse(cache.Contains("1")); }
public void AutomaticallyRemovesExpiredItems() { var cache = new CacheStorage <string, int>(); cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250); cache.Add("1", 1, expiration: new TimeSpan(0, 0, 0, 0, 250)); Assert.IsTrue(cache.Contains("1")); ThreadHelper.Sleep(750); Assert.IsFalse(cache.Contains("1")); }
public void IsAutomaticallyEnabledWhenStartedDisabledButAddingItemWithCustomExpirationPolicy() { var cache = new CacheStorage <string, int>(); cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250); cache.Add("1", 1, expiration: new TimeSpan(0, 0, 0, 0, 250)); Assert.IsTrue(cache.Contains("1")); ThreadHelper.Sleep(750); Assert.IsFalse(cache.Contains("1")); }
public void DoesNotDisposeItemsOnClearWhenDisposingNotEnabled() { var disposable = new CustomDisposable(); var cache = new CacheStorage <string, CustomDisposable>(); cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250); cache.Add("disposable", disposable, expiration: TimeSpan.FromMilliseconds(250)); Assert.IsFalse(disposable.IsDiposed); cache.Clear(); Assert.IsFalse(disposable.IsDiposed); }
public void DisposesItemOnRemoveWhenDisposingEnabled() { var disposable = new CustomDisposable(); var cache = new CacheStorage <string, CustomDisposable>(); cache.DisposeValuesOnRemoval = true; cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250); cache.Add("disposable", disposable, expiration: TimeSpan.FromMilliseconds(250)); Assert.IsFalse(disposable.IsDiposed); cache.Remove("disposable"); Assert.IsTrue(disposable.IsDiposed); }
public void DisposesExpiredItemWhenDisposingNotEnabledButForcedByEventArgs() { var disposable = new CustomDisposable(); var cache = new CacheStorage <string, CustomDisposable>(); cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250); cache.Expired += (sender, e) => { e.Dispose = true; }; cache.Add("disposable", disposable, expiration: TimeSpan.FromMilliseconds(250)); ThreadHelper.Sleep(750); Assert.IsTrue(disposable.IsDiposed); }
public void DoesNotDisposeExpiredItemWhenDisposingEnabledButCanceledByEventArgs() { var disposable = new CustomDisposable(); var cache = new CacheStorage <string, CustomDisposable>(); cache.DisposeValuesOnRemoval = true; cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250); cache.Expired += (sender, e) => { e.Dispose = false; }; cache.Add("disposable", disposable, expiration: TimeSpan.FromMilliseconds(250)); ThreadHelper.Sleep(750); Assert.IsFalse(disposable.IsDiposed); }
public void ItemStaysInCacheWhenExpiringEventIsCanceled() { var key = "1"; var value = 1; var cache = new CacheStorage <string, int>(); cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250); cache.Expiring += (sender, e) => { e.Cancel = true; }; cache.Add(key, value, expiration: new TimeSpan(0, 0, 0, 0, 250)); ThreadHelper.Sleep(750); Assert.IsTrue(cache.Contains(key)); }
public void SetChatUserInfo(int userId, int chatId, ChatUserInfo chatUserInfo) { lock (ChatUserInfosCache) { var key = new ChatUserId(userId, chatId); if (ChatUserInfosCache.ContainsKey(key)) { ChatUserInfosCache[key] = chatUserInfo; } else { ChatUserInfosCache.Add(key, chatUserInfo); } if (_newChatUserInfosHandlers.ContainsKey(key)) { _newChatUserInfosHandlers[key]?.Invoke(this, chatUserInfo); } } }
public static GalleryVM GetVM(Gallery gallery) { var gi = new GalleryInfo(gallery.ID, gallery.Token); if (Cache.TryGet(gi, out var vm)) { vm.Gallery = gallery; if (gallery.Count <= vm.currentIndex) { vm.currentIndex = -1; } } else { vm = new GalleryVM(gallery); Cache.Add(gi, vm); } return(vm); }
public void ThrowsArgumentNullExceptionForNullValueIfNotAllowNullValues() { var cache = new CacheStorage<string, object>(); ExceptionTester.CallMethodAndExpectException<ArgumentNullException>(() => cache.Add(null, null)); }
public void AddsNonExistingValue() { var cache = new CacheStorage<string, int>(); cache.Add("1", 1); Assert.AreEqual(1, cache["1"]); }
public void AddsNonExistingValueForTrueOverride() { var cache = new CacheStorage<string, int>(); cache.Add("1", 1); cache.Add("1", 2, true); Assert.AreEqual(2, cache["1"]); }
public void ThrowsArgumentNullExceptionForNullValueIfNotAllowNullValues() { var cache = new CacheStorage <string, object>(); ExceptionTester.CallMethodAndExpectException <ArgumentNullException>(() => cache.Add(null, null)); }
public void AutomaticallyRemovesExpiredItems() { var cache = new CacheStorage<string, int>(); cache.Add("1", 1, expiration: new TimeSpan(0, 0, 1)); Assert.IsTrue(cache.Contains("1")); ThreadHelper.Sleep(1500); Assert.IsFalse(cache.Contains("1")); }
public void ReturnsCachedItem() { var cache = new CacheStorage<string, int>(); cache.Add("1", 1); var value = cache.GetFromCacheOrFetch("1", () => 2); Assert.IsTrue(cache.Contains("1")); Assert.AreEqual(1, cache["1"]); Assert.AreEqual(1, value); }
public void DoesNotRaiseExpiredEventOnClearStorage() { var counter = 0; var cache = new CacheStorage<string, int>(); cache.Add("1", 1); cache.Expired += (sender, e) => counter++; Assert.AreEqual(0, counter); }
public void IsAutomaticallyEnabledWhenStartedDisabledButAddingItemWithCustomExpirationPolicy() { var cache = new CacheStorage<string, int>(); cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250); cache.Add("1", 1, expiration: new TimeSpan(0, 0, 0, 0, 250)); Assert.IsTrue(cache.Contains("1")); ThreadHelper.Sleep(750); Assert.IsFalse(cache.Contains("1")); }
public void DisposesExpiredItemWhenDisposingNotEnabledButForcedByEventArgs() { var disposable = new CustomDisposable(); var cache = new CacheStorage<string, CustomDisposable>(); cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250); cache.Expired += (sender, e) => { e.Dispose = true; }; cache.Add("disposable", disposable, expiration: TimeSpan.FromMilliseconds(250)); ThreadHelper.Sleep(750); Assert.IsTrue(disposable.IsDiposed); }
public void DoesNotDisposeItemsOnClearWhenDisposingNotEnabled() { var disposable = new CustomDisposable(); var cache = new CacheStorage<string, CustomDisposable>(); cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250); cache.Add("disposable", disposable, expiration: TimeSpan.FromMilliseconds(250)); Assert.IsFalse(disposable.IsDiposed); cache.Clear(); Assert.IsFalse(disposable.IsDiposed); }
public void DoesNotDisposeExpiredItemWhenDisposingEnabledButCanceledByEventArgs() { var disposable = new CustomDisposable(); var cache = new CacheStorage<string, CustomDisposable>(); cache.DisposeValuesOnRemoval = true; cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250); cache.Expired += (sender, e) => { e.Dispose = false; }; cache.Add("disposable", disposable, expiration: TimeSpan.FromMilliseconds(250)); ThreadHelper.Sleep(750); Assert.IsFalse(disposable.IsDiposed); }
public void ThrowsArgumentNullExceptionForNullKey() { var cache = new CacheStorage<string, int>(); ExceptionTester.CallMethodAndExpectException<ArgumentNullException>(() => cache.Add(null, 1)); }
public void SaveToCache(Uri iconUri, byte[] streamContent) { _cache.Add(iconUri.ToString(), streamContent, StoringPolicy); }
public void AddsItemToCacheWithOverrideAndReturnsIt() { var cache = new CacheStorage<string, int>(); cache.Add("1", 1); var value = cache.GetFromCacheOrFetch("1", () => 2, true); Assert.IsTrue(cache.Contains("1")); Assert.AreEqual(2, cache["1"]); Assert.AreEqual(2, value); }
public void RaisesExpiringEventWithCorrectEventArgsWhenItemExpires() { var key = "1"; var expirationPolicy = new SlidingExpirationPolicy(TimeSpan.FromMilliseconds(250)); var value = 1; var evKey = (string)null; var evExpirationPolicy = default(ExpirationPolicy); var evValue = 0; var cache = new CacheStorage<string, int>(); cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250); cache.Expiring += (sender, e) => { evKey = e.Key; evExpirationPolicy = e.ExpirationPolicy; evValue = e.Value; }; cache.Add(key, value, expirationPolicy); ThreadHelper.Sleep(750); Assert.AreEqual(key, evKey); Assert.AreEqual(expirationPolicy, evExpirationPolicy); Assert.AreEqual(value, evValue); }
public void ReturnsTrueForExistingKey() { var cache = new CacheStorage<string, int>(); cache.Add("1", 1); cache.Add("2", 2); Assert.IsTrue(cache.Contains("2")); }
public void AutomaticallyRemovesExpiredItemsOfACacheStorageWithDefaultExpirationPolicyInitializationCode() { var cache = new CacheStorage<string, int>(() => ExpirationPolicy.Duration(TimeSpan.FromMilliseconds(250))); cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250); cache.Add("1", 1); Assert.IsTrue(cache.Contains("1")); ThreadHelper.Sleep(750); Assert.IsFalse(cache.Contains("1")); }
public void RemovesExistingValue() { var cache = new CacheStorage<string, int>(); cache.Add("1", 1); Assert.IsTrue(cache.Contains("1")); cache.Remove("1"); Assert.IsFalse(cache.Contains("1")); }
public void ItemStaysInCacheWhenExpiringEventIsCanceled() { var key = "1"; var value = 1; var cache = new CacheStorage<string, int>(); cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250); cache.Expiring += (sender, e) => { e.Cancel = true; }; cache.Add(key, value, expiration: new TimeSpan(0, 0, 0, 0, 250)); ThreadHelper.Sleep(750); Assert.IsTrue(cache.Contains(key)); }
public void DoesNotAddExistingValueForFalseOverride() { var cache = new CacheStorage<string, int>(); cache.Add("1", 1); cache.Add("1", 2, false); Assert.AreEqual(1, cache["1"]); }
public void ThrowsArgumentNullExceptionForNullKey() { var cache = new CacheStorage <string, int>(); ExceptionTester.CallMethodAndExpectException <ArgumentNullException>(() => cache.Add(null, 1)); }
public void RaisesExpiredEventWithCorrectEventArgsWhenItemExpires() { var dispose = true; var key = "1"; var value = 1; var evDispose = false; var evKey = (string)null; var evValue = 0; var cache = new CacheStorage<string, int>(); cache.DisposeValuesOnRemoval = dispose; cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250); cache.Expired += (sender, e) => { evDispose = e.Dispose; evKey = e.Key; evValue = e.Value; }; cache.Add(key, value, expiration: new TimeSpan(0, 0, 0, 0, 250)); ThreadHelper.Sleep(750); Assert.AreEqual(dispose, evDispose); Assert.AreEqual(key, evKey); Assert.AreEqual(value, evValue); }
public void AutomaticallyRemovesExpiredItems() { var cache = new CacheStorage<string, int>(); cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250); cache.Add("1", 1, expiration: new TimeSpan(0, 0, 0, 0, 250)); Assert.IsTrue(cache.Contains("1")); ThreadHelper.Sleep(750); Assert.IsFalse(cache.Contains("1")); }
protected override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { HttpStatusCode statusCode; string token; // determine whether a jwt exists or not if (!TryRetrieveToken(request, out token)) { statusCode = HttpStatusCode.Unauthorized; return(base.SendAsync(request, cancellationToken)); } try { var secretKey = ConfigurationManager.AppSettings["JWT_SECRET_KEY"]; var audienceToken = ConfigurationManager.AppSettings["JWT_AUDIENCE_TOKEN"]; var issuerToken = ConfigurationManager.AppSettings["JWT_ISSUER_TOKEN"]; var securityKey = new SymmetricSecurityKey(System.Text.Encoding.Default.GetBytes(secretKey)); SecurityToken securityToken; var tokenHandler = new System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler(); TokenValidationParameters validationParameters = new TokenValidationParameters() { ValidAudience = audienceToken, ValidIssuer = issuerToken, ValidateLifetime = true, ValidateIssuerSigningKey = true, LifetimeValidator = this.LifetimeValidator, IssuerSigningKey = securityKey }; //Delete currentUserId and UserName in Cache CurrentUser.DeleteId(); CurrentUser.DeleteName(); // Extract and assign Current Principal and user Thread.CurrentPrincipal = tokenHandler.ValidateToken(token, validationParameters, out securityToken); HttpContext.Current.User = tokenHandler.ValidateToken(token, validationParameters, out securityToken); string userName = Thread.CurrentPrincipal.Identity.Name; string[] userValue = userName.Split(','); this.currentUserName = userValue[0]; this.currentUserId = Convert.ToInt64(userValue[1]); //Cache estorage by 5 minutes CacheStorage.Add("currentUserName", currentUserName, DateTimeOffset.UtcNow.AddMinutes(5)); CacheStorage.Add("currentUserId", currentUserId, DateTimeOffset.UtcNow.AddMinutes(5)); return(base.SendAsync(request, cancellationToken)); } catch (SecurityTokenValidationException) { statusCode = HttpStatusCode.Unauthorized; } catch (Exception) { statusCode = HttpStatusCode.InternalServerError; } return(Task <HttpResponseMessage> .Factory.StartNew(() => new HttpResponseMessage(statusCode) { })); }
public void DisposesItemOnRemoveWhenDisposingEnabled() { var disposable = new CustomDisposable(); var cache = new CacheStorage<string, CustomDisposable>(); cache.DisposeValuesOnRemoval = true; cache.ExpirationTimerInterval = TimeSpan.FromMilliseconds(250); cache.Add("disposable", disposable, expiration: TimeSpan.FromMilliseconds(250)); Assert.IsFalse(disposable.IsDiposed); cache.Remove("disposable"); Assert.IsTrue(disposable.IsDiposed); }