切换导航
{{systemName}}
{{ info.Title }}
{{info.Title}}
{{ menu.Title }}
{{menu.Title}}
登录
|
退出
搜索
.net core 中使用redis
作者:ych
#### 安装依赖 ``` Install-Package StackExchange.Redis.Extensions.NetCore -Version 1.0.1 ``` #### 创建redishelper ``` using System; using System.Collections.Generic; using System.Configuration; using System.IO; using System.Linq; using System.Runtime.Serialization.Formatters.Binary; using System.Threading.Tasks; using StackExchange.Redis; namespace Common { ///
/// Redis 助手 ///
public class RedisHelper { ///
/// 连接字符串 ///
private static readonly string ConnectionString; ///
/// redis 连接对象 ///
private static readonly IConnectionMultiplexer ConnMultiplexer; ///
/// 默认的 Key 值(用来当作 RedisKey 的前缀) ///
private static readonly string DefaultKey; ///
/// 锁 ///
private static readonly object Locker = new object(); ///
/// 数据库 ///
private readonly IDatabase _db; ///
/// 获取 Redis 连接对象 ///
///
public IConnectionMultiplexer GetConnectionRedisMultiplexer() { if ((ConnMultiplexer == null) || !ConnMultiplexer.IsConnected) { lock (Locker) { if ((ConnMultiplexer == null) || !ConnMultiplexer.IsConnected) ConnectionMultiplexer.Connect(ConnectionString); } } return ConnMultiplexer; } #region 其它 public ITransaction GetTransaction() { return _db.CreateTransaction(); } #endregion 其它 #region 构造函数 static RedisHelper() { ConnectionString = ConfigHelper.GetSectionValue("Redis:Conn"); ConnMultiplexer = ConnectionMultiplexer.Connect(ConnectionString); DefaultKey = ConfigHelper.GetSectionValue("Redis:DefaultKey"); AddRegisterEvent(); } public RedisHelper(int db = -1) { _db = ConnMultiplexer.GetDatabase(db); } #endregion 构造函数 #region String 操作 ///
/// 设置 key 并保存字符串(如果 key 已存在,则覆盖值) ///
///
///
///
///
public bool StringSet(string redisKey, string redisValue, TimeSpan? expiry = null) { redisKey = AddKeyPrefix(redisKey); return _db.StringSet(redisKey, redisValue, expiry); } ///
/// 保存多个 Key-value ///
///
///
public bool StringSet(IEnumerable
> keyValuePairs) { keyValuePairs = keyValuePairs.Select(x => new KeyValuePair
(AddKeyPrefix(x.Key), x.Value)); return _db.StringSet(keyValuePairs.ToArray()); } ///
/// 获取字符串 ///
///
///
///
public string StringGet(string redisKey, TimeSpan? expiry = null) { redisKey = AddKeyPrefix(redisKey); return _db.StringGet(redisKey); } ///
/// 存储一个对象(该对象会被序列化保存) ///
///
///
///
///
public bool StringSet
(string redisKey, T redisValue, TimeSpan? expiry = null) { redisKey = AddKeyPrefix(redisKey); var json = Serialize(redisValue); return _db.StringSet(redisKey, json, expiry); } ///
/// 获取一个对象(会进行反序列化) ///
///
///
///
public T StringGet
(string redisKey, TimeSpan? expiry = null) { redisKey = AddKeyPrefix(redisKey); return Deserialize
(_db.StringGet(redisKey)); } #region async ///
/// 保存一个字符串值 ///
///
///
///
///
public async Task
StringSetAsync(string redisKey, string redisValue, TimeSpan? expiry = null) { redisKey = AddKeyPrefix(redisKey); return await _db.StringSetAsync(redisKey, redisValue, expiry); } ///
/// 保存一组字符串值 ///
///
///
public async Task
StringSetAsync(IEnumerable
> keyValuePairs) { keyValuePairs = keyValuePairs.Select(x => new KeyValuePair
(AddKeyPrefix(x.Key), x.Value)); return await _db.StringSetAsync(keyValuePairs.ToArray()); } ///
/// 获取单个值 ///
///
///
///
///
public async Task
StringGetAsync(string redisKey, string redisValue, TimeSpan? expiry = null) { redisKey = AddKeyPrefix(redisKey); return await _db.StringGetAsync(redisKey); } ///
/// 存储一个对象(该对象会被序列化保存) ///
///
///
///
///
public async Task
StringSetAsync
(string redisKey, T redisValue, TimeSpan? expiry = null) { redisKey = AddKeyPrefix(redisKey); var json = Serialize(redisValue); return await _db.StringSetAsync(redisKey, json, expiry); } ///
/// 获取一个对象(会进行反序列化) ///
///
///
///
public async Task
StringGetAsync
(string redisKey, TimeSpan? expiry = null) { redisKey = AddKeyPrefix(redisKey); return Deserialize
(await _db.StringGetAsync(redisKey)); } #endregion async #endregion String 操作 #region Hash 操作 ///
/// 判断该字段是否存在 hash 中 ///
///
///
///
public bool HashExists(string redisKey, string hashField) { redisKey = AddKeyPrefix(redisKey); return _db.HashExists(redisKey, hashField); } ///
/// 从 hash 中移除指定字段 ///
///
///
///
public bool HashDelete(string redisKey, string hashField) { redisKey = AddKeyPrefix(redisKey); return _db.HashDelete(redisKey, hashField); } ///
/// 从 hash 中移除指定字段 ///
///
///
///
public long HashDelete(string redisKey, IEnumerable
hashField) { redisKey = AddKeyPrefix(redisKey); return _db.HashDelete(redisKey, hashField.ToArray()); } ///
/// 在 hash 设定值 ///
///
///
///
///
public bool HashSet(string redisKey, string hashField, string value) { redisKey = AddKeyPrefix(redisKey); return _db.HashSet(redisKey, hashField, value); } ///
/// 在 hash 中设定值 ///
///
///
public void HashSet(string redisKey, IEnumerable
hashFields) { redisKey = AddKeyPrefix(redisKey); _db.HashSet(redisKey, hashFields.ToArray()); } ///
/// 在 hash 中获取值 ///
///
///
///
public RedisValue HashGet(string redisKey, string hashField) { redisKey = AddKeyPrefix(redisKey); return _db.HashGet(redisKey, hashField); } ///
/// 在 hash 中获取值 ///
///
///
///
///
public RedisValue[] HashGet(string redisKey, RedisValue[] hashField, string value) { redisKey = AddKeyPrefix(redisKey); return _db.HashGet(redisKey, hashField); } ///
/// 从 hash 返回所有的字段值 ///
///
///
public IEnumerable
HashKeys(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.HashKeys(redisKey); } ///
/// 返回 hash 中的所有值 ///
///
///
public RedisValue[] HashValues(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.HashValues(redisKey); } ///
/// 在 hash 设定值(序列化) ///
///
///
///
///
public bool HashSet
(string redisKey, string hashField, T value) { redisKey = AddKeyPrefix(redisKey); var json = Serialize(value); return _db.HashSet(redisKey, hashField, json); } ///
/// 在 hash 中获取值(反序列化) ///
///
///
///
public T HashGet
(string redisKey, string hashField) { redisKey = AddKeyPrefix(redisKey); return Deserialize
(_db.HashGet(redisKey, hashField)); } #region async ///
/// 判断该字段是否存在 hash 中 ///
///
///
///
public async Task
HashExistsAsync(string redisKey, string hashField) { redisKey = AddKeyPrefix(redisKey); return await _db.HashExistsAsync(redisKey, hashField); } ///
/// 从 hash 中移除指定字段 ///
///
///
///
public async Task
HashDeleteAsync(string redisKey, string hashField) { redisKey = AddKeyPrefix(redisKey); return await _db.HashDeleteAsync(redisKey, hashField); } ///
/// 从 hash 中移除指定字段 ///
///
///
///
public async Task
HashDeleteAsync(string redisKey, IEnumerable
hashField) { redisKey = AddKeyPrefix(redisKey); return await _db.HashDeleteAsync(redisKey, hashField.ToArray()); } ///
/// 在 hash 设定值 ///
///
///
///
///
public async Task
HashSetAsync(string redisKey, string hashField, string value) { redisKey = AddKeyPrefix(redisKey); return await _db.HashSetAsync(redisKey, hashField, value); } ///
/// 在 hash 中设定值 ///
///
///
public async Task HashSetAsync(string redisKey, IEnumerable
hashFields) { redisKey = AddKeyPrefix(redisKey); await _db.HashSetAsync(redisKey, hashFields.ToArray()); } ///
/// 在 hash 中获取值 ///
///
///
///
public async Task
HashGetAsync(string redisKey, string hashField) { redisKey = AddKeyPrefix(redisKey); return await _db.HashGetAsync(redisKey, hashField); } ///
/// 在 hash 中获取值 ///
///
///
///
///
public async Task
> HashGetAsync(string redisKey, RedisValue[] hashField, string value) { redisKey = AddKeyPrefix(redisKey); return await _db.HashGetAsync(redisKey, hashField); } ///
/// 从 hash 返回所有的字段值 ///
///
///
public async Task
> HashKeysAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.HashKeysAsync(redisKey); } ///
/// 返回 hash 中的所有值 ///
///
///
public async Task
> HashValuesAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.HashValuesAsync(redisKey); } ///
/// 在 hash 设定值(序列化) ///
///
///
///
///
public async Task
HashSetAsync
(string redisKey, string hashField, T value) { redisKey = AddKeyPrefix(redisKey); var json = Serialize(value); return await _db.HashSetAsync(redisKey, hashField, json); } ///
/// 在 hash 中获取值(反序列化) ///
///
///
///
public async Task
HashGetAsync
(string redisKey, string hashField) { redisKey = AddKeyPrefix(redisKey); return Deserialize
(await _db.HashGetAsync(redisKey, hashField)); } #endregion async #endregion Hash 操作 #region List 操作 ///
/// 移除并返回存储在该键列表的第一个元素 ///
///
///
public string ListLeftPop(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.ListLeftPop(redisKey); } ///
/// 移除并返回存储在该键列表的最后一个元素 ///
///
///
public string ListRightPop(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.ListRightPop(redisKey); } ///
/// 移除列表指定键上与该值相同的元素 ///
///
///
///
public long ListRemove(string redisKey, string redisValue) { redisKey = AddKeyPrefix(redisKey); return _db.ListRemove(redisKey, redisValue); } ///
/// 在列表尾部插入值。如果键不存在,先创建再插入值 ///
///
///
///
public long ListRightPush(string redisKey, string redisValue) { redisKey = AddKeyPrefix(redisKey); return _db.ListRightPush(redisKey, redisValue); } ///
/// 在列表头部插入值。如果键不存在,先创建再插入值 ///
///
///
///
public long ListLeftPush(string redisKey, string redisValue) { redisKey = AddKeyPrefix(redisKey); return _db.ListLeftPush(redisKey, redisValue); } ///
/// 返回列表上该键的长度,如果不存在,返回 0 ///
///
///
public long ListLength(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.ListLength(redisKey); } ///
/// 返回在该列表上键所对应的元素 ///
///
///
public IEnumerable
ListRange(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.ListRange(redisKey); } ///
/// 移除并返回存储在该键列表的第一个元素 ///
///
///
public T ListLeftPop
(string redisKey) { redisKey = AddKeyPrefix(redisKey); return Deserialize
(_db.ListLeftPop(redisKey)); } ///
/// 移除并返回存储在该键列表的最后一个元素 ///
///
///
public T ListRightPop
(string redisKey) { redisKey = AddKeyPrefix(redisKey); return Deserialize
(_db.ListRightPop(redisKey)); } ///
/// 在列表尾部插入值。如果键不存在,先创建再插入值 ///
///
///
///
public long ListRightPush
(string redisKey, T redisValue) { redisKey = AddKeyPrefix(redisKey); return _db.ListRightPush(redisKey, Serialize(redisValue)); } ///
/// 在列表头部插入值。如果键不存在,先创建再插入值 ///
///
///
///
public long ListLeftPush
(string redisKey, T redisValue) { redisKey = AddKeyPrefix(redisKey); return _db.ListLeftPush(redisKey, Serialize(redisValue)); } #region List-async ///
/// 移除并返回存储在该键列表的第一个元素 ///
///
///
public async Task
ListLeftPopAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.ListLeftPopAsync(redisKey); } ///
/// 移除并返回存储在该键列表的最后一个元素 ///
///
///
public async Task
ListRightPopAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.ListRightPopAsync(redisKey); } ///
/// 移除列表指定键上与该值相同的元素 ///
///
///
///
public async Task
ListRemoveAsync(string redisKey, string redisValue) { redisKey = AddKeyPrefix(redisKey); return await _db.ListRemoveAsync(redisKey, redisValue); } ///
/// 在列表尾部插入值。如果键不存在,先创建再插入值 ///
///
///
///
public async Task
ListRightPushAsync(string redisKey, string redisValue) { redisKey = AddKeyPrefix(redisKey); return await _db.ListRightPushAsync(redisKey, redisValue); } ///
/// 在列表头部插入值。如果键不存在,先创建再插入值 ///
///
///
///
public async Task
ListLeftPushAsync(string redisKey, string redisValue) { redisKey = AddKeyPrefix(redisKey); return await _db.ListLeftPushAsync(redisKey, redisValue); } ///
/// 返回列表上该键的长度,如果不存在,返回 0 ///
///
///
public async Task
ListLengthAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.ListLengthAsync(redisKey); } ///
/// 返回在该列表上键所对应的元素 ///
///
///
public async Task
> ListRangeAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.ListRangeAsync(redisKey); } ///
/// 移除并返回存储在该键列表的第一个元素 ///
///
///
public async Task
ListLeftPopAsync
(string redisKey) { redisKey = AddKeyPrefix(redisKey); return Deserialize
(await _db.ListLeftPopAsync(redisKey)); } ///
/// 移除并返回存储在该键列表的最后一个元素 ///
///
///
public async Task
ListRightPopAsync
(string redisKey) { redisKey = AddKeyPrefix(redisKey); return Deserialize
(await _db.ListRightPopAsync(redisKey)); } ///
/// 在列表尾部插入值。如果键不存在,先创建再插入值 ///
///
///
///
public async Task
ListRightPushAsync
(string redisKey, T redisValue) { redisKey = AddKeyPrefix(redisKey); return await _db.ListRightPushAsync(redisKey, Serialize(redisValue)); } ///
/// 在列表头部插入值。如果键不存在,先创建再插入值 ///
///
///
///
public async Task
ListLeftPushAsync
(string redisKey, T redisValue) { redisKey = AddKeyPrefix(redisKey); return await _db.ListLeftPushAsync(redisKey, Serialize(redisValue)); } #endregion List-async #endregion List 操作 #region SortedSet 操作 ///
/// SortedSet 新增 ///
///
///
///
///
public bool SortedSetAdd(string redisKey, string member, double score) { redisKey = AddKeyPrefix(redisKey); return _db.SortedSetAdd(redisKey, member, score); } ///
/// 在有序集合中返回指定范围的元素,默认情况下从低到高。 ///
///
///
public IEnumerable
SortedSetRangeByRank(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.SortedSetRangeByRank(redisKey); } ///
/// 返回有序集合的元素个数 ///
///
///
public long SortedSetLength(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.SortedSetLength(redisKey); } ///
/// 返回有序集合的元素个数 ///
///
///
///
public bool SortedSetLength(string redisKey, string memebr) { redisKey = AddKeyPrefix(redisKey); return _db.SortedSetRemove(redisKey, memebr); } ///
/// SortedSet 新增 ///
/// < using System; using System.Collections.Generic; using System.Configuration; using System.IO; using System.Linq; using System.Runtime.Serialization.Formatters.Binary; using System.Threading.Tasks; using StackExchange.Redis; namespace Common { ///
/// Redis 助手 ///
public class RedisHelper { ///
/// 连接字符串 ///
private static readonly string ConnectionString; ///
/// redis 连接对象 ///
private static readonly IConnectionMultiplexer ConnMultiplexer; ///
/// 默认的 Key 值(用来当作 RedisKey 的前缀) ///
private static readonly string DefaultKey; ///
/// 锁 ///
private static readonly object Locker = new object(); ///
/// 数据库 ///
private readonly IDatabase _db; ///
/// 获取 Redis 连接对象 ///
///
public IConnectionMultiplexer GetConnectionRedisMultiplexer() { if ((ConnMultiplexer == null) || !ConnMultiplexer.IsConnected) { lock (Locker) { if ((ConnMultiplexer == null) || !ConnMultiplexer.IsConnected) ConnectionMultiplexer.Connect(ConnectionString); } } return ConnMultiplexer; } #region 其它 public ITransaction GetTransaction() { return _db.CreateTransaction(); } #endregion 其它 #region 构造函数 static RedisHelper() { ConnectionString = ConfigHelper.GetSectionValue("Redis:Conn"); ConnMultiplexer = ConnectionMultiplexer.Connect(ConnectionString); DefaultKey = ConfigHelper.GetSectionValue("Redis:DefaultKey"); AddRegisterEvent(); } public RedisHelper(int db = -1) { _db = ConnMultiplexer.GetDatabase(db); } #endregion 构造函数 #region String 操作 ///
/// 设置 key 并保存字符串(如果 key 已存在,则覆盖值) ///
///
///
///
///
public bool StringSet(string redisKey, string redisValue, TimeSpan? expiry = null) { redisKey = AddKeyPrefix(redisKey); return _db.StringSet(redisKey, redisValue, expiry); } ///
/// 保存多个 Key-value ///
///
///
public bool StringSet(IEnumerable
> keyValuePairs) { keyValuePairs = keyValuePairs.Select(x => new KeyValuePair
(AddKeyPrefix(x.Key), x.Value)); return _db.StringSet(keyValuePairs.ToArray()); } ///
/// 获取字符串 ///
///
///
///
public string StringGet(string redisKey, TimeSpan? expiry = null) { redisKey = AddKeyPrefix(redisKey); return _db.StringGet(redisKey); } ///
/// 存储一个对象(该对象会被序列化保存) ///
///
///
///
///
public bool StringSet
(string redisKey, T redisValue, TimeSpan? expiry = null) { redisKey = AddKeyPrefix(redisKey); var json = Serialize(redisValue); return _db.StringSet(redisKey, json, expiry); } ///
/// 获取一个对象(会进行反序列化) ///
///
///
///
public T StringGet
(string redisKey, TimeSpan? expiry = null) { redisKey = AddKeyPrefix(redisKey); return Deserialize
(_db.StringGet(redisKey)); } #region async ///
/// 保存一个字符串值 ///
///
///
///
///
public async Task
StringSetAsync(string redisKey, string redisValue, TimeSpan? expiry = null) { redisKey = AddKeyPrefix(redisKey); return await _db.StringSetAsync(redisKey, redisValue, expiry); } ///
/// 保存一组字符串值 ///
///
///
public async Task
StringSetAsync(IEnumerable
> keyValuePairs) { keyValuePairs = keyValuePairs.Select(x => new KeyValuePair
(AddKeyPrefix(x.Key), x.Value)); return await _db.StringSetAsync(keyValuePairs.ToArray()); } ///
/// 获取单个值 ///
///
///
///
///
public async Task
StringGetAsync(string redisKey, string redisValue, TimeSpan? expiry = null) { redisKey = AddKeyPrefix(redisKey); return await _db.StringGetAsync(redisKey); } ///
/// 存储一个对象(该对象会被序列化保存) ///
///
///
///
///
public async Task
StringSetAsync
(string redisKey, T redisValue, TimeSpan? expiry = null) { redisKey = AddKeyPrefix(redisKey); var json = Serialize(redisValue); return await _db.StringSetAsync(redisKey, json, expiry); } ///
/// 获取一个对象(会进行反序列化) ///
///
///
///
public async Task
StringGetAsync
(string redisKey, TimeSpan? expiry = null) { redisKey = AddKeyPrefix(redisKey); return Deserialize
(await _db.StringGetAsync(redisKey)); } #endregion async #endregion String 操作 #region Hash 操作 ///
/// 判断该字段是否存在 hash 中 ///
///
///
///
public bool HashExists(string redisKey, string hashField) { redisKey = AddKeyPrefix(redisKey); return _db.HashExists(redisKey, hashField); } ///
/// 从 hash 中移除指定字段 ///
///
///
///
public bool HashDelete(string redisKey, string hashField) { redisKey = AddKeyPrefix(redisKey); return _db.HashDelete(redisKey, hashField); } ///
/// 从 hash 中移除指定字段 ///
///
///
///
public long HashDelete(string redisKey, IEnumerable
hashField) { redisKey = AddKeyPrefix(redisKey); return _db.HashDelete(redisKey, hashField.ToArray()); } ///
/// 在 hash 设定值 ///
///
///
///
///
public bool HashSet(string redisKey, string hashField, string value) { redisKey = AddKeyPrefix(redisKey); return _db.HashSet(redisKey, hashField, value); } ///
/// 在 hash 中设定值 ///
///
///
public void HashSet(string redisKey, IEnumerable
hashFields) { redisKey = AddKeyPrefix(redisKey); _db.HashSet(redisKey, hashFields.ToArray()); } ///
/// 在 hash 中获取值 ///
///
///
///
public RedisValue HashGet(string redisKey, string hashField) { redisKey = AddKeyPrefix(redisKey); return _db.HashGet(redisKey, hashField); } ///
/// 在 hash 中获取值 ///
///
///
///
///
public RedisValue[] HashGet(string redisKey, RedisValue[] hashField, string value) { redisKey = AddKeyPrefix(redisKey); return _db.HashGet(redisKey, hashField); } ///
/// 从 hash 返回所有的字段值 ///
///
///
public IEnumerable
HashKeys(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.HashKeys(redisKey); } ///
/// 返回 hash 中的所有值 ///
///
///
public RedisValue[] HashValues(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.HashValues(redisKey); } ///
/// 在 hash 设定值(序列化) ///
///
///
///
///
public bool HashSet
(string redisKey, string hashField, T value) { redisKey = AddKeyPrefix(redisKey); var json = Serialize(value); return _db.HashSet(redisKey, hashField, json); } ///
/// 在 hash 中获取值(反序列化) ///
///
///
///
public T HashGet
(string redisKey, string hashField) { redisKey = AddKeyPrefix(redisKey); return Deserialize
(_db.HashGet(redisKey, hashField)); } #region async ///
/// 判断该字段是否存在 hash 中 ///
///
///
///
public async Task
HashExistsAsync(string redisKey, string hashField) { redisKey = AddKeyPrefix(redisKey); return await _db.HashExistsAsync(redisKey, hashField); } ///
/// 从 hash 中移除指定字段 ///
///
///
///
public async Task
HashDeleteAsync(string redisKey, string hashField) { redisKey = AddKeyPrefix(redisKey); return await _db.HashDeleteAsync(redisKey, hashField); } ///
/// 从 hash 中移除指定字段 ///
///
///
///
public async Task
HashDeleteAsync(string redisKey, IEnumerable
hashField) { redisKey = AddKeyPrefix(redisKey); return await _db.HashDeleteAsync(redisKey, hashField.ToArray()); } ///
/// 在 hash 设定值 ///
///
///
///
///
public async Task
HashSetAsync(string redisKey, string hashField, string value) { redisKey = AddKeyPrefix(redisKey); return await _db.HashSetAsync(redisKey, hashField, value); } ///
/// 在 hash 中设定值 ///
///
///
public async Task HashSetAsync(string redisKey, IEnumerable
hashFields) { redisKey = AddKeyPrefix(redisKey); await _db.HashSetAsync(redisKey, hashFields.ToArray()); } ///
/// 在 hash 中获取值 ///
///
///
///
public async Task
HashGetAsync(string redisKey, string hashField) { redisKey = AddKeyPrefix(redisKey); return await _db.HashGetAsync(redisKey, hashField); } ///
/// 在 hash 中获取值 ///
///
///
///
///
public async Task
> HashGetAsync(string redisKey, RedisValue[] hashField, string value) { redisKey = AddKeyPrefix(redisKey); return await _db.HashGetAsync(redisKey, hashField); } ///
/// 从 hash 返回所有的字段值 ///
///
///
public async Task
> HashKeysAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.HashKeysAsync(redisKey); } ///
/// 返回 hash 中的所有值 ///
///
///
public async Task
> HashValuesAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.HashValuesAsync(redisKey); } ///
/// 在 hash 设定值(序列化) ///
///
///
///
///
public async Task
HashSetAsync
(string redisKey, string hashField, T value) { redisKey = AddKeyPrefix(redisKey); var json = Serialize(value); return await _db.HashSetAsync(redisKey, hashField, json); } ///
/// 在 hash 中获取值(反序列化) ///
///
///
///
public async Task
HashGetAsync
(string redisKey, string hashField) { redisKey = AddKeyPrefix(redisKey); return Deserialize
(await _db.HashGetAsync(redisKey, hashField)); } #endregion async #endregion Hash 操作 #region List 操作 ///
/// 移除并返回存储在该键列表的第一个元素 ///
///
///
public string ListLeftPop(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.ListLeftPop(redisKey); } ///
/// 移除并返回存储在该键列表的最后一个元素 ///
///
///
public string ListRightPop(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.ListRightPop(redisKey); } ///
/// 移除列表指定键上与该值相同的元素 ///
///
///
///
public long ListRemove(string redisKey, string redisValue) { redisKey = AddKeyPrefix(redisKey); return _db.ListRemove(redisKey, redisValue); } ///
/// 在列表尾部插入值。如果键不存在,先创建再插入值 ///
///
///
///
public long ListRightPush(string redisKey, string redisValue) { redisKey = AddKeyPrefix(redisKey); return _db.ListRightPush(redisKey, redisValue); } ///
/// 在列表头部插入值。如果键不存在,先创建再插入值 ///
///
///
///
public long ListLeftPush(string redisKey, string redisValue) { redisKey = AddKeyPrefix(redisKey); return _db.ListLeftPush(redisKey, redisValue); } ///
/// 返回列表上该键的长度,如果不存在,返回 0 ///
///
///
public long ListLength(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.ListLength(redisKey); } ///
/// 返回在该列表上键所对应的元素 ///
///
///
public IEnumerable
ListRange(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.ListRange(redisKey); } ///
/// 移除并返回存储在该键列表的第一个元素 ///
///
///
public T ListLeftPop
(string redisKey) { redisKey = AddKeyPrefix(redisKey); return Deserialize
(_db.ListLeftPop(redisKey)); } ///
/// 移除并返回存储在该键列表的最后一个元素 ///
///
///
public T ListRightPop
(string redisKey) { redisKey = AddKeyPrefix(redisKey); return Deserialize
(_db.ListRightPop(redisKey)); } ///
/// 在列表尾部插入值。如果键不存在,先创建再插入值 ///
///
///
///
public long ListRightPush
(string redisKey, T redisValue) { redisKey = AddKeyPrefix(redisKey); return _db.ListRightPush(redisKey, Serialize(redisValue)); } ///
/// 在列表头部插入值。如果键不存在,先创建再插入值 ///
///
///
///
public long ListLeftPush
(string redisKey, T redisValue) { redisKey = AddKeyPrefix(redisKey); return _db.ListLeftPush(redisKey, Serialize(redisValue)); } #region List-async ///
/// 移除并返回存储在该键列表的第一个元素 ///
///
///
public async Task
ListLeftPopAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.ListLeftPopAsync(redisKey); } ///
/// 移除并返回存储在该键列表的最后一个元素 ///
///
///
public async Task
ListRightPopAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.ListRightPopAsync(redisKey); } ///
/// 移除列表指定键上与该值相同的元素 ///
///
///
///
public async Task
ListRemoveAsync(string redisKey, string redisValue) { redisKey = AddKeyPrefix(redisKey); return await _db.ListRemoveAsync(redisKey, redisValue); } ///
/// 在列表尾部插入值。如果键不存在,先创建再插入值 ///
///
///
///
public async Task
ListRightPushAsync(string redisKey, string redisValue) { redisKey = AddKeyPrefix(redisKey); return await _db.ListRightPushAsync(redisKey, redisValue); } ///
/// 在列表头部插入值。如果键不存在,先创建再插入值 ///
///
///
///
public async Task
ListLeftPushAsync(string redisKey, string redisValue) { redisKey = AddKeyPrefix(redisKey); return await _db.ListLeftPushAsync(redisKey, redisValue); } ///
/// 返回列表上该键的长度,如果不存在,返回 0 ///
///
///
public async Task
ListLengthAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.ListLengthAsync(redisKey); } ///
/// 返回在该列表上键所对应的元素 ///
///
///
public async Task
> ListRangeAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.ListRangeAsync(redisKey); } ///
/// 移除并返回存储在该键列表的第一个元素 ///
///
///
public async Task
ListLeftPopAsync
(string redisKey) { redisKey = AddKeyPrefix(redisKey); return Deserialize
(await _db.ListLeftPopAsync(redisKey)); } ///
/// 移除并返回存储在该键列表的最后一个元素 ///
///
///
public async Task
ListRightPopAsync
(string redisKey) { redisKey = AddKeyPrefix(redisKey); return Deserialize
(await _db.ListRightPopAsync(redisKey)); } ///
/// 在列表尾部插入值。如果键不存在,先创建再插入值 ///
///
///
///
public async Task
ListRightPushAsync
(string redisKey, T redisValue) { redisKey = AddKeyPrefix(redisKey); return await _db.ListRightPushAsync(redisKey, Serialize(redisValue)); } ///
/// 在列表头部插入值。如果键不存在,先创建再插入值 ///
///
///
///
public async Task
ListLeftPushAsync
(string redisKey, T redisValue) { redisKey = AddKeyPrefix(redisKey); return await _db.ListLeftPushAsync(redisKey, Serialize(redisValue)); } #endregion List-async #endregion List 操作 #region SortedSet 操作 ///
/// SortedSet 新增 ///
///
///
///
///
public bool SortedSetAdd(string redisKey, string member, double score) { redisKey = AddKeyPrefix(redisKey); return _db.SortedSetAdd(redisKey, member, score); } ///
/// 在有序集合中返回指定范围的元素,默认情况下从低到高。 ///
///
///
public IEnumerable
SortedSetRangeByRank(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.SortedSetRangeByRank(redisKey); } param name="redisKey"> ///
///
///
public bool SortedSetAdd
(string redisKey, T member, double score) { redisKey = AddKeyPrefix(redisKey); var json = Serialize(member); return _db.SortedSetAdd(redisKey, json, score); } #region SortedSet-Async ///
/// SortedSet 新增 ///
///
///
///
///
public async Task
SortedSetAddAsync(string redisKey, string member, double score) { redisKey = AddKeyPrefix(redisKey); return await _db.SortedSetAddAsync(redisKey, member, score); } ///
/// 在有序集合中返回指定范围的元素,默认情况下从低到高。 ///
///
///
public async Task
> SortedSetRangeByRankAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.SortedSetRangeByRankAsync(redisKey); } ///
/// 返回有序集合的元素个数 ///
///
///
public async Task
SortedSetLengthAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.SortedSetLengthAsync(redisKey); } ///
/// 返回有序集合的元素个数 ///
///
///
///
public async Task
SortedSetRemoveAsync(string redisKey, string memebr) { redisKey = AddKeyPrefix(redisKey); return await _db.SortedSetRemoveAsync(redisKey, memebr); } ///
/// SortedSet 新增 ///
///
///
///
///
public async Task
SortedSetAddAsync
(string redisKey, T member, double score) { redisKey = AddKeyPrefix(redisKey); var json = Serialize(member); return await _db.SortedSetAddAsync(redisKey, json, score); } #endregion SortedSet-Async #endregion SortedSet 操作 #region key 操作 ///
/// 移除指定 Key ///
///
///
public bool KeyDelete(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.KeyDelete(redisKey); } ///
/// 移除指定 Key ///
///
///
public long KeyDelete(IEnumerable
redisKeys) { var keys = redisKeys.Select(x => (RedisKey)AddKeyPrefix(x)); return _db.KeyDelete(keys.ToArray()); } ///
/// 校验 Key 是否存在 ///
///
///
public bool KeyExists(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.KeyExists(redisKey); } ///
/// 重命名 Key ///
///
///
///
public bool KeyRename(string redisKey, string redisNewKey) { redisKey = AddKeyPrefix(redisKey); return _db.KeyRename(redisKey, redisNewKey); } ///
/// 设置 Key 的时间 ///
///
///
///
public bool KeyExpire(string redisKey, TimeSpan? expiry) { redisKey = AddKeyPrefix(redisKey); return _db.KeyExpire(redisKey, expiry); } #region key-async ///
/// 移除指定 Key ///
///
///
public async Task
KeyDeleteAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.KeyDeleteAsync(redisKey); } ///
/// 移除指定 Key ///
///
///
public async Task
KeyDeleteAsync(IEnumerable
redisKeys) { var keys = redisKeys.Select(x => (RedisKey)AddKeyPrefix(x)); return await _db.KeyDeleteAsync(keys.ToArray()); } ///
/// 校验 Key 是否存在 ///
///
///
public async Task
KeyExistsAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.KeyExistsAsync(redisKey); } ///
/// 重命名 Key ///
///
///
///
public async Task
KeyRenameAsync(string redisKey, string redisNewKey) { redisKey = AddKeyPrefix(redisKey); return await _db.KeyRenameAsync(redisKey, redisNewKey); } ///
/// 设置 Key 的时间 ///
///
///
///
public async Task
KeyExpireAsync(string redisKey, TimeSpan? expiry) { redisKey = AddKeyPrefix(redisKey); return await _db.KeyExpireAsync(redisKey, expiry); } #endregion key-async #endregion key 操作 #region 发布订阅 ///
/// 订阅 ///
///
///
public void Subscribe(RedisChannel channel, Action
handle) { var sub = ConnMultiplexer.GetSubscriber(); sub.Subscribe(channel, handle); } ///
/// 发布 ///
///
///
///
public long Publish(RedisChannel channel, RedisValue message) { var sub = ConnMultiplexer.GetSubscriber(); return sub.Publish(channel, message); } ///
/// 发布(使用序列化) ///
///
///
///
///
public long Publish
(RedisChannel channel, T message) { var sub = ConnMultiplexer.GetSubscriber(); return sub.Publish(channel, Serialize(message)); } #region 发布订阅-async ///
/// 订阅 ///
///
///
public async Task SubscribeAsync(RedisChannel channel, Action
handle) { var sub = ConnMultiplexer.GetSubscriber(); await sub.SubscribeAsync(channel, handle); } ///
/// 发布 ///
///
///
///
public async Task
PublishAsync(RedisChannel channel, RedisValue message) { var sub = ConnMultiplexer.GetSubscriber(); return await sub.PublishAsync(channel, message); } ///
/// 发布(使用序列化) ///
///
///
///
///
public async Task
PublishAsync
(RedisChannel channel, T message) { var sub = ConnMultiplexer.GetSubscriber(); return await sub.PublishAsync(channel, Serialize(message)); } #endregion 发布订阅-async #endregion 发布订阅 #region private method ///
/// 添加 Key 的前缀 ///
///
///
private static string AddKeyPrefix(string key) { return $"{DefaultKey}:{key}"; } #region 注册事件 ///
/// 添加注册事件 ///
private static void AddRegisterEvent() { ConnMultiplexer.ConnectionRestored += ConnMultiplexer_ConnectionRestored; ConnMultiplexer.ConnectionFailed += ConnMultiplexer_ConnectionFailed; ConnMultiplexer.ErrorMessage += ConnMultiplexer_ErrorMessage; ConnMultiplexer.ConfigurationChanged += ConnMultiplexer_ConfigurationChanged; ConnMultiplexer.HashSlotMoved += ConnMultiplexer_HashSlotMoved; ConnMultiplexer.InternalError += ConnMultiplexer_InternalError; ConnMultiplexer.ConfigurationChangedBroadcast += ConnMultiplexer_ConfigurationChangedBroadcast; } ///
/// 重新配置广播时(通常意味着主从同步更改) ///
///
///
private static void ConnMultiplexer_ConfigurationChangedBroadcast(object sender, EndPointEventArgs e) { Console.WriteLine($"{nameof(ConnMultiplexer_ConfigurationChangedBroadcast)}: {e.EndPoint}"); } ///
/// 发生内部错误时(主要用于调试) ///
///
///
private static void ConnMultiplexer_InternalError(object sender, InternalErrorEventArgs e) { Console.WriteLine($"{nameof(ConnMultiplexer_InternalError)}: {e.Exception}"); } ///
/// 更改集群时 ///
///
///
private static void ConnMultiplexer_HashSlotMoved(object sender, HashSlotMovedEventArgs e) { Console.WriteLine( $"{nameof(ConnMultiplexer_HashSlotMoved)}: {nameof(e.OldEndPoint)}-{e.OldEndPoint} To {nameof(e.NewEndPoint)}-{e.NewEndPoint}, "); } ///
/// 配置更改时 ///
///
///
private static void ConnMultiplexer_ConfigurationChanged(object sender, EndPointEventArgs e) { Console.WriteLine($"{nameof(ConnMultiplexer_ConfigurationChanged)}: {e.EndPoint}"); } ///
/// 发生错误时 ///
///
///
private static void ConnMultiplexer_ErrorMessage(object sender, RedisErrorEventArgs e) { Console.WriteLine($"{nameof(ConnMultiplexer_ErrorMessage)}: {e.Message}"); } ///
/// 物理连接失败时 ///
///
///
private static void ConnMultiplexer_ConnectionFailed(object sender, ConnectionFailedEventArgs e) { Console.WriteLine($"{nameof(ConnMultiplexer_ConnectionFailed)}: {e.Exception}"); } ///
/// 建立物理连接时 ///
///
///
private static void ConnMultiplexer_ConnectionRestored(object sender, ConnectionFailedEventArgs e) { Console.WriteLine($"{nameof(ConnMultiplexer_ConnectionRestored)}: {e.Exception}"); } #endregion 注册事件 ///
/// 序列化 ///
///
///
private static byte[] Serialize(object obj) { if (obj == null) return null; var binaryFormatter = new BinaryFormatter(); using (var memoryStream = new MemoryStream()) { binaryFormatter.Serialize(memoryStream, obj); var data = memoryStream.ToArray(); return data; } } ///
/// 反序列化 ///
///
///
///
private static T Deserialize
(byte[] data) { if (data == null) return default(T); var binaryFormatter = new BinaryFormatter(); using (var memoryStream = new MemoryStream(data)) { var result = (T)binaryFormatter.Deserialize(memoryStream); return result; } } #endregion private method } } ``` #### 2.设置及获取 ``` string uuid = Guid.NewGuid().ToString(); var redis = new RedisHelper(1); var exp = new TimeSpan(60 * 10000000);//设置过期时间为60s redis.StringSet(uuid, code, exp); Thread.Sleep(2000); string get = redis.StringGet("test"); //string userMenu =string.Format("menu_{0}", userid); //var redis = new RedisHelper(1); //var exp = new TimeSpan(2,0,0);//设置过期时间为2h //redis.StringSet(userMenu, showMenuList, exp); ``` #### 3.可视化工具 Redis Desktop Manager 官网下载:https://redisdesktop.com/download github地址:https://github.com/uglide/RedisDesktopManager/releases 一、下载 Redis Desktop Manager 百度网盘:http://pan.baidu.com/s/1kU8sY3P 文件名称:redis-desktop-manager-0.8.8.384.exe 还有一个免费的: https://github.com/qishibo/AnotherRedisDesktopManager
相关推荐
post数据过多导致 net::ERR_CONNECTION_RESET
.NET Core下使用Autofac实现构造函数注入
.net core微软自带的构造函数注入
.net core 使用EF
评论区
先去登录
版权所有:机遇屋在线 Copyright © 2021-2025 jiyuwu Co., Ltd.
鲁ICP备16042261号-1