通过C#学习redis02(哈希操作)

static void Main(string[] args)
{
    
    
    RedisClient cli = new RedisClient("127.0.0.1:6379,password=,defaultDatabase=0");
    #region hash学习
    //为哈希表的域设值
    cli.HSet<string>("hash01", "name", "小明");
    cli.HSet<int>("hash01", "age", 20);
    Console.WriteLine(cli.HGet<string>("hash01", "name"));
    Console.WriteLine(cli.HGet<int>("hash01", "age"));
    //为哈希表不存在的域设值
    cli.HSetNx<string>("hash01", "name", "小明01");
    Console.WriteLine(cli.HGet<string>("hash01", "name"));
    cli.HSetNx<string>("hash01", "class", "大一");
    Console.WriteLine(cli.HGet<string>("hash01", "class"));
    //设置多个域和值到哈希表中
    cli.HMSet("hash02", "name", "小张", "age", 23);
    //获取hash表中所有的域和值
    Console.WriteLine("获取hash表中所有的域和值");
    var hashAll = cli.HGetAll("hash01");
    foreach (var h in hashAll)
    {
    
    
        Console.WriteLine(h.Key + ":" + h.Value);
    }
    //获取多个域的值
    Console.WriteLine("获取多个域的值");
    var hash02Arr = cli.HMGet("hash02","name","age");
    foreach (var h in hash02Arr)
    {
    
    
        Console.WriteLine(h);
    }
    //获取哈希表中的所有域
    Console.WriteLine("获取哈希表中的所有域");
    foreach (var item in cli.HKeys("hash01"))
    {
    
    
        Console.WriteLine(item);
    }
    //获取哈希表中的所有值
    Console.WriteLine("获取哈希表中的所有值");
    foreach (var item in cli.HVals("hash01"))
    {
    
    
        Console.WriteLine(item);
    }
    //统计哈希表中域的数量
    Console.WriteLine(cli.HLen("hash01"));
    //统计域的值的字符串长度
    Console.WriteLine(cli.HStrLen("hash01","name"));
    //为哈希表中的域加上增量值
    cli.HIncrBy("hincrby01","age",1);
    Console.WriteLine(cli.HGet("hincrby01","age"));
    cli.HIncrBy("hincrby01", "age", -2);
    Console.WriteLine(cli.HGet("hincrby01", "age"));
    //为哈希表中的域加上浮点数增量值
    cli.HIncrByFloat("hincrby01", "salary", 1000.2M);
    Console.WriteLine(cli.HGet("hincrby01", "salary"));
    cli.HIncrByFloat("hincrby01", "salary", -20.2M);
    Console.WriteLine(cli.HGet("hincrby01", "salary"));
    //删除哈希表中的多个域
    cli.HDel("hincrby01","age", "salary");
    Console.WriteLine(cli.HLen("hincrby01"));
    //判断哈希表中的域是否存在
    Console.WriteLine(cli.HExists("hincrby01", "age"));
    Console.WriteLine(cli.HExists("hash01", "name"));
    #endregion
    Console.WriteLine("执行完毕");
    Console.ReadKey();
}

猜你喜欢

转载自blog.csdn.net/qq_36437991/article/details/131583416