Exemplo n.º 1
0
        /// <summary>
        /// Inserts or updates the value in the cache.
        /// </summary>
        /// <param name="key">Cache Key</param>
        /// <param name="value">Value to add or update</param>
        /// <param name="timeout">Timespan of time to live in the cache. If null, then it will stay in the cache indefinitely.</param>
        /// <param name="partitionName">
        /// Optional value for the name of a partition in the application cache. Partitions can be a good way to categorize or group
        /// certain types of items in the cache together.
        /// </param>
        public async Task AddOrUpdateAsync(string key, object value, TimeSpan?timeout, string partitionName = "")
        {
            if (string.IsNullOrEmpty(key) || value == null)
            {
                return;
            }
            var cacheValue = JsonConvert.SerializeObject(value);

            if (string.IsNullOrEmpty(partitionName))
            {
                if (timeout == null)
                {
                    await _redisDatabase.StringSetAsync(key, cacheValue).ConfigureAwait(false);
                }
                else
                {
                    await _redisDatabase.StringSetAsync(key, cacheValue, timeout).ConfigureAwait(false);
                }
            }
            else
            {
                var partitionKey = ComposePartitionKey(partitionName);
                await _redisDatabase.SetAddAsync(AllPartitionsCacheKey, partitionKey).ConfigureAwait(false);

                await _redisDatabase.HashSetAsync(partitionKey, key, cacheValue).ConfigureAwait(false);
                await AddTimeoutToPartitionAsync(partitionName, key, timeout).ConfigureAwait(false);
            }
        }
 /// <summary>
 /// 在 hash 设定值
 /// </summary>
 /// <param name="redisKey"></param>
 /// <param name="hashField"></param>
 /// <param name="value"></param>
 /// <returns></returns>
 public async Task <bool> HashSetAsync(string redisKey, string hashField, string value)
 {
     redisKey = GetKeyForRedis(redisKey);
     return(await _database.HashSetAsync(redisKey, hashField, value));
 }