2)hash类型,上代码

  1. using (RedisClient client = new RedisClient("127.0.0.1", 6379, "12345", 10))
  2. {
  3. //删除当前数据库中的所有Key 默认删除的是db0
  4. client.FlushDb();
  5. //删除所有数据库中的key
  6. //client.FlushAll();
  7. //大key
  8. string hashid = "pengbo";
  9. #region 向hashid集合中添加key/value
  10. client.SetEntryInHash(hashid, "id", "001");
  11. Console.WriteLine(client.GetValuesFromHash(hashid, "id").FirstOrDefault());
  12. client.SetEntryInHash(hashid, "name", "world");
  13. Console.WriteLine(client.GetValuesFromHash(hashid, "name").FirstOrDefault());
  14. client.SetEntryInHash(hashid, "socre", "100");
  15. Console.WriteLine(client.GetValuesFromHash(hashid, "socre").FirstOrDefault());
  16. #endregion
  17. #region 批量新增key的值
  18. client.FlushDb();
  19. Dictionary<string, string> pairs = new Dictionary<string, string>();
  20. pairs.Add("id", "001");
  21. pairs.Add("name", "world");
  22. client.SetRangeInHash(hashid, pairs);
  23. //获取当前key的值
  24. Console.WriteLine(client.GetValueFromHash(hashid, "id"));
  25. Console.WriteLine(client.GetValueFromHash(hashid, "name"));
  26. //一次性的获取所有想要获取的小key(属性的)值 如果key不存在,则返回空,不抛出异常
  27. var list = client.GetValuesFromHash(hashid, "id", "name", "abc");
  28. Console.WriteLine("*********");
  29. foreach (var item in list)
  30. {
  31. Console.WriteLine(item);
  32. }
  33. #endregion
  34. #region 如果hashid集合中存在key/value则不添加返回false,如果不存在在添加key/value,返回true
  35. client.FlushDb();
  36. Console.WriteLine(client.SetEntryInHashIfNotExists(hashid, "name", "你好美"));
  37. Console.WriteLine(client.SetEntryInHashIfNotExists(hashid, "name", "你好美 哈哈哈"));
  38. Console.WriteLine(client.GetValuesFromHash(hashid, "name").FirstOrDefault());
  39. #endregion
  40. #region 存储对象T t到hash集合中
  41. client.FlushDb();
  42. //urn: 类名: id的值
  43. client.StoreAsHash<UserInfo>(new UserInfo() { Id = 2, Name = "world", number = 0 });
  44. //如果id存在的话,则覆盖之前相同的id 他帮助我们序列化或者反射了一些事儿
  45. client.StoreAsHash<UserInfo>(new UserInfo() { Id = 2, Name = "world2" });
  46. //获取对象T中ID为id的数据。 必须要有属性id,不区分大小写
  47. Console.WriteLine(client.GetFromHash<UserInfo>(2).Name);
  48. var olduserinfo = client.GetFromHash<UserInfo>(2);
  49. olduserinfo.number = 4;
  50. client.StoreAsHash<UserInfo>(olduserinfo);
  51. Console.WriteLine("最后的结果" + client.GetFromHash<UserInfo>(2).number);
  52. client.StoreAsHash<UserInfoTwo>(new UserInfoTwo() { Id = "001", Name = "world2" });
  53. Console.WriteLine(client.GetFromHash<UserInfoTwo>("001").Name);
  54. client.StoreAsHash<UserInfoTwo>(new UserInfoTwo() { Id = "002", Name = "world" });
  55. Console.WriteLine(client.GetFromHash<UserInfoTwo>("002").Name);
  56. UserInfo lisi = new UserInfo() { Id = 1, Name = "李四", number = 0 };
  57. client.StoreAsHash<UserInfo>(lisi);
  58. Console.WriteLine(client.GetFromHash<UserInfo>(1).number);
  59. //做个自增
  60. var oldzhang = client.GetFromHash<UserInfo>(1);
  61. oldzhang.number++;
  62. client.StoreAsHash<UserInfo>(oldzhang);
  63. #endregion
  64. #region 获取所有hashid数据集的key/value数据集合
  65. client.FlushDb();
  66. Dictionary<string, string> pairs2 = new Dictionary<string, string>();
  67. pairs2.Add("id", "001");
  68. pairs2.Add("name", "world");
  69. client.SetRangeInHash(hashid, pairs2);
  70. var dics = client.GetAllEntriesFromHash(hashid);
  71. foreach (var item in dics)
  72. {
  73. Console.WriteLine(item.Key + ":" + item.Value);
  74. }
  75. #endregion
  76. #region 获取hashid数据集中的数据总数
  77. client.FlushDb();
  78. Dictionary<string, string> pairs3 = new Dictionary<string, string>();
  79. pairs3.Add("id", "001");
  80. pairs3.Add("name", "world");
  81. client.SetRangeInHash(hashid, pairs3);
  82. //自己做到心中有数
  83. Console.WriteLine(client.GetHashCount(hashid));
  84. #endregion
  85. #region 获取hashid数据集中所有key的集合
  86. client.FlushDb();
  87. Dictionary<string, string> pairs4 = new Dictionary<string, string>();
  88. pairs4.Add("id", "001");
  89. pairs4.Add("name", "world");
  90. client.SetRangeInHash(hashid, pairs4);
  91. var keys = client.GetHashKeys(hashid);
  92. foreach (var item in keys)
  93. {
  94. Console.WriteLine(item);
  95. }
  96. #endregion
  97. #region 获取hashid数据集中的所有value集合
  98. client.FlushDb();
  99. Dictionary<string, string> pairs5 = new Dictionary<string, string>();
  100. pairs5.Add("id", "001");
  101. pairs5.Add("name", "world");
  102. client.SetRangeInHash(hashid, pairs5);
  103. var values = client.GetHashValues(hashid);
  104. foreach (var item in values)
  105. {
  106. Console.WriteLine(item);
  107. }
  108. #endregion
  109. #region 删除hashid数据集中的key数据
  110. client.FlushDb();
  111. Dictionary<string, string> pairs6 = new Dictionary<string, string>();
  112. pairs6.Add("id", "001");
  113. pairs6.Add("name", "world");
  114. client.SetRangeInHash(hashid, pairs6);
  115. client.RemoveEntryFromHash(hashid, "id");
  116. var values6 = client.GetHashValues(hashid);
  117. foreach (var item in values6)
  118. {
  119. Console.WriteLine(item);
  120. }
  121. #endregion
  122. #region 判断hashid数据集中是否存在key的数据
  123. client.FlushDb();
  124. Dictionary<string, string> pairs7 = new Dictionary<string, string>();
  125. pairs7.Add("id", "001");
  126. pairs7.Add("name", "world");
  127. client.SetRangeInHash(hashid, pairs7);
  128. Console.WriteLine(client.HashContainsEntry(hashid, "id")); //T F
  129. Console.WriteLine(client.HashContainsEntry(hashid, "number"));// T F
  130. #endregion
  131. #region 给hashid数据集key的value加countby,返回相加后的数据
  132. client.FlushDb();
  133. Dictionary<string, string> pairs8 = new Dictionary<string, string>();
  134. pairs8.Add("id", "001");
  135. pairs8.Add("name", "world");
  136. pairs8.Add("number", "2");
  137. client.SetRangeInHash(hashid, pairs8);
  138. Console.WriteLine(client.IncrementValueInHash(hashid, "number", 2));
  139. //注意,存的值必须是数字类型,否则抛出异常
  140. #endregion
  141. #region 自定义
  142. HashTool.StoreAsHash<UserInfoTwo>(new UserInfoTwo() { Id = "10001", Name = "world" });
  143. var user = HashTool.GetFromHash<UserInfoTwo>("10001");
  144. Console.WriteLine("华丽丽的结束");
  145. #endregion
  146. }

hash在redis里面的存储格式有两种,ZipList和Hashtable。ZipList就是压缩版的list,field及其值会依次存储,这个时候在存储时都要先找到最后一个存储的位置。如果存储的key的长度越来越多了或者说这个类的属性越来越多,这个时候找到最后一个位置时就需要使用for循环。Redis经过测试发现,当这个key也就是类的属性个数不超过512或者任意一个key或value的长度小于等于64个字节时,redis的性能影响是忽略不计的,当超过这个限定值时,redis就会使用另外一种数据结构Hashtable进行存储。

HashTable存储时就是使用key-Value的形式进行存储。通过hash算法得出需要存储值对应的hash算法的出来的值作为特征,通过这个hash算法得出的特征值理论上是不会重复的。

哎, 后面全是算法+算法+算法 以后需要恶补一下算法,可能就恍然大悟了吧

Redis之品鉴之旅(二)的更多相关文章

  1. Redis之品鉴之旅(一)

    Redis之品鉴之旅(一) 好知识就如好酒,需要我们坐下来,静静的慢慢的去品鉴.Redis作为主流nosql数据库,在提升性能的方面是不可或缺的.下面就拿好小板凳,我们慢慢的来一一品鉴. 1)redi ...

  2. Redis之品鉴之旅(七)

    分布式锁 1)阻塞锁: 尝试在redis中创建一个字符串结构缓存,方法传入的key,value为锁的过期时间timeout的时间戳. 若redis中没有这个key,则创建成功(即抢到锁),然后立即返回 ...

  3. Redis之品鉴之旅(六)

    持久化 快照的方式(RDB) 文件追加方式(AOF) 快照形式: save和bgsave能快速的备份数据.但是.........., Save命令:将内存数据镜像保存为rdb文件,由于redis是单线 ...

  4. Redis之品鉴之旅(五)

    Redis事务 原子性:就是最小的单位 一致性:好多命令,要么全部执行成功,要么全部执行失败 隔离性:一个会话和另一个会话之间是互相隔离的 持久性:执行了就执行了,数据保存在硬盘上 典型例子:银行转账 ...

  5. Redis之品鉴之旅(四)

    发布订阅,简单场景下的发布订阅完全可以使用. 可以简单的理解,将一个公众号视为发布者,关注公众号的人视作订阅者,公众号发布一条文章或者消息,凡事订阅公众号的都可以收到消息.一个人可以订阅多个公众号,一 ...

  6. Redis之品鉴之旅(三)

    3)Set,可以去重的.无序的集合.可以取交集.并集.zset(sorted set),有序的.去重的集合,排序不是根据value排序,而是根据score排序. using (RedisClient ...

  7. redis成长之路——(二)

    redis操作封装 针对这些常用结构,StackExchange.Redis已经做了一些封装,不过在实际应用场景中还必须添加一些功能,例如重试等 所以对一些常功能做了一些自行封装SERedisOper ...

  8. Redis源码阅读(二)高可用设计——复制

    Redis源码阅读(二)高可用设计-复制 复制的概念:Redis的复制简单理解就是一个Redis服务器从另一台Redis服务器复制所有的Redis数据库数据,能保持两台Redis服务器的数据库数据一致 ...

  9. Redis指令与数据结构(二)

    0.Redis目录结构 1)Redis介绍及部署在CentOS7上(一) 2)Redis指令与数据结构(二) 3)Redis客户端连接以及持久化数据(三) 4)Redis高可用之主从复制实践(四) 5 ...

随机推荐

  1. wpf 绘图

  2. beeline: 新版连接Hive server的工具

    HiveServer2 支持一个新的命令行Shell,称为Beeline,它是基于SQLLine CLI的JDBC客户端.它是从 Hive 0.11版本引入的,是Hive新的命令行客户端工具.Hive ...

  3. Contos6.5卸载自带JDK

    1.查看CentOS6.5自带的JDK是否已经安装#Java -version2.查看JDK的信息#rpm -qa|grep java3.卸载JDK#rpm -e --nodeps tzdata-ja ...

  4. Google 开发console查找元素或方法

    F12 后 在console中输入: $("#R")[0] 查找ID 为R的元素, 如需打印出元素属性值,则输入: console.dir($("#R")[0] ...

  5. jQuery中的属性过滤选择器(四、五):[attribute] 、[attribute=value]、[attribute!=value] 、[attribute^=value] 等

    <!DOCTYPE html> <html> <head> <title>属性过滤选择器</title> <meta http-equ ...

  6. Oracle数据库 —— DDL

    时间:2016-10-5 14:55 逆风的方向更适合飞翔我不怕千万人阻挡只怕自己投降 --------------------------------------- 一.表的创建与管理1.表的基本操 ...

  7. Go版本依赖--伪版本

    目录 1.简介 2. 什么是伪版本 3. 伪版本风格 4. 如何获取伪版本 1.简介 在go.mod中通常使用语义化版本来标记依赖,比如v1.2.3.v0.1.5等.因为go.mod文件通常是go命令 ...

  8. java 接口代理

    接口 public interface Cc { void say(); } 实现类: public class C implements Cc{ @Override public void say( ...

  9. 跨平台APP推荐收藏

    时间:2019-04-11 整理:pangYuaner 标题:十大跨平台优秀软件 地址:https://www.cnblogs.com/the-king-of-cnblogs/p/3154758.ht ...

  10. 虚拟机VMWare开机黑屏 无法进入系统

    参考了: https://blog.csdn.net/x534119219/article/details/79497264 可能方案一: 关闭VMware Workstation加速3D图形设置 可 ...