public async Task Get_non_existant_value_returns_null() { var model = ModelWithIdAndName.Create(1); var cacheKey = model.CreateUrn(); var existingModel = await cacheClient.GetAsync <ModelWithIdAndName>(cacheKey); Assert.That(existingModel, Is.Null); }
public static async Task <T> GetOrCreateAsync <T>(this ICacheClientAsync cache, string key, TimeSpan expiresIn, Func <Task <T> > createFn) { var value = await cache.GetAsync <T>(key); if (Equals(value, default(T))) { value = await createFn().ConfigAwait(); await cache.SetAsync(key, value, expiresIn); } return(value); }
public static async Task <ValidCache> HasValidCacheAsync(this ICacheClientAsync cache, IRequest req, string cacheKey, DateTime?checkLastModified, CancellationToken token = default) { if (!HostContext.GetPlugin <HttpCacheFeature>().ShouldAddLastModifiedToOptimizedResults()) { return(ValidCache.NotValid); } var ticks = await cache.GetAsync <long>(DateCacheKey(cacheKey), token).ConfigAwait(); if (ticks > 0) { if (checkLastModified == null) { return(ValidCache.NotValid); } var lastModified = new DateTime(ticks, DateTimeKind.Utc); return(new ValidCache(checkLastModified.Value <= lastModified, lastModified)); } return(ValidCache.NotValid); }
public static async Task <IAuthSession> GetUntypedSessionAsync(this ICacheClientAsync cache, IRequest httpReq = null, IResponse httpRes = null, CancellationToken token = default) { var sessionKey = GetSessionKey(httpReq); if (sessionKey != null) { var userSession = await cache.GetAsync <IAuthSession>(sessionKey, token); if (!Equals(userSession, default(AuthUserSession))) { return(userSession); } } if (sessionKey == null) { SessionFeature.CreateSessionIds(httpReq, httpRes); } var unAuthorizedSession = (IAuthSession)typeof(AuthUserSession).CreateInstance(); return(unAuthorizedSession); }
public async Task <T> GetAsync <T>(string key, CancellationToken token = default) { return(await cache.GetAsync <T>(EnsurePrefix(key), token).ConfigAwait()); }
public async Task Can_set_and_remove_entry() { var key = 1.ToUrn < Item > (); var item = await Cache.GetAsync <Item>(key); Assert.That(item, Is.Null); var whenNotExists = await Cache.SetAsync(key, new Item { Id = 1, Name = "Foo" }); Assert.That(whenNotExists, Is.True); var whenExists = await Cache.SetAsync(key, new Item { Id = 1, Name = "Foo" }); Assert.That(whenExists, Is.True); item = await Cache.GetAsync <Item>(key); Assert.That(item, Is.Not.Null); Assert.That(item.Name, Is.EqualTo("Foo")); whenExists = await Cache.RemoveAsync(key); Assert.That(whenExists, Is.True); whenNotExists = await Cache.RemoveAsync(key); Assert.That(whenNotExists, Is.False); }
public static async Task<bool> HandleValidCache(this IRequest req, CacheInfo cacheInfo, CancellationToken token=default) { if (cacheInfo == null) return false; ICacheClient cache; ICacheClientAsync cacheAsync = null; // only non-null if native ICacheClientAsync exists if (cacheInfo.LocalCache) cache = HostContext.AppHost.GetMemoryCacheClient(req); else HostContext.AppHost.TryGetNativeCacheClient(req, out cache, out cacheAsync); var cacheControl = HostContext.GetPlugin<HttpCacheFeature>().BuildCacheControlHeader(cacheInfo); var res = req.Response; DateTime? lastModified = null; var doHttpCaching = cacheInfo.MaxAge != null || cacheInfo.CacheControl != CacheControl.None; if (doHttpCaching) { lastModified = cacheAsync != null ? await cacheAsync.GetAsync<DateTime?>(cacheInfo.LastModifiedKey(), token).ConfigAwait() : cache.Get<DateTime?>(cacheInfo.LastModifiedKey()); if (req.HasValidCache(lastModified)) { if (cacheControl != null) res.AddHeader(HttpHeaders.CacheControl, cacheControl); res.EndNotModified(); return true; } } var encoding = !cacheInfo.NoCompression ? req.GetCompressionType() : null; var useCacheKey = encoding != null ? cacheInfo.CacheKey + "." + encoding : cacheInfo.CacheKey; var responseBytes = cacheAsync != null ? await cacheAsync.GetAsync<byte[]>(useCacheKey, token).ConfigAwait() : cache.Get<byte[]>(useCacheKey); if (responseBytes != null) { if (encoding != null) res.AddHeader(HttpHeaders.ContentEncoding, encoding); if (cacheInfo.VaryByUser) res.AddHeader(HttpHeaders.Vary, "Cookie"); if (cacheControl != null) res.AddHeader(HttpHeaders.CacheControl, cacheControl); if (!doHttpCaching) { lastModified = cacheAsync != null ? await cacheAsync.GetAsync<DateTime?>(cacheInfo.LastModifiedKey(), token).ConfigAwait() : cache.Get<DateTime?>(cacheInfo.LastModifiedKey()); } if (lastModified != null) res.AddHeader(HttpHeaders.LastModified, lastModified.Value.ToUniversalTime().ToString("r")); await res.WriteBytesToResponse(responseBytes, req.ResponseContentType, token).ConfigAwait(); return true; } return false; }
public Task <T> GetAsync <T>(string key, CancellationToken token = default) { return(cacheClient.GetAsync <T>(EnsurePrefix(key), token)); }
public static async Task <object> ResolveFromCacheAsync(this ICacheClientAsync cache, string cacheKey, IRequest req, CancellationToken token = default) { var checkModifiedSince = GetIfModifiedSince(req); if (!req.ResponseContentType.IsBinary()) { string modifiers = null; if (req.ResponseContentType == MimeTypes.Json) { string jsonp = req.GetJsonpCallback(); if (jsonp != null) { modifiers = ".jsonp," + jsonp.SafeVarName(); } } var cacheKeySerialized = GetCacheKeyForSerialized(cacheKey, req.ResponseContentType, modifiers); var compressionType = req.GetCompressionType(); bool doCompression = compressionType != null; if (doCompression) { var cacheKeySerializedZip = GetCacheKeyForCompressed(cacheKeySerialized, compressionType); var validCache = await cache.HasValidCacheAsync(req, cacheKeySerializedZip, checkModifiedSince, token).ConfigAwait(); if (validCache.IsValid) { return(HttpResult.NotModified()); } DateTime?lastModified = validCache.LastModified; if (req.Response.GetHeader(HttpHeaders.CacheControl) != null) { lastModified = null; } var compressedResult = await cache.GetAsync <byte[]>(cacheKeySerializedZip, token).ConfigAwait(); if (compressedResult != null) { return(new CompressedResult( compressedResult, compressionType, req.ResponseContentType) { LastModified = lastModified, }); } } else { if ((await cache.HasValidCacheAsync(req, cacheKeySerialized, checkModifiedSince, token).ConfigAwait()).IsValid) { return(HttpResult.NotModified()); } var serializedResult = await cache.GetAsync <string>(cacheKeySerialized, token).ConfigAwait(); if (serializedResult != null) { return(serializedResult); } } } else { var cacheKeySerialized = GetCacheKeyForSerialized(cacheKey, req.ResponseContentType, modifiers: null); if ((await cache.HasValidCacheAsync(req, cacheKeySerialized, checkModifiedSince, token).ConfigAwait()).IsValid) { return(HttpResult.NotModified()); } var serializedResult = await cache.GetAsync <byte[]>(cacheKeySerialized, token).ConfigAwait(); if (serializedResult != null) { return(serializedResult); } } return(null); }
public async Task Can_set_get_and_remove() { await cache.SetAsync("Car", "Audi"); var response = await cache.GetAsync <string>("Car"); Assert.That(response, Is.EqualTo("Audi")); await cache.RemoveAsync("Car"); response = await cache.GetAsync <string>("Car"); Assert.That(response, Is.EqualTo(default(string))); }