Redis之品鉴之旅(二)
2)hash类型,上代码
using (RedisClient client = new RedisClient("127.0.0.1", 6379, "12345", 10))
{
//删除当前数据库中的所有Key 默认删除的是db0
client.FlushDb();
//删除所有数据库中的key
//client.FlushAll();
//大key
string hashid = "pengbo";
#region 向hashid集合中添加key/value
client.SetEntryInHash(hashid, "id", "001");
Console.WriteLine(client.GetValuesFromHash(hashid, "id").FirstOrDefault());
client.SetEntryInHash(hashid, "name", "world");
Console.WriteLine(client.GetValuesFromHash(hashid, "name").FirstOrDefault());
client.SetEntryInHash(hashid, "socre", "100");
Console.WriteLine(client.GetValuesFromHash(hashid, "socre").FirstOrDefault());
#endregion
#region 批量新增key的值
client.FlushDb();
Dictionary<string, string> pairs = new Dictionary<string, string>();
pairs.Add("id", "001");
pairs.Add("name", "world");
client.SetRangeInHash(hashid, pairs);
//获取当前key的值
Console.WriteLine(client.GetValueFromHash(hashid, "id"));
Console.WriteLine(client.GetValueFromHash(hashid, "name"));
//一次性的获取所有想要获取的小key(属性的)值 如果key不存在,则返回空,不抛出异常
var list = client.GetValuesFromHash(hashid, "id", "name", "abc");
Console.WriteLine("*********");
foreach (var item in list)
{
Console.WriteLine(item);
}
#endregion
#region 如果hashid集合中存在key/value则不添加返回false,如果不存在在添加key/value,返回true
client.FlushDb();
Console.WriteLine(client.SetEntryInHashIfNotExists(hashid, "name", "你好美"));
Console.WriteLine(client.SetEntryInHashIfNotExists(hashid, "name", "你好美 哈哈哈"));
Console.WriteLine(client.GetValuesFromHash(hashid, "name").FirstOrDefault());
#endregion
#region 存储对象T t到hash集合中
client.FlushDb();
//urn: 类名: id的值
client.StoreAsHash<UserInfo>(new UserInfo() { Id = 2, Name = "world", number = 0 });
//如果id存在的话,则覆盖之前相同的id 他帮助我们序列化或者反射了一些事儿
client.StoreAsHash<UserInfo>(new UserInfo() { Id = 2, Name = "world2" });
//获取对象T中ID为id的数据。 必须要有属性id,不区分大小写
Console.WriteLine(client.GetFromHash<UserInfo>(2).Name);
var olduserinfo = client.GetFromHash<UserInfo>(2);
olduserinfo.number = 4;
client.StoreAsHash<UserInfo>(olduserinfo);
Console.WriteLine("最后的结果" + client.GetFromHash<UserInfo>(2).number);
client.StoreAsHash<UserInfoTwo>(new UserInfoTwo() { Id = "001", Name = "world2" });
Console.WriteLine(client.GetFromHash<UserInfoTwo>("001").Name);
client.StoreAsHash<UserInfoTwo>(new UserInfoTwo() { Id = "002", Name = "world" });
Console.WriteLine(client.GetFromHash<UserInfoTwo>("002").Name);
UserInfo lisi = new UserInfo() { Id = 1, Name = "李四", number = 0 };
client.StoreAsHash<UserInfo>(lisi);
Console.WriteLine(client.GetFromHash<UserInfo>(1).number);
//做个自增
var oldzhang = client.GetFromHash<UserInfo>(1);
oldzhang.number++;
client.StoreAsHash<UserInfo>(oldzhang);
#endregion
#region 获取所有hashid数据集的key/value数据集合
client.FlushDb();
Dictionary<string, string> pairs2 = new Dictionary<string, string>();
pairs2.Add("id", "001");
pairs2.Add("name", "world");
client.SetRangeInHash(hashid, pairs2);
var dics = client.GetAllEntriesFromHash(hashid);
foreach (var item in dics)
{
Console.WriteLine(item.Key + ":" + item.Value);
}
#endregion
#region 获取hashid数据集中的数据总数
client.FlushDb();
Dictionary<string, string> pairs3 = new Dictionary<string, string>();
pairs3.Add("id", "001");
pairs3.Add("name", "world");
client.SetRangeInHash(hashid, pairs3);
//自己做到心中有数
Console.WriteLine(client.GetHashCount(hashid));
#endregion
#region 获取hashid数据集中所有key的集合
client.FlushDb();
Dictionary<string, string> pairs4 = new Dictionary<string, string>();
pairs4.Add("id", "001");
pairs4.Add("name", "world");
client.SetRangeInHash(hashid, pairs4);
var keys = client.GetHashKeys(hashid);
foreach (var item in keys)
{
Console.WriteLine(item);
}
#endregion
#region 获取hashid数据集中的所有value集合
client.FlushDb();
Dictionary<string, string> pairs5 = new Dictionary<string, string>();
pairs5.Add("id", "001");
pairs5.Add("name", "world");
client.SetRangeInHash(hashid, pairs5);
var values = client.GetHashValues(hashid);
foreach (var item in values)
{
Console.WriteLine(item);
}
#endregion
#region 删除hashid数据集中的key数据
client.FlushDb();
Dictionary<string, string> pairs6 = new Dictionary<string, string>();
pairs6.Add("id", "001");
pairs6.Add("name", "world");
client.SetRangeInHash(hashid, pairs6);
client.RemoveEntryFromHash(hashid, "id");
var values6 = client.GetHashValues(hashid);
foreach (var item in values6)
{
Console.WriteLine(item);
}
#endregion
#region 判断hashid数据集中是否存在key的数据
client.FlushDb();
Dictionary<string, string> pairs7 = new Dictionary<string, string>();
pairs7.Add("id", "001");
pairs7.Add("name", "world");
client.SetRangeInHash(hashid, pairs7);
Console.WriteLine(client.HashContainsEntry(hashid, "id")); //T F
Console.WriteLine(client.HashContainsEntry(hashid, "number"));// T F
#endregion
#region 给hashid数据集key的value加countby,返回相加后的数据
client.FlushDb();
Dictionary<string, string> pairs8 = new Dictionary<string, string>();
pairs8.Add("id", "001");
pairs8.Add("name", "world");
pairs8.Add("number", "2");
client.SetRangeInHash(hashid, pairs8);
Console.WriteLine(client.IncrementValueInHash(hashid, "number", 2));
//注意,存的值必须是数字类型,否则抛出异常
#endregion
#region 自定义
HashTool.StoreAsHash<UserInfoTwo>(new UserInfoTwo() { Id = "10001", Name = "world" });
var user = HashTool.GetFromHash<UserInfoTwo>("10001");
Console.WriteLine("华丽丽的结束");
#endregion
}
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之品鉴之旅(二)的更多相关文章
- Redis之品鉴之旅(一)
Redis之品鉴之旅(一) 好知识就如好酒,需要我们坐下来,静静的慢慢的去品鉴.Redis作为主流nosql数据库,在提升性能的方面是不可或缺的.下面就拿好小板凳,我们慢慢的来一一品鉴. 1)redi ...
- Redis之品鉴之旅(七)
分布式锁 1)阻塞锁: 尝试在redis中创建一个字符串结构缓存,方法传入的key,value为锁的过期时间timeout的时间戳. 若redis中没有这个key,则创建成功(即抢到锁),然后立即返回 ...
- Redis之品鉴之旅(六)
持久化 快照的方式(RDB) 文件追加方式(AOF) 快照形式: save和bgsave能快速的备份数据.但是.........., Save命令:将内存数据镜像保存为rdb文件,由于redis是单线 ...
- Redis之品鉴之旅(五)
Redis事务 原子性:就是最小的单位 一致性:好多命令,要么全部执行成功,要么全部执行失败 隔离性:一个会话和另一个会话之间是互相隔离的 持久性:执行了就执行了,数据保存在硬盘上 典型例子:银行转账 ...
- Redis之品鉴之旅(四)
发布订阅,简单场景下的发布订阅完全可以使用. 可以简单的理解,将一个公众号视为发布者,关注公众号的人视作订阅者,公众号发布一条文章或者消息,凡事订阅公众号的都可以收到消息.一个人可以订阅多个公众号,一 ...
- Redis之品鉴之旅(三)
3)Set,可以去重的.无序的集合.可以取交集.并集.zset(sorted set),有序的.去重的集合,排序不是根据value排序,而是根据score排序. using (RedisClient ...
- redis成长之路——(二)
redis操作封装 针对这些常用结构,StackExchange.Redis已经做了一些封装,不过在实际应用场景中还必须添加一些功能,例如重试等 所以对一些常功能做了一些自行封装SERedisOper ...
- Redis源码阅读(二)高可用设计——复制
Redis源码阅读(二)高可用设计-复制 复制的概念:Redis的复制简单理解就是一个Redis服务器从另一台Redis服务器复制所有的Redis数据库数据,能保持两台Redis服务器的数据库数据一致 ...
- Redis指令与数据结构(二)
0.Redis目录结构 1)Redis介绍及部署在CentOS7上(一) 2)Redis指令与数据结构(二) 3)Redis客户端连接以及持久化数据(三) 4)Redis高可用之主从复制实践(四) 5 ...
随机推荐
- Mysql 之 IFNULL(expr1,expr2) 对空不可判
目标 当传入参数 @OrderId为空时 不做过滤 sql语句如下 SELECT o.* FROM `order` AS o LEFT JOIN receivemoneyconfirm AS re O ...
- 阿里云sql监控配置-druid
今天我们说说数据源和数据库连接池,熟悉java开发的同仁应该都了解C3PO,在这里不做过多的赘述了,今天我们说的是阿里DRUID,druid是后起之秀,因为它的优秀很快占领了使用市场,下边我们一起来看 ...
- jq的常用事件及其案例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Python如何读写Excel文件-使用xlrd/xlwt模块
时间: 2020-08-18 整理: qiyuan 安装和导入 1.模块介绍 在 python 中使用 xlrd/xlwt 和 openpyxl 模块可以对Excel电子表格(xls.xlsx文件)进 ...
- Node.js 应用全链路追踪技术——[全链路信息获取]
全链路追踪技术的两个核心要素分别是 全链路信息获取 和 全链路信息存储展示. Node.js 应用也不例外,这里将分成两篇文章进行介绍:第一篇介绍 Node.js 应用全链路信息获取, 第二篇介绍 N ...
- Hamcrest 断言框架
Hamcrest是一个为了测试为目的,能组合成灵活表达式的匹配器类库.用于编断言的框架,使用这个框架编写断言,提高可读性及开发测试的效率,提供了大量"匹配器"方法,每个匹配器用于执 ...
- Intel® QAT加速卡之Ring & Ring Bank
1. QAT的应用模式 Intel 通讯系列芯片对于每种受支持的加速服务(加密,数据压缩),都支持以下应用模式: 内核模式,其中应用程序和加速服务都在内核中运行空间. 用户空间直接访问在用户空间中运行 ...
- python3 spider [ urllib.request ]
# # 导入urllib库的urlopen函数 # from urllib.request import urlopen # # 发出请求,获取html # html = urlopen(" ...
- 【第三篇】- Git 工作流程之Spring Cloud直播商城 b2b2c电子商务技术总结
Git 工作流程 本章节我们将为大家介绍 Git 的工作流程. 一般工作流程如下: 克隆 Git 资源作为工作目录. 在克隆的资源上添加或修改文件. 如果其他人修改了,你可以更新资源. 在提交前查看修 ...
- Spring Boot中使用@Async的时候,千万别忘了线程池的配置!
上一篇我们介绍了如何使用@Async注解来创建异步任务,我可以用这种方法来实现一些并发操作,以加速任务的执行效率.但是,如果只是如前文那样直接简单的创建来使用,可能还是会碰到一些问题.存在有什么问题呢 ...