C# 通过ServiceStack 操作Redis——Hash类型的使用及示例
接着上一篇,下面转到hash类型的代码使用
Hash:结构 key-key-value,通过索引快速定位到指定元素的,可直接修改某个字段
/// <summary> /// Hash:类似dictionary,通过索引快速定位到指定元素的,耗时均等,跟string的区别在于不用反序列化,直接修改某个字段 /// string的话要么是 001:序列化整个实体 /// 要么是 001_name: 001_pwd: 多个key-value /// Hash的话,一个hashid-{key:value;key:value;key:value;} /// 可以一次性查找实体,也可以单个,还可以单个修改 /// </summary> public class RedisHashService : RedisBase { #region 添加 /// <summary> /// 向hashid集合中添加key/value /// </summary> public bool SetEntryInHash(string hashid, string key, string value) { return base.iClient.SetEntryInHash(hashid, key, value); } /// <summary> /// 如果hashid集合中存在key/value则不添加返回false, /// 如果不存在在添加key/value,返回true /// </summary> public bool SetEntryInHashIfNotExists(string hashid, string key, string value) { return base.iClient.SetEntryInHashIfNotExists(hashid, key, value); } /// <summary> /// 存储对象T t到hash集合中 /// 需要包含Id,然后用Id获取 /// </summary> public void StoreAsHash<T>(T t) { base.iClient.StoreAsHash<T>(t); } #endregion #region 获取 /// <summary> /// 获取对象T中ID为id的数据。 /// </summary> public T GetFromHash<T>(object id) { return base.iClient.GetFromHash<T>(id); } /// <summary> /// 获取所有hashid数据集的key/value数据集合 /// </summary> public Dictionary<string, string> GetAllEntriesFromHash(string hashid) { return base.iClient.GetAllEntriesFromHash(hashid); } /// <summary> /// 获取hashid数据集中的数据总数 /// </summary> public long GetHashCount(string hashid) { return base.iClient.GetHashCount(hashid); } /// <summary> /// 获取hashid数据集中所有key的集合 /// </summary> public List<string> GetHashKeys(string hashid) { return base.iClient.GetHashKeys(hashid); } /// <summary> /// 获取hashid数据集中的所有value集合 /// </summary> public List<string> GetHashValues(string hashid) { return base.iClient.GetHashValues(hashid); } /// <summary> /// 获取hashid数据集中,key的value数据 /// </summary> public string GetValueFromHash(string hashid, string key) { return base.iClient.GetValueFromHash(hashid, key); } /// <summary> /// 获取hashid数据集中,多个keys的value集合 /// </summary> public List<string> GetValuesFromHash(string hashid, string[] keys) { return base.iClient.GetValuesFromHash(hashid, keys); } #endregion #region 删除 /// <summary> /// 删除hashid数据集中的key数据 /// </summary> public bool RemoveEntryFromHash(string hashid, string key) { return base.iClient.RemoveEntryFromHash(hashid, key); } #endregion #region 其它 /// <summary> /// 判断hashid数据集中是否存在key的数据 /// </summary> public bool HashContainsEntry(string hashid, string key) { return base.iClient.HashContainsEntry(hashid, key); } /// <summary> /// 给hashid数据集key的value加countby,返回相加后的数据 /// </summary> public double IncrementValueInHash(string hashid, string key, double countBy) { return base.iClient.IncrementValueInHash(hashid, key, countBy); } #endregion }
添加
//1. 添加 UserInfo userInfo = new UserInfo() { Id = 3, Age = 50, Name = "zxl", Pwd = "123456" }; //添加第一种方式 client.SetEntryInHash("UserInfor_001", "0000", "11111"); client.SetEntryInHash("UserInfor_001", nameof(userInfo.Id), userInfo.Id.ToString()); client.SetEntryInHash("UserInfor_001", nameof(userInfo.Age), userInfo.Age.ToString()); client.SetEntryInHash("UserInfor_001", nameof(userInfo.Name), userInfo.Name.ToString()); client.SetEntryInHash("UserInfor_001", nameof(userInfo.Pwd), userInfo.Pwd.ToString()); //不存在则添加 bool b1= client.SetEntryInHashIfNotExists("UserInfor_001", nameof(userInfo.Pwd), userInfo.Pwd.ToString()); bool b2= client.SetEntryInHashIfNotExists("UserInfor_001", "潇洒", "小康1000000"); UserInfo userInfo2 = new UserInfo() { Id = 4, Age = 60, Name = "萧十一郎", Pwd = "123456" }; UserInfo userInfo3 = new UserInfo() { Id = 5, Age = 66, Name = "萧十一郎66", Pwd = "123456" }; //添加第二种方式 UserInfo 必须有Id属性 client.StoreAsHash<UserInfo>(userInfo2); client.StoreAsHash<UserInfo>(userInfo3);
获取
//2.获取 string hashId = "UserInfor_001"; //获取所有hashid数据集的key / value数据集合 Dictionary<string, string> data1 = client.GetAllEntriesFromHash(hashId); //获取hashid的数据总数 long data2 = client.GetHashCount(hashId); //获取hashid数据集中所有key的集合 List<string>data3= client.GetHashKeys(hashId); //获取hashid数据集中的所有value集合 List<string> data4= client.GetHashValues(hashId); UserInfo data5= client.GetFromHash<UserInfo>(hashId); //单个value的读取 string age = client.GetValueFromHash(hashId, nameof(userInfo.Age)); string name = client.GetValueFromHash(hashId, nameof(userInfo.Name));
删除
//3. 删除 client.RemoveEntryFromHash(hashId, nameof(userInfo.Pwd));
修改
//4. 修改 client.SetEntryInHash(hashId, nameof(userInfo.Name), "zzzxxxlll");
付费内容,请联系本人QQ:1002453261
本文来自博客园,作者:明志德道,转载请注明原文链接:https://www.cnblogs.com/for-easy-fast/p/14528399.html