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算法得出的特征值理论上是不会重复的。

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

原文地址:https://www.cnblogs.com/vigorous/p/13543866.html