Esempio n. 1
0
        public async Task Can_store_and_get_model()
        {
            var model    = ModelWithIdAndName.Create(1);
            var cacheKey = model.CreateUrn();
            await cacheClient.SetAsync(cacheKey, model);

            var existingModel = await cacheClient.GetAsync <ModelWithIdAndName>(cacheKey);

            ModelWithIdAndName.AssertIsEqual(existingModel, model);
        }
Esempio n. 2
0
 public static async Task SetAsync <T>(this ICacheClientAsync cache, string cacheKey, T value, TimeSpan?expireCacheIn, CancellationToken token = default)
 {
     if (expireCacheIn.HasValue)
     {
         await cache.SetAsync(cacheKey, value, expireCacheIn.Value, token);
     }
     else
     {
         await cache.SetAsync(cacheKey, value, token);
     }
 }
Esempio n. 3
0
 public static async Task CacheSetAsync <T>(this ICacheClientAsync cache, string key, T value, TimeSpan?expiresIn, CancellationToken token = default)
 {
     if (expiresIn.HasValue)
     {
         await cache.SetAsync(key, value, expiresIn.Value, token).ConfigAwait();
     }
     else
     {
         await cache.SetAsync(key, value, token).ConfigAwait();
     }
 }
Esempio n. 4
0
            public async Task SetAsync <T>(string key, T value, CancellationToken token = default)
            {
                var expiry = HostContext.GetPlugin <SessionFeature>()?.SessionBagExpiry;

                if (expiry != null)
                {
                    await cacheClient.SetAsync(EnsurePrefix(key), value, expiry.Value, token).ConfigAwait();
                }
                else
                {
                    await cacheClient.SetAsync(EnsurePrefix(key), value, token).ConfigAwait();
                }
            }
Esempio n. 5
0
        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)));
        }
Esempio n. 6
0
        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);
        }
Esempio n. 7
0
 public async Task <bool> SetAsync <T>(string key, T value, CancellationToken token = default)
 {
     return(await cache.SetAsync(EnsurePrefix(key), value, token).ConfigAwait());
 }
Esempio n. 8
0
        public async Task Does_flush_all()
        {
            await 3.TimesAsync(async i =>
                               await Cache.SetAsync(i.ToUrn <Item>(), new Item {
                Id = i, Name = "Name" + i
            }));

            Assert.That(await Cache.GetAsync <Item>(1.ToUrn < Item > ()), Is.Not.Null);

            await Cache.FlushAllAsync();

            Assert.That(await Cache.GetAsync <Item>(1.ToUrn < Item > ()), Is.Null);
        }
Esempio n. 9
0
        public static async Task <object> CacheAsync(this ICacheClientAsync cache,
                                                     string cacheKey,
                                                     object responseDto,
                                                     IRequest req,
                                                     TimeSpan?expireCacheIn  = null,
                                                     CancellationToken token = default)
        {
            req.Response.Dto = responseDto;
            await cache.SetAsync(cacheKey, responseDto, expireCacheIn, token).ConfigAwait();

            if (!req.ResponseContentType.IsBinary())
            {
                string serializedDto = SerializeToString(req, responseDto);

                string modifiers = null;
                if (req.ResponseContentType.MatchesContentType(MimeTypes.Json))
                {
                    var jsonp = req.GetJsonpCallback();
                    if (jsonp != null)
                    {
                        modifiers     = ".jsonp," + jsonp.SafeVarName();
                        serializedDto = jsonp + "(" + serializedDto + ")";

                        //Add a default expire timespan for jsonp requests,
                        //because they aren't cleared when calling ClearCaches()
                        if (expireCacheIn == null)
                        {
                            expireCacheIn = HostContext.Config.DefaultJsonpCacheExpiration;
                        }
                    }
                }

                var cacheKeySerialized = GetCacheKeyForSerialized(cacheKey, req.ResponseContentType, modifiers);
                await cache.SetAsync(cacheKeySerialized, serializedDto, expireCacheIn, token).ConfigAwait();

                var  compressionType = req.GetCompressionType();
                bool doCompression   = compressionType != null;
                if (doCompression)
                {
                    var lastModified = HostContext.GetPlugin <HttpCacheFeature>().ShouldAddLastModifiedToOptimizedResults() &&
                                       string.IsNullOrEmpty(req.Response.GetHeader(HttpHeaders.CacheControl))
                        ? DateTime.UtcNow
                        : (DateTime?)null;

                    var cacheKeySerializedZip = GetCacheKeyForCompressed(cacheKeySerialized, compressionType);

                    byte[] compressedSerializedDto = serializedDto.Compress(compressionType);
                    await cache.SetAsync(cacheKeySerializedZip, compressedSerializedDto, expireCacheIn, token).ConfigAwait();

                    if (lastModified != null)
                    {
                        await cache.SetAsync(DateCacheKey(cacheKeySerializedZip), lastModified.Value.Ticks, expireCacheIn, token).ConfigAwait();
                    }

                    return(compressedSerializedDto != null
                        ? new CompressedResult(compressedSerializedDto, compressionType, req.ResponseContentType)
                    {
                        Status = req.Response.StatusCode,
                        LastModified = lastModified,
                    }
                        : null);
                }

                return(serializedDto);
            }
            else
            {
                string modifiers          = null;
                byte[] serializedDto      = HostContext.ContentTypes.SerializeToBytes(req, responseDto);
                var    cacheKeySerialized = GetCacheKeyForSerialized(cacheKey, req.ResponseContentType, modifiers);
                await cache.SetAsync(cacheKeySerialized, serializedDto, expireCacheIn, token).ConfigAwait();

                return(serializedDto);
            }
        }