Esempio n. 1
0
        public void AddWithTagsTest()
        {
            var    provider = new MemoryCacheProvider();
            string key      = "AddTest" + 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();
        }
Esempio n. 2
0
        public void RemoveTest()
        {
            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);

            var removed = provider.Remove(cacheKey);

            removed.Should().NotBeNull();
            removed.Should().Be(value);

            // look in underlying MemoryCache
            var previous = MemoryCache.Default.Get(innerKey);

            previous.Should().BeNull();
        }
Esempio n. 3
0
        public void AddWithExistingTagTest()
        {
            var    provider = new MemoryCacheProvider();
            string key      = "AddTest" + 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 = "AddTest2" + 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);
        }
Esempio n. 4
0
        /// <summary>
        /// Gets the story.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns></returns>
        public Story GetStory(int id)
        {
            if (MemoryCacheProvider.GetValue(id.ToString()) is Story story)
            {
                return(story);
            }

            story = _reader.GetStory(id);
            int cacheStoriesInDays = Convert.ToInt32(_configuration["CacheStoriesInDays"]);

            MemoryCacheProvider.Add(id.ToString(), story, DateTime.Now.AddDays(cacheStoriesInDays));
            return(story);
        }
        public void Adds_Function_Correctly()
        {
            var collection = new List<KeyValuePair<string, CachedValue<string>>>
            {
                new KeyValuePair<string, CachedValue<string>>("key2", new CachedValue<string>("value2")),
                new KeyValuePair<string, CachedValue<string>>("key3", new CachedValue<string>("value3")),
            };

            using (var provider = new MemoryCacheProvider<string, string>())
            {
                provider.Add("key1", new CachedValue<string>("value2"));
                provider.Add("key1", new CachedValue<string>("value1"));
                provider.Add(collection);
                Assert.AreEqual(3, provider.Count());
                Assert.AreEqual("value1", provider.Get("key1").Value);
                Assert.IsTrue(provider.Get("non-existant").IsExpired);
                foreach (var value in provider.Get(new List<string> { "key2", "key3" }))
                {
                    Assert.IsTrue(collection.Any(item => item.Value.Value == value.Value));
                }
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Gets the best story ids.
        /// </summary>
        /// <param name="number">The number of ids to be returned.</param>
        /// <returns></returns>
        public IEnumerable <int> GetBestStoryIds(int number)
        {
            if (MemoryCacheProvider.GetValue("Ids") is IEnumerable <int> storyIds)
            {
                return(storyIds);
            }

            int numberOfStories = Convert.ToInt32(_configuration["NumberOfStories"]);

            storyIds = _reader.GetBestStoriesIds().Take(numberOfStories);
            int cacheIdsMinutes = Convert.ToInt32(_configuration["CacheIdsMinutes"]);

            MemoryCacheProvider.Add("Ids", storyIds, DateTime.Now.AddMinutes(cacheIdsMinutes));
            return(storyIds);
        }
Esempio n. 7
0
        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(cacheKey);

            existing.Should().NotBeNull();
            existing.Should().BeSameAs(value);
        }
Esempio n. 8
0
        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         = "AddTest" + 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         = "AddTest2" + 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         = "AddTest3" + 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();

            // 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(cacheKey);

            expiredValue.Should().BeNull();

            var expiredValue2 = provider.Get(cacheKey2);

            expiredValue2.Should().BeNull();

            var expiredValue3 = provider.Get(cacheKey3);

            expiredValue3.Should().NotBeNull();

            cache.GetCount().Should().Be(4);
        }
Esempio n. 9
0
        public async Task AddItemsToCacheTest()
        {
            _cacheProvider.Initialize("test", _enabledSettings);
            const string key = "TestKey";

            Assert.IsTrue(await _cacheProvider.Add(key, new object(), "FirstRegion", new CacheOptions()));
            var count = await _cacheProvider.Count("");

            Assert.AreEqual(1, count);
        }