public void Should_Create_New_Item()
        {
            // Arrange
            const string key  = "TEST_KEY";
            var          tags = new[] { "tag1", "tag2" };

            // Act
            var result = _sut.Create(key, tags);

            // Assert
            result.Key.ShouldBe(key);
            result.Tags.ShouldBe(tags);
        }
        /// <summary>
        /// Sets an item into the cache under the given key with the associated tags
        /// </summary>
        /// <param name="client">The <see cref="RedisClient" /> to use to communicate with Redis</param>
        /// <param name="key">The key to set the item under</param>
        /// <param name="value">The value to store</param>
        /// <param name="expiry">When to expire the cached item</param>
        /// <param name="tags">The tags to assign to the cached value</param>
        /// <typeparam name="T">The type of the item being stored</typeparam>
        public Task <bool> Set <T>(RedisClient client, string key, T value, DateTime?expiry = null, params string[]?tags)
            where T : notnull
        {
            var cacheItem  = _cacheItemFactory.Create(key, value, expiry, tags);
            var serialized = _serializer.Serialize(cacheItem);

            return(client.ThrowIfNull(nameof(client)).Set(key, serialized, expiry));
        }
Пример #3
0
        public Task Save <TItem>(string key, TItem item, DateTime?expiry = null, params string[]?tags) where TItem : notnull
        {
            ExpiryCheck();

            var cacheKey  = GenerateKey <TItem>(key);
            var cacheItem = _cacheItemFactory.Create(cacheKey, item, expiry, tags);

            if (!CacheItemIsValid(cacheItem))
            {
                return(Task.CompletedTask);
            }

            KeyedCache[cacheKey] = JsonSerializer.ToJsonString(cacheItem);

            if (tags == null)
            {
                return(Task.CompletedTask);
            }

            if (!tags.Any())
            {
                return(Task.CompletedTask);
            }

            foreach (var tag in tags)
            {
                var tagKeyItem = TaggedKeys.ContainsKey(tag)
                    ? TaggedKeys[tag] ?? new List <string>()
                    : new List <string>();

                if (!tagKeyItem.Contains(cacheKey))
                {
                    tagKeyItem.Add(cacheKey);
                }

                TaggedKeys[tag] = tagKeyItem;
            }

            return(Task.CompletedTask);
        }
Пример #4
0
 /// <summary>
 /// Updates the tags for the given key
 /// </summary>
 /// <param name="client">The <see cref="RedisClient" /> to use to connect to Redis</param>
 /// <param name="key">The key to update tags upon</param>
 /// <param name="tags">The tags to update</param>
 public Task UpdateTags(RedisClient client, string key, params string[] tags)
 => UpdateTags(client, _cacheItemFactory.Create(key, tags));