Esempio n. 1
0
        public void Get_should_return_instance_previously_stored_in_cache()
        {
            Mock <Microsoft.Extensions.Caching.Distributed.IDistributedCache> mockDistributedCache = new Mock <Microsoft.Extensions.Caching.Distributed.IDistributedCache>();
            string key = "anything";

            mockDistributedCache.Setup(idc => idc.Get(It.Is <string>(k => k == key))).Returns(new byte[0] {
            }).Verifiable();                                                                                              // Because GetString() is an extension method, we cannot mock it.  We mock Get() instead.

            ISyncCacheProvider <string> provider = mockDistributedCache.Object.AsSyncCacheProvider <string>();
            string got = provider.Get(key);

            mockDistributedCache.Verify(v => v.Get(key), Times.Once);
        }
Esempio n. 2
0
        public void Get_should_return_null_on_unknown_key()
        {
            Mock <Microsoft.Extensions.Caching.Distributed.IDistributedCache> mockDistributedCache = new Mock <Microsoft.Extensions.Caching.Distributed.IDistributedCache>();
            string key = "anything";

            mockDistributedCache.Setup(idc => idc.Get(It.Is <string>(k => k == key))).Verifiable(); // Because GetString() is an extension method, we cannot mock it.  We mock Get() instead.

            ISyncCacheProvider <string> provider = mockDistributedCache.Object.AsSyncCacheProvider <string>();
            string someOtherKey = Guid.NewGuid().ToString();
            string got          = provider.Get(someOtherKey);

            mockDistributedCache.Verify(v => v.Get(someOtherKey), Times.Once);
            got.Should().BeNull();
        }
        public void Get_should_return_instance_previously_stored_in_cache()
        {
            Mock <Microsoft.Extensions.Caching.Distributed.IDistributedCache> mockDistributedCache = new Mock <Microsoft.Extensions.Caching.Distributed.IDistributedCache>();
            string key         = "anything";
            var    cachedValue = new byte[] { 0 };

            mockDistributedCache.Setup(idc => idc.Get(It.Is <string>(k => k == key))).Returns(cachedValue).Verifiable();

            ISyncCacheProvider <byte[]> provider = mockDistributedCache.Object.AsSyncCacheProvider <byte[]>();

            byte[] got = provider.Get(key);

            mockDistributedCache.Verify(v => v.Get(key), Times.Once);
            got.Should().BeSameAs(cachedValue);
        }
        public void Get_should_return_null_on_unknown_key()
        {
            Mock <Microsoft.Extensions.Caching.Distributed.IDistributedCache> mockDistributedCache = new Mock <Microsoft.Extensions.Caching.Distributed.IDistributedCache>();
            string key         = "anything";
            var    cachedValue = new byte[] { 0 };

            mockDistributedCache.Setup(idc => idc.Get(It.Is <string>(k => k == key))).Returns(cachedValue).Verifiable();

            ISyncCacheProvider <byte[]> provider = mockDistributedCache.Object.AsSyncCacheProvider <byte[]>();
            string someOtherKey = Guid.NewGuid().ToString();

            byte[] got = provider.Get(someOtherKey);

            mockDistributedCache.Verify(v => v.Get(someOtherKey), Times.Once);
            got.Should().BeNull();
        }
Esempio n. 5
0
        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);
        }
Esempio n. 6
0
 TCacheFormat ISyncCacheProvider <TCacheFormat> .Get(string key)
 {
     return((TCacheFormat)_wrappedCacheProvider.Get(key));
 }
Esempio n. 7
0
        /// <summary>
        /// Gets a value from the cache.
        /// </summary>
        /// <param name="key">The cache key.</param>
        /// <returns>The value from cache; or null, if none was found.</returns>
        public object Get(string key)
        {
            TSerialized objectToDeserialize = _wrappedCacheProvider.Get(key);

            return(objectToDeserialize == null || objectToDeserialize.Equals(default(TSerialized)) ? null : _serializer.Deserialize(objectToDeserialize));
        }
Esempio n. 8
0
 TCacheFormat ISyncCacheProvider <TCacheFormat> .Get(string key)
 {
     return((TCacheFormat)(_wrappedCacheProvider.Get(key) ?? default(TCacheFormat)));
 }
Esempio n. 9
0
 /// <summary>
 /// Gets a value from the cache.
 /// </summary>
 /// <param name="key">The cache key.</param>
 /// <returns>The value from cache; or null, if none was found.</returns>
 public object Get(string key)
 {
     return(_serializer.Deserialize(_wrappedCacheProvider.Get(key)));
 }