csredis封装_[C#] 使用 StackExchange.Redis 封装属于自己的 RedisHelper

本文介绍了如何使用 C# 编程语言,基于 StackExchange.Redis 封装一个简单的 RedisHelper 类,用于方便地操作 Redis 数据库。包括字符串、哈希表、列表和有序集合的操作,并提供了异步方法。类库中还包含了连接管理、序列化和反序列化功能。
摘要由CSDN通过智能技术生成

目前 .NET 使用访问 Redis 的的类库主流应该是 StackExchange.Redis,自己参考网上的文章(也许是吃饱了撑着),也尝试做出简单的封装。

///

/// Redis 助手

///

public class RedisHelper

{

///

/// 连接字符串

///

private static readonly string ConnectionString;

///

/// redis 连接对象

///

private static 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)

_connMultiplexer = ConnectionMultiplexer.Connect(ConnectionString);

}

}

return _connMultiplexer;

}

#region 其它

public ITransaction GetTransaction()

{

return _db.CreateTransaction();

}

#endregion 其它

#region 构造函数

static RedisHelper()

{

ConnectionString = ConfigurationManager.ConnectionStrings["RedisConnectionString"].ConnectionString;

_connMultiplexer = ConnectionMultiplexer.Connect(ConnectionString);

DefaultKey = ConfigurationManager.AppSettings["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);

}

///

/// 移除并返回存储在该键列表的最后一个元素

/// </

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值