Ejemplo n.º 1
0
        /// <summary>
        /// Gets all async.
        /// </summary>
        /// <returns>The all async.</returns>
        /// <param name="cacheKeys">Cache keys.</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        public async Task <IDictionary <string, CacheValue <T> > > GetAllAsync <T>(IEnumerable <string> cacheKeys) where T : class
        {
            ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys));

            var keyArray = cacheKeys.ToArray();
            var values   = await _cache.StringGetAsync(keyArray.Select(k => (RedisKey)k).ToArray());

            var result = new Dictionary <string, CacheValue <T> >();

            for (int i = 0; i < keyArray.Length; i++)
            {
                var cachedValue = values[i];
                if (!cachedValue.IsNull)
                {
                    result.Add(keyArray[i], new CacheValue <T>(_serializer.Deserialize <T>(cachedValue), true));
                }
                else
                {
                    result.Add(keyArray[i], CacheValue <T> .NoValue);
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
        public override void BaseRemoveAll(IEnumerable <string> cacheKeys)
        {
            ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys));

            foreach (string key in cacheKeys)
            {
                if (string.IsNullOrWhiteSpace(key))
                {
                    continue;
                }

                var path = GetRawPath(key);

                if (!File.Exists(path))
                {
                    continue;
                }

                if (DeleteFileWithRetry(path))
                {
                    _cacheKeysMap.TryRemove(key, out _);
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets all.
        /// </summary>
        /// <returns>The all.</returns>
        /// <param name="cacheKeys">Cache keys.</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        public override IDictionary <string, CacheValue <T> > BaseGetAll <T>(IEnumerable <string> cacheKeys)
        {
            ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys));

            var result = new Dictionary <string, CacheValue <T> >();

            //maybe we should use mget here based on redis mode
            //multiple keys may trigger `don't hash to the same slot`

            foreach (var item in cacheKeys)
            {
                var cachedValue = _cache.Get <byte[]>(item);
                if (cachedValue != null)
                {
                    result.Add(item, new CacheValue <T>(_serializer.Deserialize <T>(cachedValue), true));
                }
                else
                {
                    result.Add(item, CacheValue <T> .NoValue);
                }
            }

            return(result);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Gets all.
        /// </summary>
        /// <returns>The all.</returns>
        /// <param name="cacheKeys">Cache keys.</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        protected override IDictionary <string, CacheValue <T> > BaseGetAll <T>(IEnumerable <string> cacheKeys)
        {
            ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys));

            var keyArray = cacheKeys.ToArray();
            var values   = _redisDb.StringGet(keyArray.Select(k => (RedisKey)k).ToArray());

            var result = new Dictionary <string, CacheValue <T> >();

            for (int i = 0; i < keyArray.Length; i++)
            {
                var cachedValue = values[i];
                if (!cachedValue.IsNull)
                {
                    result.Add(keyArray[i], new CacheValue <T>(_serializer.Deserialize <T>(cachedValue), true));
                }
                else
                {
                    result.Add(keyArray[i], CacheValue <T> .NoValue);
                }
            }

            return(result);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Sets all.
 /// </summary>
 /// <param name="values">Values.</param>
 /// <param name="expiration">Expiration.</param>
 /// <typeparam name="T">The 1st type parameter.</typeparam>
 public override void BaseSetAll <T>(IDictionary <string, T> values, TimeSpan expiration)
 {
     ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration));
     ArgumentCheck.NotNullAndCountGTZero(values, nameof(values));
     _litedb.BeginTrans();
     try
     {
         foreach (var item in values)
         {
             _cache.Upsert(new CacheItem
             {
                 cachekey   = item.Key,
                 name       = _name,
                 cachevalue = Newtonsoft.Json.JsonConvert.SerializeObject(item.Value),
                 expiration = DateTimeOffset.UtcNow.Add(expiration).ToUnixTimeSeconds()
             });
         }
         _litedb.Commit();
     }
     catch (Exception)
     {
         _litedb.Rollback();
     }
 }
Ejemplo n.º 6
0
        public void NotNullAndCountGTZero_Dict_Should_Throw_ArgumentNullException_When_Argument_Is_Null()
        {
            Dictionary <string, string> dict = null;

            Assert.Throws <ArgumentNullException>(() => ArgumentCheck.NotNullAndCountGTZero(dict, nameof(dict)));
        }
Ejemplo n.º 7
0
        public void NotNullAndCountGTZero_List_Should_Throw_ArgumentNullException_When_Argument_Is_Empty()
        {
            var list = new List <string>();

            Assert.Throws <ArgumentNullException>(() => ArgumentCheck.NotNullAndCountGTZero(list, nameof(list)));
        }
 /// <summary>
 /// Removes all async.
 /// </summary>
 /// <returns>The all async.</returns>
 /// <param name="cacheKeys">Cache keys.</param>
 public override async Task BaseRemoveAllAsync(IEnumerable <string> cacheKeys)
 {
     ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys));
     await Task.Run(() => BaseRemoveAll(cacheKeys));
 }
 /// <summary>
 /// Gets all async.
 /// </summary>
 /// <returns>The all async.</returns>
 /// <param name="cacheKeys">Cache keys.</param>
 /// <typeparam name="T">The 1st type parameter.</typeparam>
 public override async Task <IDictionary <string, CacheValue <T> > > BaseGetAllAsync <T>(IEnumerable <string> cacheKeys)
 {
     ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys));
     return(await Task.Run(() => BaseGetAll <T>(cacheKeys)));
 }
Ejemplo n.º 10
0
        /// <summary>
        /// Get the expiration of cache key
        /// </summary>
        /// <param name="cacheKey">cache key</param>
        /// <returns>expiration</returns>
        protected override async Task BaseKeyExpireAsync(IEnumerable <string> cacheKeys, int seconds)
        {
            ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys));

            await _redisDb.KeyExpireAsync(cacheKeys, seconds);
        }