diff --git a/Infrastructure/Cache/RedisCacheContext.cs b/Infrastructure/Cache/RedisCacheContext.cs index ceedd6d..0bf4b60 100644 --- a/Infrastructure/Cache/RedisCacheContext.cs +++ b/Infrastructure/Cache/RedisCacheContext.cs @@ -1,7 +1,4 @@ -using System; -using Enyim.Caching; -using Enyim.Caching.Memcached; -using Microsoft.Extensions.Options; +using Microsoft.Extensions.Options; using StackExchange.Redis; namespace Infrastructure.Cache @@ -59,5 +56,116 @@ namespace Infrastructure.Cache { return func(id); } + + /// + /// HSET - 将哈希表 key 中的字段 field 的值设为 value。 + /// + /// 键 + /// 字段 + /// 值 + public void HashSet(string key, string field, T value) + { + iDatabase.HashSet(new RedisKey(key), + new[] { new HashEntry(new RedisValue(field), new RedisValue(value.ToString() ?? string.Empty)) }); + } + + /// + /// HGET - 获取哈希表中的指定字段的值。 + /// + /// 键 + /// 字段 + /// 字段对应的值 + public object HashGet(string key, string field) + { + return iDatabase.HashGet(key, field); + } + + /// + /// HEXISTS - 查看哈希表中是否存在指定字段。 + /// + /// 键 + /// 字段 + /// 是否存在 + public bool HashCheck(string key, string field) + { + return iDatabase.HashExists(key, field); + } + + /// + /// HDEL - 删除一个或多个哈希表字段。 + /// + /// 键 + /// 要删除的字段数组 + /// 移除数量 + public long HashDel(string key, string[] fields) + { + var redisFields = fields.Select(f => new RedisValue(f)).ToArray(); + return iDatabase.HashDelete(new RedisKey(key), redisFields); + } + + /// + /// HLEN - 获取哈希表中字段的数量。 + /// + /// 键 + /// 字段数量 + public long HashLen(string key) + { + return iDatabase.HashLength(key); + } + + + /// + /// EXPIRE - 为键设置过期时间(秒)。 + /// + /// 键 + /// 过期时间(秒) + /// 是否设置成功 + public bool ExpireKey(string key, long timeout) + { + return iDatabase.KeyExpire(key, TimeSpan.FromSeconds(timeout)); + } + + + /// + /// GET - 获取键对应的值。 + /// + /// 键 + /// 键对应的值 + public object Get(string key) + { + return iDatabase.StringGet(key); + } + + + /// + /// EXISTS - 判断键是否存在。 + /// + /// 键 + /// 是否存在 + public bool CheckExist(string key) + { + return iDatabase.KeyExists(key); + } + + /// + /// DEL - 删除键。 + /// + /// 键 + /// 是否删除成功 + public bool Del(string key) + { + return CheckExist(key) && iDatabase.KeyDelete(key); + } + + + /// + /// LLEN - 获取列表的长度。 + /// + /// 键 + /// 列表长度 + public long ListLen(string key) + { + return iDatabase.ListLength(key); + } } } \ No newline at end of file