public void AddWithTagsTest()
        {
            var provider = new MemoryCacheProvider();
            string key = "AddWithTagsTest" + DateTime.Now.Ticks;
            string[] tags = new[] { "a", "b" };
            var cacheKey = new CacheKey(key, tags);
            var value = "Test Value " + DateTime.Now;
            var cachePolicy = new CachePolicy();

            bool result = provider.Add(cacheKey, value, cachePolicy);
            result.Should().BeTrue();

            // look in underlying MemoryCache
            string innerKey = MemoryCacheProvider.GetKey(cacheKey);
            var cachedValue = MemoryCache.Default.Get(innerKey);
            cachedValue.Should().NotBeNull();
            cachedValue.Should().Be(value);

            // make sure cache key is in underlying MemoryCache
            var cacheTag = new CacheTag("a");
            string tagKey = MemoryCacheProvider.GetTagKey(cacheTag);
            tagKey.Should().NotBeNullOrEmpty();

            var cachedTag = MemoryCache.Default.Get(tagKey);
            cachedTag.Should().NotBeNull();
        }
        //[Fact]
        public void ExpireTest()
        {
            var cacheManager = new CacheManager();
            string key = "ExpireTest" + DateTime.Now.Ticks;
            var tags = new[] { "a", "b" };
            var cacheKey = new CacheKey(key, tags);
            var value = "Test Value " + DateTime.Now;
            var cachePolicy = new CachePolicy();

            bool result = cacheManager.Add(cacheKey, value, cachePolicy);
            result.Should().BeTrue();

            // add second value with same tag
            string key2 = "ExpireTest2" + DateTime.Now.Ticks;
            var tags2 = new[] { "a", "c" };
            var cacheKey2 = new CacheKey(key2, tags2);
            var value2 = "Test Value 2 " + DateTime.Now;
            var cachePolicy2 = new CachePolicy();

            bool result2 = cacheManager.Add(cacheKey2, value2, cachePolicy2);
            result2.Should().BeTrue();

            // add third value with same tag
            string key3 = "ExpireTest3" + DateTime.Now.Ticks;
            var tags3 = new[] { "b", "c" };
            var cacheKey3 = new CacheKey(key3, tags3);
            var value3 = "Test Value 3 " + DateTime.Now;
            var cachePolicy3 = new CachePolicy();

            bool result3 = cacheManager.Add(cacheKey3, value3, cachePolicy3);
            result3.Should().BeTrue();


            var cacheTag = new CacheTag("a");
            string tagKey = MemoryCacheProvider.GetTagKey(cacheTag);
            tagKey.Should().NotBeNullOrEmpty();

            var cachedTag = cacheManager.Get<object>(tagKey);
            cachedTag.Should().NotBeNull();

            // expire actually just changes the value for tag key
            cacheManager.Expire(cacheTag);

            // allow flush
            System.Threading.Thread.Sleep(500);

            var expiredTag = cacheManager.Get<object>(tagKey);
            expiredTag.Should().NotBeNull();
            expiredTag.Should().NotBe(cachedTag);

            // items should have been removed
            var expiredValue = cacheManager.Get<string>(cacheKey.Key);
            expiredValue.Should().BeNull();

            var expiredValue2 = cacheManager.Get<string>(cacheKey2.Key);
            expiredValue2.Should().BeNull();

            var expiredValue3 = cacheManager.Get<string>(cacheKey3.Key);
            expiredValue3.Should().NotBeNull();
        }
        public void AddWithExistingTagTest()
        {
            var provider = new MemoryCacheProvider();
            string key = "AddWithExistingTagTest" + DateTime.Now.Ticks;
            string[] tags = new[] { "a", "b" };
            var cacheKey = new CacheKey(key, tags);
            var value = "Test Value " + DateTime.Now;
            var cachePolicy = new CachePolicy();

            bool result = provider.Add(cacheKey, value, cachePolicy);
            result.Should().BeTrue();

            // make sure cache key is in underlying MemoryCache
            var cacheTag = new CacheTag("a");
            string tagKey = MemoryCacheProvider.GetTagKey(cacheTag);
            tagKey.Should().NotBeNullOrEmpty();

            var cachedTag = MemoryCache.Default.Get(tagKey);
            cachedTag.Should().NotBeNull();

            // add second value with same tag
            string key2 = "AddWithExistingTagTest2" + DateTime.Now.Ticks;
            string[] tags2 = new[] { "a", "c" };
            var cacheKey2 = new CacheKey(key2, tags2);
            var value2 = "Test Value 2 " + DateTime.Now;
            var cachePolicy2 = new CachePolicy();

            bool result2 = provider.Add(cacheKey2, value2, cachePolicy2);
            result2.Should().BeTrue();

            // tag 'a' should have same value
            var cachedTag2 = MemoryCache.Default.Get(tagKey);
            cachedTag2.Should().NotBeNull();
            cachedTag2.Should().Be(cachedTag);
        }
        /// <summary>
        /// Inserts a cache entry into the cache without overwriting any existing cache entry.
        /// </summary>
        /// <param name="cacheKey">A unique identifier for the cache entry.</param>
        /// <param name="value">The object to insert.</param>
        /// <param name="cachePolicy">An object that contains eviction details for the cache entry.</param>
        /// <returns>
        ///   <c>true</c> if insertion succeeded, or <c>false</c> if there is an already an entry in the cache that has the same key as key.
        /// </returns>
        public bool Add(CacheKey cacheKey, object value, CachePolicy cachePolicy)
        {
            string key = GetKey(cacheKey);
            var item = new CacheItem(key, value);
            var policy = CreatePolicy(cacheKey, cachePolicy);

            var existing = MemoryCache.Default.AddOrGetExisting(item, policy);
            return existing.Value == null;
        }
Beispiel #5
0
 public void CachePolicyConstructorTest()
 {
     var cachePolicy = new CachePolicy();
     
     cachePolicy.Should().NotBeNull();
     cachePolicy.Mode.Should().Be(CacheExpirationMode.None);
     cachePolicy.AbsoluteExpiration.Should().Be(ObjectCache.InfiniteAbsoluteExpiration);
     cachePolicy.SlidingExpiration.Should().Be(ObjectCache.NoSlidingExpiration);
 }
        public void AddTest()
        {
            var cacheManager = new CacheManager();
            var cacheKey = new CacheKey("AddTest" + DateTime.Now.Ticks);
            var value = "Test Value " + DateTime.Now;
            var cachePolicy = new CachePolicy();

            bool result = cacheManager.Add(cacheKey, value, cachePolicy);
            result.Should().BeTrue();
        }
        public void AddTest()
        {
            var provider = new MemoryCacheProvider();
            var cacheKey = new CacheKey("AddTest" + DateTime.Now.Ticks);
            var value = "Test Value " + DateTime.Now;
            var cachePolicy = new CachePolicy();

            bool result = provider.Add(cacheKey, value, cachePolicy);
            result.Should().BeTrue();

            // look in underlying MemoryCache
            string innerKey = MemoryCacheProvider.GetKey(cacheKey);
            var cachedValue = MemoryCache.Default.Get(innerKey);
            cachedValue.Should().NotBeNull();
            cachedValue.Should().Be(value);
        }
        /// <summary>
        /// Gets the cache value for the specified key that is already in the dictionary or the new value for the key as returned by <paramref name="valueFactory"/>.
        /// </summary>
        /// <param name="cacheKey">A unique identifier for the cache entry.</param>
        /// <param name="valueFactory">The function used to generate a value to insert into cache.</param>
        /// <param name="cachePolicy">A <see cref="CachePolicy"/> that contains eviction details for the cache entry.</param>
        /// <returns>
        /// The value for the key. This will be either the existing value for the key if the key is already in the cache,
        /// or the new value for the key as returned by <paramref name="valueFactory"/> if the key was not in the cache.
        /// </returns>
        public object GetOrAdd(CacheKey cacheKey, Func<CacheKey, object> valueFactory, CachePolicy cachePolicy)
        {
            string key = GetKey(cacheKey);
            if (MemoryCache.Default.Contains(key))
            {
                Debug.WriteLine("Cache Hit : " + key);
                return MemoryCache.Default.Get(key);
            }

            Debug.WriteLine("Cache Miss: " + key);
            // get value and add to cache
            object value = valueFactory(cacheKey);
            if (Add(cacheKey, value, cachePolicy))
                return value;

            // add failed
            return null;
        }
Beispiel #9
0
        /// <summary>
        /// Gets the cache value for the specified key that is already in the dictionary or the new value for the key as returned by <paramref name="valueFactory"/>.
        /// </summary>
        /// <param name="cacheKey">A unique identifier for the cache entry.</param>
        /// <param name="valueFactory">The function used to generate a value to insert into cache.</param>
        /// <param name="cachePolicy">A <see cref="CachePolicy"/> that contains eviction details for the cache entry.</param>
        /// <returns>
        /// The value for the key. This will be either the existing value for the key if the key is already in the cache,
        /// or the new value for the key as returned by <paramref name="valueFactory"/> if the key was not in the cache.
        /// </returns>
        public object GetOrAdd(CacheKey cacheKey, Func<CacheKey, object> valueFactory, CachePolicy cachePolicy)
        {

            var key = GetKey(cacheKey);
            var cachedResult = MemoryCache.Default.Get(key);

            if (cachedResult != null)
            {
                Debug.WriteLine("Cache Hit : " + key);
                return cachedResult;
            }

            Debug.WriteLine("Cache Miss: " + key);

            // get value and add to cache, not bothered
            // if it succeeds or not just rerturn the value
            var value = valueFactory(cacheKey);
            this.Add(cacheKey, value, cachePolicy);

            return value;
        }
 /// <summary>
 /// Inserts a cache entry into the cache overwriting any existing cache entry.
 /// </summary>
 /// <param name="key">A unique identifier for the cache entry.</param>
 /// <param name="value">The object to insert.</param>
 /// <param name="cachePolicy">An object that contains eviction details for the cache entry.</param>
 public void Set(string key, object value, CachePolicy cachePolicy)
 {
     var cacheKey = new CacheKey(key);
     Set(cacheKey, value, cachePolicy);
 }
        /// <summary>
        /// Gets the cache value for the specified key that is already in the dictionary or the new value for the key as returned asynchronously by <paramref name="valueFactory"/>.
        /// </summary>
        /// <param name="cacheKey">A unique identifier for the cache entry.</param>
        /// <param name="valueFactory">The asynchronous function used to generate a value to insert into cache.</param>
        /// <param name="cachePolicy">An object that contains eviction details for the cache entry.</param>
        /// <returns>
        /// The value for the key. This will be either the existing value for the key if the key is already in the dictionary, 
        /// or the new value for the key as returned by <paramref name="valueFactory"/> if the key was not in the dictionary.
        /// </returns>
        public virtual Task<object> GetOrAddAsync(CacheKey cacheKey, Func<CacheKey, Task<object>> valueFactory, CachePolicy cachePolicy)
        {
            var provider = ResolveProvider();
            var item = provider.GetOrAddAsync(cacheKey, valueFactory, cachePolicy);

            return item;
        }
 /// <summary>
 /// Gets the cache value for the specified key that is already in the dictionary or the new value for the key as returned by <paramref name="valueFactory"/>.
 /// </summary>
 /// <param name="key">A unique identifier for the cache entry.</param>
 /// <param name="valueFactory">The function used to generate a value to insert into cache.</param>
 /// <param name="cachePolicy">An object that contains eviction details for the cache entry.</param>
 /// <returns>
 /// The value for the key. This will be either the existing value for the key if the key is already in the dictionary, 
 /// or the new value for the key as returned by <paramref name="valueFactory"/> if the key was not in the dictionary.
 /// </returns>
 public object GetOrAdd(string key, Func<string, object> valueFactory, CachePolicy cachePolicy)
 {
     var cacheKey = new CacheKey(key);
     return GetOrAdd(cacheKey, valueFactory, cachePolicy);
 }
 /// <summary>
 /// Gets the cache value for the specified key that is already in the dictionary or the new value if the key was not in the dictionary.
 /// </summary>
 /// <param name="key">A unique identifier for the cache entry.</param>
 /// <param name="value">The object to insert.</param>
 /// <param name="cachePolicy">An object that contains eviction details for the cache entry.</param>
 /// <returns>
 /// The value for the key. This will be either the existing value for the key if the key is already in the dictionary, 
 /// or the new value if the key was not in the dictionary.
 /// </returns>
 public object GetOrAdd(string key, object value, CachePolicy cachePolicy)
 {
     var cacheKey = new CacheKey(key);
     return GetOrAdd(cacheKey, value, cachePolicy);
 }
 /// <summary>
 /// Inserts a cache entry into the cache without overwriting any existing cache entry.
 /// </summary>
 /// <param name="cacheKey">A unique identifier for the cache entry.</param>
 /// <param name="value">The object to insert.</param>
 /// <param name="cachePolicy">An object that contains eviction details for the cache entry.</param>
 /// <returns>
 ///   <c>true</c> if insertion succeeded, or <c>false</c> if there is an already an entry in the cache that has the same key as key.
 /// </returns>
 public virtual bool Add(CacheKey cacheKey, object value, CachePolicy cachePolicy)
 {
     var provider = ResolveProvider();
     return provider.Add(cacheKey, value, cachePolicy);
 }
        /// <summary>
        /// Inserts a cache entry into the cache without overwriting any existing cache entry.
        /// </summary>
        /// <param name="cacheKey">A unique identifier for the cache entry.</param>
        /// <param name="value">The object to insert.</param>
        /// <param name="cachePolicy">An object that contains eviction details for the cache entry.</param>
        /// <returns>
        ///   <c>true</c> if insertion succeeded, or <c>false</c> if there is an already an entry in the cache that has the same key as key.
        /// </returns>
        public virtual bool Add(CacheKey cacheKey, object value, CachePolicy cachePolicy)
        {
            var provider = ResolveProvider();

            return(provider.Add(cacheKey, value, cachePolicy));
        }
        public void GetOrAddTest()
        {
            var provider = new MemoryCacheProvider();
            var cacheKey = new CacheKey("GetOrAddTest" + DateTime.Now.Ticks);
            var value = "Test Value " + DateTime.Now;
            var cachePolicy = new CachePolicy();
            int callCount = 0;

            Func<CacheKey, object> valueFactory = k =>
            {
                callCount++;
                return value;
            };

            var result = provider.GetOrAdd(cacheKey, valueFactory, cachePolicy);
            result.Should().Be(value);
            callCount.Should().Be(1);

            // look in underlying MemoryCache
            string innerKey = MemoryCacheProvider.GetKey(cacheKey);
            var cachedValue = MemoryCache.Default.Get(innerKey);
            cachedValue.Should().NotBeNull();
            cachedValue.Should().Be(value);

            callCount = 0;
            var result2 = provider.GetOrAdd(cacheKey, valueFactory, cachePolicy);
            result2.Should().Be(value);
            callCount.Should().Be(0);
        }
        /// <summary>
        /// Gets the cache value for the specified key that is already in the dictionary or the new value for the key as returned by <paramref name="valueFactory"/>.
        /// </summary>
        /// <param name="key">A unique identifier for the cache entry.</param>
        /// <param name="valueFactory">The function used to generate a value to insert into cache.</param>
        /// <param name="cachePolicy">An object that contains eviction details for the cache entry.</param>
        /// <returns>
        /// The value for the key. This will be either the existing value for the key if the key is already in the dictionary,
        /// or the new value for the key as returned by <paramref name="valueFactory"/> if the key was not in the dictionary.
        /// </returns>
        public object GetOrAdd(string key, Func <string, object> valueFactory, CachePolicy cachePolicy)
        {
            var cacheKey = new CacheKey(key);

            return(GetOrAdd(cacheKey, valueFactory, cachePolicy));
        }
        /// <summary>
        /// Gets the cache value for the specified key that is already in the dictionary or the new value for the key as returned by <paramref name="valueFactory"/>.
        /// </summary>
        /// <param name="key">A unique identifier for the cache entry.</param>
        /// <param name="valueFactory">The function used to generate a value to insert into cache.</param>
        /// <param name="slidingExpiration">A span of time within which a cache entry must be accessed before the cache entry is evicted from the cache.</param>
        /// <returns>
        /// The value for the key. This will be either the existing value for the key if the key is already in the dictionary,
        /// or the new value for the key as returned by <paramref name="valueFactory"/> if the key was not in the dictionary.
        /// </returns>
        public object GetOrAdd(string key, Func <string, object> valueFactory, TimeSpan slidingExpiration)
        {
            var policy = CachePolicy.WithSlidingExpiration(slidingExpiration);

            return(GetOrAdd(key, valueFactory, policy));
        }
        /// <summary>
        /// Gets the cache value for the specified key that is already in the dictionary or the new value for the key as returned by <paramref name="valueFactory"/>.
        /// </summary>
        /// <param name="key">A unique identifier for the cache entry.</param>
        /// <param name="valueFactory">The function used to generate a value to insert into cache.</param>
        /// <param name="absoluteExpiration">The fixed date and time at which the cache entry will expire.</param>
        /// <returns>
        /// The value for the key. This will be either the existing value for the key if the key is already in the dictionary,
        /// or the new value for the key as returned by <paramref name="valueFactory"/> if the key was not in the dictionary.
        /// </returns>
        public object GetOrAdd(string key, Func <string, object> valueFactory, DateTimeOffset absoluteExpiration)
        {
            var policy = CachePolicy.WithAbsoluteExpiration(absoluteExpiration);

            return(GetOrAdd(key, valueFactory, policy));
        }
 /// <summary>
 /// Gets the cache value for the specified key that is already in the dictionary or the new value if the key was not in the dictionary.
 /// </summary>
 /// <param name="key">A unique identifier for the cache entry.</param>
 /// <param name="value">The object to insert.</param>
 /// <param name="cachePolicy">An object that contains eviction details for the cache entry.</param>
 /// <returns>
 /// The value for the key. This will be either the existing value for the key if the key is already in the dictionary,
 /// or the new value if the key was not in the dictionary.
 /// </returns>
 public object GetOrAdd(CacheKey key, object value, CachePolicy cachePolicy)
 {
     return(GetOrAdd(key, k => value, cachePolicy));
 }
        /// <summary>
        /// Gets the cache value for the specified key that is already in the dictionary or the new value if the key was not in the dictionary.
        /// </summary>
        /// <param name="key">A unique identifier for the cache entry.</param>
        /// <param name="value">The object to insert.</param>
        /// <param name="cachePolicy">An object that contains eviction details for the cache entry.</param>
        /// <returns>
        /// The value for the key. This will be either the existing value for the key if the key is already in the dictionary,
        /// or the new value if the key was not in the dictionary.
        /// </returns>
        public object GetOrAdd(string key, object value, CachePolicy cachePolicy)
        {
            var cacheKey = new CacheKey(key);

            return(GetOrAdd(cacheKey, value, cachePolicy));
        }
        /// <summary>
        /// Gets the cache value for the specified key that is already in the dictionary or the new value if the key was not in the dictionary.
        /// </summary>
        /// <param name="key">A unique identifier for the cache entry.</param>
        /// <param name="value">The object to insert.</param>
        /// <param name="slidingExpiration">A span of time within which a cache entry must be accessed before the cache entry is evicted from the cache.</param>
        /// <returns>
        /// The value for the key. This will be either the existing value for the key if the key is already in the dictionary,
        /// or the new value if the key was not in the dictionary.
        /// </returns>
        public object GetOrAdd(string key, object value, TimeSpan slidingExpiration)
        {
            var policy = CachePolicy.WithSlidingExpiration(slidingExpiration);

            return(GetOrAdd(key, value, policy));
        }
        /// <summary>
        /// Gets the cache value for the specified key that is already in the dictionary or the new value if the key was not in the dictionary.
        /// </summary>
        /// <param name="key">A unique identifier for the cache entry.</param>
        /// <param name="value">The object to insert.</param>
        /// <returns>
        /// The value for the key. This will be either the existing value for the key if the key is already in the dictionary,
        /// or the new value if the key was not in the dictionary.
        /// </returns>
        public object GetOrAdd(string key, object value)
        {
            var policy = new CachePolicy();

            return(GetOrAdd(key, value, policy));
        }
 /// <summary>
 /// Inserts a cache entry into the cache without overwriting any existing cache entry.
 /// </summary>
 /// <param name="key">A unique identifier for the cache entry.</param>
 /// <param name="value">The object to insert.</param>
 /// <returns><c>true</c> if insertion succeeded, or <c>false</c> if there is an already an entry in the cache that has the same key as key.</returns>
 public bool Add(string key, object value)
 {
     var cachePolicy = new CachePolicy();
     return Add(key, value, cachePolicy);
 }
        public void ExpireTest()
        {
            var cache = MemoryCache.Default;
            // purge all values
            foreach (KeyValuePair<string, object> pair in cache)
                cache.Remove(pair.Key);

            var provider = new MemoryCacheProvider();
            string key = "ExpireTest" + DateTime.Now.Ticks;
            var tags = new[] { "a", "b" };
            var cacheKey = new CacheKey(key, tags);
            var value = "Test Value " + DateTime.Now;
            var cachePolicy = new CachePolicy();

            bool result = provider.Add(cacheKey, value, cachePolicy);
            result.Should().BeTrue();

            // add second value with same tag
            string key2 = "ExpireTest2" + DateTime.Now.Ticks;
            var tags2 = new[] { "a", "c" };
            var cacheKey2 = new CacheKey(key2, tags2);
            var value2 = "Test Value 2 " + DateTime.Now;
            var cachePolicy2 = new CachePolicy();

            bool result2 = provider.Add(cacheKey2, value2, cachePolicy2);
            result2.Should().BeTrue();

            // add third value with same tag
            string key3 = "ExpireTest3" + DateTime.Now.Ticks;
            var tags3 = new[] { "b", "c" };
            var cacheKey3 = new CacheKey(key3, tags3);
            var value3 = "Test Value 3 " + DateTime.Now;
            var cachePolicy3 = new CachePolicy();

            bool result3 = provider.Add(cacheKey3, value3, cachePolicy3);
            result3.Should().BeTrue();


            var cacheTag = new CacheTag("a");
            string tagKey = MemoryCacheProvider.GetTagKey(cacheTag);
            tagKey.Should().NotBeNullOrEmpty();

            // underlying cache
            cache.GetCount().Should().Be(6);

            var cachedTag = cache.Get(tagKey);
            cachedTag.Should().NotBeNull();

            System.Threading.Thread.Sleep(500);

            // expire actually just changes the value for tag key
            provider.Expire(cacheTag);

            var expiredTag = cache.Get(tagKey);
            expiredTag.Should().NotBeNull();
            expiredTag.Should().NotBe(cachedTag);

            // items should have been removed
            var expiredValue = provider.Get<string>(cacheKey);
            expiredValue.Should().BeNull();

            var expiredValue2 = provider.Get<string>(cacheKey2);
            expiredValue2.Should().BeNull();

            var expiredValue3 = provider.Get<string>(cacheKey3);
            expiredValue3.Should().NotBeNull();

            cache.GetCount().Should().Be(4);
        }
        /// <summary>
        /// Gets the cache value for the specified key that is already in the dictionary or the new value for the key as returned by <paramref name="valueFactory"/>.
        /// </summary>
        /// <param name="cacheKey">A unique identifier for the cache entry.</param>
        /// <param name="valueFactory">The function used to generate a value to insert into cache.</param>
        /// <param name="cachePolicy">An object that contains eviction details for the cache entry.</param>
        /// <returns>
        /// The value for the key. This will be either the existing value for the key if the key is already in the dictionary,
        /// or the new value for the key as returned by <paramref name="valueFactory"/> if the key was not in the dictionary.
        /// </returns>
        public virtual object GetOrAdd(CacheKey cacheKey, Func <CacheKey, object> valueFactory, CachePolicy cachePolicy)
        {
            var provider = ResolveProvider();
            var item     = provider.GetOrAdd(cacheKey, valueFactory, cachePolicy);

            return(item);
        }
Beispiel #27
0
        /// <summary>
        /// Inserts a cache entry into the cache overwriting any existing cache entry.
        /// </summary>
        /// <param name="key">A unique identifier for the cache entry.</param>
        /// <param name="value">The object to insert.</param>
        /// <param name="absoluteExpiration">The fixed date and time at which the cache entry will expire.</param>
        public void Set(string key, object value, DateTimeOffset absoluteExpiration)
        {
            var policy = CachePolicy.WithAbsoluteExpiration(absoluteExpiration);

            Set(key, value, policy);
        }
Beispiel #28
0
        /// <summary>
        /// Inserts a cache entry into the cache without overwriting any existing cache entry.
        /// </summary>
        /// <param name="key">A unique identifier for the cache entry.</param>
        /// <param name="value">The object to insert.</param>
        /// <param name="absoluteExpiration">The fixed date and time at which the cache entry will expire.</param>
        /// <returns>
        ///   <c>true</c> if insertion succeeded, or <c>false</c> if there is an already an entry in the cache that has the same key as key.
        /// </returns>
        public bool Add(string key, object value, DateTimeOffset absoluteExpiration)
        {
            var cachePolicy = CachePolicy.WithAbsoluteExpiration(absoluteExpiration);

            return(Add(key, value, cachePolicy));
        }
        public void GetTest()
        {
            var cacheManager = new CacheManager();
            var cacheKey = new CacheKey("GetTest" + DateTime.Now.Ticks);
            var value = "Get Value " + DateTime.Now;
            var cachePolicy = new CachePolicy();

            bool result = cacheManager.Add(cacheKey, value, cachePolicy);
            result.Should().BeTrue();

            var existing = cacheManager.Get(cacheKey.Key);
            existing.Should().NotBeNull();
            existing.Should().BeSameAs(value);

        }
Beispiel #30
0
        /// <summary>
        /// Inserts a cache entry into the cache without overwriting any existing cache entry.
        /// </summary>
        /// <param name="key">A unique identifier for the cache entry.</param>
        /// <param name="value">The object to insert.</param>
        /// <returns><c>true</c> if insertion succeeded, or <c>false</c> if there is an already an entry in the cache that has the same key as key.</returns>
        public bool Add(string key, object value)
        {
            var cachePolicy = new CachePolicy();

            return(Add(key, value, cachePolicy));
        }
 /// <summary>
 /// Gets the cache value for the specified key that is already in the dictionary or the new value if the key was not in the dictionary.
 /// </summary>
 /// <param name="key">A unique identifier for the cache entry.</param>
 /// <param name="value">The object to insert.</param>
 /// <returns>
 /// The value for the key. This will be either the existing value for the key if the key is already in the dictionary, 
 /// or the new value if the key was not in the dictionary.
 /// </returns>
 public object GetOrAdd(string key, object value)
 {
     var policy = new CachePolicy();
     return GetOrAdd(key, value, policy);
 }
        /// <summary>
        /// Inserts a cache entry into the cache overwriting any existing cache entry.
        /// </summary>
        /// <param name="cacheKey">A unique identifier for the cache entry.</param>
        /// <param name="value">The object to insert.</param>
        /// <param name="cachePolicy">A <see cref="CachePolicy"/> that contains eviction details for the cache entry.</param>
        /// <returns></returns>
        public bool Set(CacheKey cacheKey, object value, CachePolicy cachePolicy)
        {
            string key = GetKey(cacheKey);
            var item = new CacheItem(key, value);
            var policy = CreatePolicy(cacheKey, cachePolicy);

            MemoryCache.Default.Set(item, policy);
            return true;
        }
 /// <summary>
 /// Gets the cache value for the specified key that is already in the dictionary or the new value if the key was not in the dictionary.
 /// </summary>
 /// <param name="key">A unique identifier for the cache entry.</param>
 /// <param name="value">The object to insert.</param>
 /// <param name="cachePolicy">An object that contains eviction details for the cache entry.</param>
 /// <returns>
 /// The value for the key. This will be either the existing value for the key if the key is already in the dictionary, 
 /// or the new value if the key was not in the dictionary.
 /// </returns>
 public object GetOrAdd(CacheKey key, object value, CachePolicy cachePolicy)
 {
     return GetOrAdd(key, k => value, cachePolicy);
 }
        internal static CacheItemPolicy CreatePolicy(CacheKey key, CachePolicy cachePolicy)
        {
            var policy = new CacheItemPolicy();

            switch (cachePolicy.Mode)
            {
                case CacheExpirationMode.Sliding:
                    policy.SlidingExpiration = cachePolicy.SlidingExpiration;
                    break;
                case CacheExpirationMode.Absolute:
                    policy.AbsoluteExpiration = cachePolicy.AbsoluteExpiration;
                    break;
                case CacheExpirationMode.Duration:
                    policy.AbsoluteExpiration = DateTimeOffset.Now.Add(cachePolicy.Duration);
                    break;
                default:
                    policy.AbsoluteExpiration = ObjectCache.InfiniteAbsoluteExpiration;
                    break;
            }

            var changeMonitor = CreateChangeMonitor(key);
            if (changeMonitor != null)
                policy.ChangeMonitors.Add(changeMonitor);

            return policy;
        }
        /// <summary>
        /// Gets the cache value for the specified key that is already in the dictionary or the new value for the key as returned by <paramref name="valueFactory"/>.
        /// </summary>
        /// <param name="cacheKey">A unique identifier for the cache entry.</param>
        /// <param name="valueFactory">The function used to generate a value to insert into cache.</param>
        /// <param name="cachePolicy">An object that contains eviction details for the cache entry.</param>
        /// <returns>
        /// The value for the key. This will be either the existing value for the key if the key is already in the dictionary, 
        /// or the new value for the key as returned by <paramref name="valueFactory"/> if the key was not in the dictionary.
        /// </returns>
        public virtual object GetOrAdd(CacheKey cacheKey, Func<CacheKey, object> valueFactory, CachePolicy cachePolicy)
        {
            var provider = ResolveProvider();
            var item = provider.GetOrAdd(cacheKey, valueFactory, cachePolicy);

            return item;
        }
Beispiel #36
0
        /// <summary>
        /// Inserts a cache entry into the cache overwriting any existing cache entry.
        /// </summary>
        /// <param name="cacheKey">A unique identifier for the cache entry.</param>
        /// <param name="value">The object to insert.</param>
        /// <param name="cachePolicy">An object that contains eviction details for the cache entry.</param>
        public virtual void Set(CacheKey cacheKey, object value, CachePolicy cachePolicy)
        {
            var provider = ResolveProvider();

            provider.Set(cacheKey, value, cachePolicy);
        }
 /// <summary>
 /// Inserts a cache entry into the cache overwriting any existing cache entry.
 /// </summary>
 /// <param name="key">A unique identifier for the cache entry.</param>
 /// <param name="value">The object to insert.</param>
 public void Set(string key, object value)
 {
     var policy = new CachePolicy();
     Set(key, value, policy);
 }
Beispiel #38
0
        /// <summary>
        /// Inserts a cache entry into the cache overwriting any existing cache entry.
        /// </summary>
        /// <param name="key">A unique identifier for the cache entry.</param>
        /// <param name="value">The object to insert.</param>
        public void Set(string key, object value)
        {
            var policy = new CachePolicy();

            Set(key, value, policy);
        }
 /// <summary>
 /// Inserts a cache entry into the cache overwriting any existing cache entry.
 /// </summary>
 /// <param name="cacheKey">A unique identifier for the cache entry.</param>
 /// <param name="value">The object to insert.</param>
 /// <param name="cachePolicy">An object that contains eviction details for the cache entry.</param>
 public virtual void Set(CacheKey cacheKey, object value, CachePolicy cachePolicy)
 {
     var provider = ResolveProvider();
     provider.Set(cacheKey, value, cachePolicy);
 }
        public void GetOrAddTest()
        {
            var cacheManager = new CacheManager();
            var cacheKey = new CacheKey("AddTest" + DateTime.Now.Ticks);
            var value = "Test Value " + DateTime.Now;
            var cachePolicy = new CachePolicy();
            int callCount = 0;

            Func<CacheKey, object> valueFactory = k =>
            {
                callCount++;
                return value;
            };

            var result = cacheManager.GetOrAdd(cacheKey, valueFactory, cachePolicy);
            result.Should().Be(value);
            callCount.Should().Be(1);

            var cachedValue = cacheManager.Get(cacheKey.Key);
            cachedValue.Should().NotBeNull();
            cachedValue.Should().Be(value);

            callCount = 0;
            var result2 = cacheManager.GetOrAdd(cacheKey, valueFactory, cachePolicy);
            result2.Should().Be(value);
            callCount.Should().Be(0);

        }
 /// <summary>
 /// Inserts a cache entry into the cache without overwriting any existing cache entry.
 /// </summary>
 /// <param name="key">A unique identifier for the cache entry.</param>
 /// <param name="value">The object to insert.</param>
 /// <param name="cachePolicy">An object that contains eviction details for the cache entry.</param>
 /// <returns>
 ///   <c>true</c> if insertion succeeded, or <c>false</c> if there is an already an entry in the cache that has the same key as key.
 /// </returns>
 public bool Add(string key, object value, CachePolicy cachePolicy)
 {
     var cacheKey = new CacheKey(key);
     return Add(cacheKey, value, cachePolicy);
 }
        public void RemoveTest()
        {
            var cacheManager = new CacheManager();
            var cacheKey = new CacheKey("AddTest" + DateTime.Now.Ticks);
            var value = "Test Value " + DateTime.Now;
            var cachePolicy = new CachePolicy();

            bool result = cacheManager.Add(cacheKey, value, cachePolicy);
            result.Should().BeTrue();

            // look in underlying MemoryCache
            var cachedValue = cacheManager.Get(cacheKey.Key);
            cachedValue.Should().NotBeNull();
            cachedValue.Should().Be(value);

            var removed = cacheManager.Remove(cacheKey);
            removed.Should().NotBeNull();
            removed.Should().Be(value);

            // look in underlying MemoryCache
            var previous = cacheManager.Get(cacheKey.Key);
            previous.Should().BeNull();
        }
        public void GetTest()
        {
            var provider = new MemoryCacheProvider();
            var cacheKey = new CacheKey("GetTest" + DateTime.Now.Ticks);
            var value = "Get Value " + DateTime.Now;
            var cachePolicy = new CachePolicy();

            bool result = provider.Add(cacheKey, value, cachePolicy);
            result.Should().BeTrue();

            var existing = provider.Get<string>(cacheKey);
            existing.Should().NotBeNull();
            existing.Should().BeSameAs(value);
        }
        public void SetTest()
        {
            var cacheManager = new CacheManager();
            var cacheKey = new CacheKey("SetTest" + DateTime.Now.Ticks);
            var value = "Set Value " + DateTime.Now;
            var cachePolicy = new CachePolicy();

            cacheManager.Set(cacheKey, value, cachePolicy);
            
            var cachedValue = cacheManager.Get(cacheKey.Key);
            cachedValue.Should().NotBeNull();
            cachedValue.Should().Be(value);

            var value2 = "Set Value " + DateTime.Now;
            cacheManager.Set(cacheKey, value2, cachePolicy);

            var cachedValue2 = cacheManager.Get(cacheKey.Key);
            cachedValue2.Should().NotBeNull();
            cachedValue2.Should().Be(value2);
        }
Beispiel #45
0
        /// <summary>
        /// Inserts a cache entry into the cache overwriting any existing cache entry.
        /// </summary>
        /// <param name="key">A unique identifier for the cache entry.</param>
        /// <param name="value">The object to insert.</param>
        /// <param name="slidingExpiration">A span of time within which a cache entry must be accessed before the cache entry is evicted from the cache.</param>
        public void Set(string key, object value, TimeSpan slidingExpiration)
        {
            var policy = CachePolicy.WithSlidingExpiration(slidingExpiration);

            Set(key, value, policy);
        }
Beispiel #46
0
        /// <summary>
        /// Inserts a cache entry into the cache overwriting any existing cache entry.
        /// </summary>
        /// <param name="key">A unique identifier for the cache entry.</param>
        /// <param name="value">The object to insert.</param>
        /// <param name="cachePolicy">An object that contains eviction details for the cache entry.</param>
        public void Set(string key, object value, CachePolicy cachePolicy)
        {
            var cacheKey = new CacheKey(key);

            Set(cacheKey, value, cachePolicy);
        }
Beispiel #47
0
        /// <summary>
        /// Gets the cache value for the specified key that is already in the dictionary or the new value for the key as returned asynchronously by <paramref name="valueFactory"/>.
        /// </summary>
        /// <param name="cacheKey">A unique identifier for the cache entry.</param>
        /// <param name="valueFactory">The asynchronous function used to generate a value to insert into cache.</param>
        /// <param name="cachePolicy">An object that contains eviction details for the cache entry.</param>
        /// <returns>
        /// The value for the key. This will be either the existing value for the key if the key is already in the dictionary,
        /// or the new value for the key as returned by <paramref name="valueFactory"/> if the key was not in the dictionary.
        /// </returns>
        public virtual Task <object> GetOrAddAsync(CacheKey cacheKey, Func <CacheKey, Task <object> > valueFactory, CachePolicy cachePolicy)
        {
            var provider = ResolveProvider();
            var item     = provider.GetOrAddAsync(cacheKey, valueFactory, cachePolicy);

            return(item);
        }
Beispiel #48
0
        /// <summary>
        /// Inserts a cache entry into the cache without overwriting any existing cache entry.
        /// </summary>
        /// <param name="key">A unique identifier for the cache entry.</param>
        /// <param name="value">The object to insert.</param>
        /// <param name="slidingExpiration">A span of time within which a cache entry must be accessed before the cache entry is evicted from the cache.</param>
        /// <returns>
        ///   <c>true</c> if insertion succeeded, or <c>false</c> if there is an already an entry in the cache that has the same key as key.
        /// </returns>
        public bool Add(string key, object value, TimeSpan slidingExpiration)
        {
            var cachePolicy = CachePolicy.WithSlidingExpiration(slidingExpiration);

            return(Add(key, value, cachePolicy));
        }