using Microsoft.Extensions.Options; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Txgy.RBS.Framework.RedisHelper.Init; using Txgy.RBS.Framework.RedisHelper.Interface; namespace Txgy.RBS.Framework.RedisHelper.Service { /// /// Hash:类似dictionary,通过索引快速定位到指定元素的,耗时均等,跟string的区别在于不用反序列化,直接修改某个字段 /// string的话要么是 001:序列化整个实体 /// 要么是 001_name: 001_pwd: 多个key-value /// Hash的话,一个hashid-{key:value;key:value;key:value;} /// 可以一次性查找实体,也可以单个,还可以单个修改 /// public class RedisHashService : RedisBase { public RedisHashService(IOptionsMonitor options) : base(options) { } #region 添加 /// /// 向hashid集合中添加key/value /// public bool SetEntryInHash(string hashid, string key, string value) { return IClient.SetEntryInHash(hashid, key, value); } public void SetRangeInHash(string hashid, IEnumerable> value) { IClient.SetRangeInHash(hashid, value); } /// /// 如果hashid集合中存在key/value则不添加返回false, /// 如果不存在在添加key/value,返回true /// public bool SetEntryInHashIfNotExists(string hashid, string key, string value) { return IClient.SetEntryInHashIfNotExists(hashid, key, value); } /// /// 存储对象T t到hash集合中 /// 需要包含Id,然后用Id获取 /// public void StoreAsHash(T t) { IClient.StoreAsHash(t); } #endregion #region 获取 /// /// 获取对象T中ID为id的数据。 /// public T GetFromHash(object id) { return IClient.GetFromHash(id); } /// /// 获取所有hashid数据集的key/value数据集合 /// public Dictionary GetAllEntriesFromHash(string hashid) { return IClient.GetAllEntriesFromHash(hashid); } /// /// 获取hashid数据集中的数据总数 /// public long GetHashCount(string hashid) { return IClient.GetHashCount(hashid); } /// /// 获取hashid数据集中所有key的集合 /// public List GetHashKeys(string hashid) { return IClient.GetHashKeys(hashid); } /// /// 获取hashid数据集中的所有value集合 /// public List GetHashValues(string hashid) { return IClient.GetHashValues(hashid); } /// /// 获取hashid数据集中,key的value数据 /// public string GetValueFromHash(string hashid, string key) { return IClient.GetValueFromHash(hashid, key); } /// /// 获取hashid数据集中,多个keys的value集合 /// public List GetValuesFromHash(string hashid, string[] keys) { return IClient.GetValuesFromHash(hashid, keys); } #endregion #region 删除 /// /// 删除hashid数据集中的key数据 /// public bool RemoveEntryFromHash(string hashid, string key) { return IClient.RemoveEntryFromHash(hashid, key); } public bool RemoveEntry(string hashid) { return IClient.Remove(hashid); } #endregion #region 其它 /// /// 判断hashid数据集中是否存在key的数据 /// public bool HashContainsEntry(string hashid, string key) { return IClient.HashContainsEntry(hashid, key); } /// /// 给hashid数据集key的value加countby,返回相加后的数据 /// public double IncrementValueInHash(string hashid, string key, double countBy) { return IClient.IncrementValueInHash(hashid, key, countBy); } #endregion } }