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);
        }
        //[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 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();
        }
Exemple #4
0
 public void KeyTest()
 {
     string key = DateTime.Now.Ticks.ToString(CultureInfo.InvariantCulture);
     var target = new CacheKey(key);
     target.Should().NotBeNull();
     target.Key.Should().NotBeNull();
     target.Key.Should().Be(key);
 }
Exemple #5
0
 public void CacheKeyConstructorTest1()
 {
     string key = string.Empty;
     var target = new CacheKey(key);
     target.Should().NotBeNull();
     target.Key.Should().NotBeNull();
     target.Key.Should().Be(string.Empty);
 }
        /// <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;
        }
        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();
        }
Exemple #8
0
        public void TagsTest()
        {
            string key = DateTime.Now.Ticks.ToString(CultureInfo.InvariantCulture);
            string[] tags = new[] { "a", "b" };
            var target = new CacheKey(key, tags);

            target.Should().NotBeNull();
            target.Key.Should().NotBeNull();
            target.Key.Should().Be(key);

            target.Tags.Should().HaveCount(2);
        }
        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;
        }
Exemple #11
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;
        }
        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);
        }
        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);
        }
        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);
        }
        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);
        }
 /// <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 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);
 }
        /// <summary>
        /// Removes a cache entry from the cache. 
        /// </summary>
        /// <param name="cacheKey">A unique identifier for the cache entry.</param>
        /// <returns>If the entry is found in the cache, the removed cache entry; otherwise, <see langword="null"/>.</returns>
        public virtual object Remove(CacheKey cacheKey)
        {
            var provider = ResolveProvider();
            var item = provider.Remove(cacheKey);

            return item;
        }
 /// <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);
 }
        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);

        }
 /// <summary>
 /// Removes a cache entry from the cache. 
 /// </summary>
 /// <param name="key">A unique identifier for the cache entry.</param>
 /// <returns>If the entry is found in the cache, the removed cache entry; otherwise, <see langword="null"/>.</returns>
 public object Remove(string key)
 {
     var cacheKey = new CacheKey(key);
     return Remove(cacheKey);
 }
 /// <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>
 /// 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(CacheKey key, object value, CachePolicy cachePolicy)
 {
     return GetOrAdd(key, k => value, cachePolicy);
 }
        public void CreateChangeMonitorTest()
        {
            string key = DateTime.Now.Ticks.ToString(CultureInfo.InvariantCulture);
            string[] tags = new[] { "a", "b" };
            var cacheKey = new CacheKey(key, tags);
            cacheKey.Should().NotBeNull();

            var monitor = MemoryCacheProvider.CreateChangeMonitor(cacheKey);
            monitor.Should().NotBeNull();
            monitor.CacheKeys.Should().HaveCount(2);

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

            monitor.CacheKeys.Should().Contain(tagKey);
        }
 /// <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);
 }
        public void CreatePolicySlidingTest()
        {
            string key = DateTime.Now.Ticks.ToString(CultureInfo.InvariantCulture);
            string[] tags = new[] { "a", "b" };
            var cacheKey = new CacheKey(key, tags);
            cacheKey.Should().NotBeNull();

            var slidingExpiration = TimeSpan.FromMinutes(5);
            var cachePolicy = CachePolicy.WithSlidingExpiration(slidingExpiration);
            cachePolicy.Should().NotBeNull();

            var policy = MemoryCacheProvider.CreatePolicy(cacheKey, cachePolicy);
            policy.Should().NotBeNull();
            policy.SlidingExpiration.Should().Be(slidingExpiration);
            policy.ChangeMonitors.Should().NotBeNull();
            policy.ChangeMonitors.Should().HaveCount(1);
            policy.ChangeMonitors.Should().ContainItemsAssignableTo<CacheEntryChangeMonitor>();
        }
        /// <summary>
        /// Gets the cache value for the specified key
        /// </summary>
        /// <param name="key">A unique identifier for the cache entry.</param>
        /// <returns>The cache value for the specified key, if the entry exists; otherwise, <see langword="null"/>.</returns>
        public virtual object Get(string key)
        {
            var cacheKey = new CacheKey(key);

            var provider = ResolveProvider();
            var item = provider.Get(cacheKey);

            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="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;
        }
        /// <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;
        }