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

7 篇文章 0 订阅
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();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

假装我不帅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值