示例#1
0
        /// <summary>
        /// Gets a regular Object out of the cache.
        /// </summary>
        /// <param name="key">Cache key</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>
        /// <returns>Regular (untyped) C# object representing the value in the cache</returns>
        public async Task <object> GetAsync(string key, string partitionName = "")
        {
            if (string.IsNullOrEmpty(key))
            {
                return(null);
            }
            RedisDb.RedisValue cacheValue;
            if (string.IsNullOrEmpty(partitionName))
            {
                cacheValue = await _redisDatabase.StringGetAsync(key).ConfigureAwait(false);

                if (cacheValue.IsNullOrEmpty)
                {
                    return(null);
                }
            }
            else
            {
                var partitionKey = ComposePartitionKey(partitionName);

                // Check to see if the value has timed out
                var timeout = await GetTimeoutValueAsync(partitionName, key).ConfigureAwait(false);

                if (timeout != default(DateTimeOffset) && timeout < DateTimeOffset.UtcNow)
                {
                    await RemoveExpiredItemsFromPartitionAsync(partitionName).ConfigureAwait(false);

                    return(null);
                }

                // Retrieve the actual RedisValue
                cacheValue = await _redisDatabase.HashGetAsync(partitionKey, key).ConfigureAwait(false);

                // If we have a null value, return null
                if (cacheValue.IsNullOrEmpty)
                {
                    return(null);
                }
            }
            var objValue = JsonConvert.DeserializeObject(cacheValue);

            return(objValue);
        }
 /// <summary>
 /// 在 hash 中获取值
 /// </summary>
 /// <param name="redisKey"></param>
 /// <param name="hashField"></param>
 /// <returns></returns>
 public async Task <RedisValue> HashGetAsync(string redisKey, string hashField)
 {
     redisKey = GetKeyForRedis(redisKey);
     return(await _database.HashGetAsync(redisKey, hashField));
 }