using System; using Fantasy.RedisRepository.CommonHelper; using StackExchange.Redis; using System.Threading.Tasks; namespace Fantasy.RedisRepository.RedisHelpers { /// <summary> /// Redis异步操作类 String部分类 /// </summary> internal partial class RedisHelper// : IRedisHelper { private static IDatabase _client; internal RedisHelper() { _client = RedisConnection.GenerateConnection.GetDatabase(); } #region String 写操作 /// <summary> /// 将任何数据添加到redis中 /// </summary> /// <typeparam></typeparam> /// <param></param> /// <param></param> /// <param></param> /// <returns></returns> public async Task<bool> StringSetAsync<T>(string key, T value, TimeSpan? timeout = null) { return await _client.StringSetAsync(key, SerializeHelper.Serialize(value), timeout); } public async Task<long> StringDecrementAsync(string key, long value = 1L) { return await _client.StringDecrementAsync(key, value); } public async Task<long> StringIncrementAsync(string key, long value = 1L) { return await _client.StringIncrementAsync(key, value); } #endregion #region String 读操作 /// <summary> /// 根据key获取指定类型数据 /// </summary> /// <typeparam></typeparam> /// <param></param> /// <returns></returns> public async Task<T> StringGetAsync<T>(string key) { return SerializeHelper.Deserialize<T>(await _client.StringGetAsync(key, CommandFlags.PreferSlave)); } #endregion } }
RedisHelper部分类RedisHashHelperAsync.cs