ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

Redis之品鉴之旅(二)

2020-08-21 23:31:18  阅读:213  来源: 互联网

标签:Console 之旅 Redis Add client 品鉴 WriteLine hashid id


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


}

标签:Console,之旅,Redis,Add,client,品鉴,WriteLine,hashid,id
来源: https://www.cnblogs.com/vigorous/p/13543866.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有