Exemplo n.º 1
0
            public void ReturnsDifferentHashCodeWhenDifferentNameAndKey()
            {
                var key1 = new CacheEntryKey("key.1", 1);
                var key2 = new CacheEntryKey("key.2", 2);

                key1.GetHashCode().Should().NotBe(key2.GetHashCode());
            }
Exemplo n.º 2
0
            public void ReturnsFalseWhenOneKeyIsNull()
            {
                var           key1 = new CacheEntryKey("key.1", new object());
                CacheEntryKey key2 = null;

                key1.Equals(key2).Should().BeFalse();
            }
Exemplo n.º 3
0
            public void ReturnsFalseWhenDifferentNameAndKey()
            {
                var key1 = new CacheEntryKey("key.1", 1);
                var key2 = new CacheEntryKey("key.2", 2);

                key1.Equals(key2).Should().BeFalse();
            }
Exemplo n.º 4
0
            public void ReturnsTrueWhenSameKeyAndValue(string name, object key)
            {
                var key1 = new CacheEntryKey(name, key);
                var key2 = new CacheEntryKey(name, key);

                key1.Equals(key2).Should().BeTrue();
            }
Exemplo n.º 5
0
            public void ReturnsSameHashCodeWhenSameKeyAndValue(string name, object key)
            {
                var key1 = new CacheEntryKey(name, key);
                var key2 = new CacheEntryKey(name, key);

                key1.GetHashCode().Should().Be(key2.GetHashCode());
            }
Exemplo n.º 6
0
            public void ReturnsFalseWhenDifferentNameSameKey()
            {
                object keyValue = new object();
                var    key1     = new CacheEntryKey("key.1", keyValue);
                var    key2     = new CacheEntryKey("key.2", keyValue);

                key1.Equals(key2).Should().BeFalse();
            }
        public void TwoDifferentKeysAreDifferent(int userId1, int objectId1, int userId2, int objectId2)
        {
            CacheEntryKey key1 = new CacheEntryKey(userId1, objectId1);
            CacheEntryKey key2 = new CacheEntryKey(userId2, objectId2);

            Assert.AreNotEqual(key1.GetHashCode(), key2.GetHashCode());
            Assert.AreNotEqual(key1, key2);
        }
Exemplo n.º 8
0
            public void ReturnsDifferentHashCodeWhenDifferentNameSameKey()
            {
                object keyValue = new object();
                var    key1     = new CacheEntryKey("key.1", keyValue);
                var    key2     = new CacheEntryKey("key.2", keyValue);

                key1.GetHashCode().Should().NotBe(key2.GetHashCode());
            }
Exemplo n.º 9
0
            public void ReturnsFalseWhenSameNameDifferentKey()
            {
                object keyValue1 = new object();
                object keyValue2 = new object();
                var    key1      = new CacheEntryKey("key.1", keyValue1);
                var    key2      = new CacheEntryKey("key.1", keyValue2);

                key1.Equals(key2).Should().BeFalse();
            }
Exemplo n.º 10
0
            public void ReturnsDifferentHashCodeWhenSameNameDifferentKey()
            {
                object keyValue1 = new object();
                object keyValue2 = new object();
                var    key1      = new CacheEntryKey("key.1", keyValue1);
                var    key2      = new CacheEntryKey("key.1", keyValue2);

                key1.GetHashCode().Should().NotBe(key2.GetHashCode());
            }
Exemplo n.º 11
0
        public override bool Equals(object obj)
        {
            if (!(obj is CacheEntryKey))
            {
                return(false);
            }
            CacheEntryKey val = (CacheEntryKey)obj;

            return(_userId == val.UserId && ObjectId == val.ObjectId);
        }
Exemplo n.º 12
0
        public IActionResult HeadCache(int userId, int objId)
        {
            CacheEntryKey key    = new CacheEntryKey(userId, objId);
            bool          result = _cache.TryGetValue(key, out _);

            if (result)
            {
                return(Ok());
            }
            return(NotFound());
        }
Exemplo n.º 13
0
        public IActionResult GetCache(int userId, int objId)
        {
            CacheEntryKey key    = new CacheEntryKey(userId, objId);
            bool          result = _cache.TryGetValue(key, out object value);

            if (result)
            {
                return(Ok(value));
            }
            return(NotFound("Cached item not found!"));
        }
        public void CanHeadUnexistedItem()
        {
            var    key   = new CacheEntryKey(1, 1);
            object value = null;

            _cache.Setup(c => c.TryGetValue(key, out value)).Returns(false);

            var actual = _controller.HeadCache(1, 1);

            Assert.IsInstanceOf <NotFoundResult>(actual);
        }
        public void CanAddSameItemToCache()
        {
            var    key   = new CacheEntryKey(1, 1);
            object value = null;

            _cache.Setup(c => c.TryGetValue(key, out value)).Returns(true);

            var actual = _controller.AddToCache(1, 1, null, new object());

            Assert.IsInstanceOf <OkObjectResult>(actual);
            Assert.AreEqual("Object with the same key already exists!", (actual as OkObjectResult).Value);
        }
        public void CanDeleteExistingItem()
        {
            var    key   = new CacheEntryKey(1, 1);
            object value = null;

            _cache.Setup(c => c.TryGetValue(key, out value)).Returns(true);

            var actual = _controller.DeleteCache(1, 1);

            Assert.IsInstanceOf <OkObjectResult>(actual);
            Assert.AreEqual("Item was successfully deleted", (actual as OkObjectResult).Value);
        }
        public void CanAddItemToCache()
        {
            var    key   = new CacheEntryKey(1, 1);
            object value = null;

            _cache.Setup(c => c.TryGetValue(key, out value)).Returns(false);

            var actual = _controller.AddToCache(1, 1, null, new object());

            Assert.IsInstanceOf <OkObjectResult>(actual);
            Assert.AreEqual("Successfully added object to cache", (actual as OkObjectResult).Value);
        }
        public void CanGetUnexistedItem()
        {
            var    key   = new CacheEntryKey(1, 1);
            object value = null;

            _cache.Setup(c => c.TryGetValue(key, out value)).Returns(false);

            var actual = _controller.GetCache(1, 1);

            Assert.IsInstanceOf <NotFoundObjectResult>(actual);
            Assert.AreEqual("Cached item not found!", (actual as NotFoundObjectResult).Value);
        }
Exemplo n.º 19
0
            public void ReturnsTrueWhenSameKeyAndComplexValue()
            {
                DateTimeOffset now  = DateTimeOffset.Now;
                var            key1 = new CacheEntryKey("key-test-xxx", new TestClass {
                    Count = 1, DateCreated = now
                });
                var key2 = new CacheEntryKey("key-test-xxx", new TestClass {
                    Count = 1, DateCreated = now
                });

                key1.Equals(key2).Should().BeTrue();
            }
        public void CanGetExistingItem()
        {
            var    key   = new CacheEntryKey(1, 1);
            object value = "Retrieved item";

            _cache.Setup(c => c.TryGetValue(key, out value)).Returns(true);

            var actual = _controller.GetCache(1, 1);

            Assert.IsInstanceOf <OkObjectResult>(actual);
            Assert.AreEqual("Retrieved item", (actual as OkObjectResult).Value);
        }
Exemplo n.º 21
0
        public IActionResult DeleteCache(int userId, int objId)
        {
            CacheEntryKey key    = new CacheEntryKey(userId, objId);
            bool          result = _cache.TryGetValue(key, out _);

            if (result)
            {
                _cache.Remove(key);
                return(Ok("Item was successfully deleted"));
            }

            return(NotFound("Item to delete was not found!"));
        }
Exemplo n.º 22
0
        public IActionResult AddToCache(int userId, int objId, int?lifetime, [FromBody] object obj)
        {
            CacheEntryKey key = new CacheEntryKey(userId, objId);

            if (!_cache.TryGetValue(key, out _))
            {
                MemoryCacheEntryOptions options = new MemoryCacheEntryOptions();
                if (lifetime.HasValue && lifetime.Value > 0)
                {
                    options.SetSlidingExpiration(TimeSpan.FromSeconds(lifetime.Value));
                }
                else
                {
                    options.SetPriority(CacheItemPriority.NeverRemove);
                }
                _cache.Set(key, obj, options);
                return(Ok("Successfully added object to cache"));
            }

            return(Ok("Object with the same key already exists!"));
        }
Exemplo n.º 23
0
 public async Task <CacheEntry> GetEntryAsync(PrimaryCacheKey primaryKey, CacheEntryKey entryKey)
 {
     return(null);
 }