using System; using System.Collections.Generic; using ServiceStack.Text; namespace BP.Difference.Redis { public class RedisUtils: RedisOperatorBase { public RedisUtils() : base() { } public bool Set(string key, T t) { return Redis.Set(key, t); } public bool Set(string key, String value) { return Redis.Set(key, value); } public T Get(string key) { return Redis.Get(key); } public string Get(string key) { Object obj = Redis.Get(key); if (obj == null) return null; return obj.ToString(); } public bool del(string key) { return Redis.Remove(key); } /// /// 判断某个数据是否已经被缓存 /// public bool ExistInHash(string hashId, string key) { return Redis.HashContainsEntry(hashId, key); } /// /// 存储数据到hash表 /// public bool SetInHash(string hashId, string key, T t) { var value = JsonSerializer.SerializeToString(t); return Redis.SetEntryInHash(hashId, key, value); } public bool SetInHash(string hashId, string key, string value) { return Redis.SetEntryInHash(hashId, key, value); } /// /// 移除hash中的某值 /// public bool RemoveHash(string hashId, string key) { return Redis.RemoveEntryFromHash(hashId, key); } /// /// 移除整个hash /// public bool RemoveHash(string key) { return Redis.Remove(key); } /// /// 从hash表获取数据 /// public T GetFromHash(string hashId, string key) { string value = Redis.GetValueFromHash(hashId, key); return JsonSerializer.DeserializeFromString(value); } public String GetFromHash(string hashId, string key) { return Redis.GetValueFromHash(hashId, key); } /// /// 获取整个hash的数据 /// public List GetAllHashValues(string hashId) { var result = new List(); var list = Redis.GetHashValues(hashId); if (list != null && list.Count > 0) { list.ForEach(x => { var value = JsonSerializer.DeserializeFromString(x); result.Add(value); }); } return result; } /// /// 设置缓存过期 /// public void SetExpire(string key, DateTime datetime) { Redis.ExpireEntryAt(key, datetime); } } }