/// <summary> /// Puts the specified value in the cache. /// </summary> /// <param name="key">The cache key.</param> /// <param name="value">The value to put into the cache.</param> /// <param name="ttl">The time-to-live for the cache entry.</param> public void Put(string key, object value, Ttl ttl) { if (value != null) { _wrappedCacheProvider.Put(key, _serializer.Serialize(value), ttl); } }
public void Put_should_put_item_using_passed_sliding_ttl() { Mock <Microsoft.Extensions.Caching.Distributed.IDistributedCache> mockDistributedCache = new Mock <Microsoft.Extensions.Caching.Distributed.IDistributedCache>(); string key = "anything"; var valueToCache = "something to cache"; ISyncCacheProvider <string> provider = mockDistributedCache.Object.AsSyncCacheProvider <string>(); mockDistributedCache.Setup(idc => idc.Set(It.Is <string>(k => k == key), It.IsAny <byte[]>(), It.IsAny <DistributedCacheEntryOptions>())).Verifiable(); // Because SetString() is an extension method, we cannot mock it. We mock Set() instead. TimeSpan timespan = TimeSpan.FromSeconds(10); Ttl ttl = new Ttl(timespan, true); provider.Put(key, valueToCache, ttl); mockDistributedCache.Verify(idc => idc.Set(key, It.IsAny <byte[]>(), It.Is <DistributedCacheEntryOptions>(o => o.SlidingExpiration == timespan))); }
public void Put_should_put_item_using_passed_nonsliding_ttl() { Mock <IDistributedCache> mockDistributedCache = new Mock <IDistributedCache>(); string key = "anything"; var valueToCache = Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()); ISyncCacheProvider <byte[]> provider = mockDistributedCache.Object.AsSyncCacheProvider <byte[]>(); mockDistributedCache.Setup(idc => idc.Set(It.Is <string>(k => k == key), It.Is <byte[]>(v => v == valueToCache), It.IsAny <DistributedCacheEntryOptions>())).Verifiable(); TimeSpan timespan = TimeSpan.FromSeconds(10); Ttl ttl = new Ttl(timespan, false); provider.Put(key, valueToCache, ttl); mockDistributedCache.Verify(idc => idc.Set(key, valueToCache, It.Is <DistributedCacheEntryOptions>(o => o.AbsoluteExpirationRelativeToNow == timespan))); }
public void Put_should_put_item_using_passed_sliding_ttl() { Mock <Microsoft.Extensions.Caching.Distributed.IDistributedCache> mockDistributedCache = new Mock <Microsoft.Extensions.Caching.Distributed.IDistributedCache>(); string key = "anything"; var valueToCache = new byte[] { 0 }; ISyncCacheProvider <byte[]> provider = mockDistributedCache.Object.AsSyncCacheProvider <byte[]>(); mockDistributedCache.Setup(idc => idc.Set(It.Is <string>(k => k == key), It.Is <byte[]>(v => v == valueToCache), It.IsAny <DistributedCacheEntryOptions>())).Verifiable(); TimeSpan timespan = TimeSpan.FromSeconds(10); Ttl ttl = new Ttl(timespan, true); provider.Put(key, valueToCache, ttl); mockDistributedCache.Verify(idc => idc.Set(key, valueToCache, It.Is <DistributedCacheEntryOptions>(o => o.SlidingExpiration == timespan))); }
internal static TResult Implementation <TResult>( ISyncCacheProvider <TResult> cacheProvider, ITtlStrategy ttlStrategy, Func <Context, string> cacheKeyStrategy, Func <Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken, Action <Context, string> onCacheGet, Action <Context, string> onCacheMiss, Action <Context, string> onCachePut, Action <Context, string, Exception> onCacheGetError, Action <Context, string, Exception> onCachePutError) { cancellationToken.ThrowIfCancellationRequested(); string cacheKey = cacheKeyStrategy(context); if (cacheKey == null) { return(action(context, cancellationToken)); } TResult valueFromCache; try { valueFromCache = cacheProvider.Get(cacheKey); } catch (Exception ex) { valueFromCache = default(TResult); onCacheGetError(context, cacheKey, ex); } if (valueFromCache != null && !valueFromCache.Equals(default(TResult))) { onCacheGet(context, cacheKey); return(valueFromCache); } else { onCacheMiss(context, cacheKey); } TResult result = action(context, cancellationToken); Ttl ttl = ttlStrategy.GetTtl(context); if (ttl.Timespan > TimeSpan.Zero) { try { cacheProvider.Put(cacheKey, result, ttl); onCachePut(context, cacheKey); } catch (Exception ex) { onCachePutError(context, cacheKey, ex); } } return(result); }
void ISyncCacheProvider <TCacheFormat> .Put(string key, TCacheFormat value, Ttl ttl) { _wrappedCacheProvider.Put(key, value, ttl); }