zoukankan      html  css  js  c++  java
  • Redis学习二 C#中如何进行这五种数据类型的操作

    我在网上找了好久,就是没有找到Redis和C#结合的书,都是和别的编程语言在一起鬼混。

    简单的用C#实现向Redis中插入那我中类型的数据

    首先需要到NuGet 里面下载 Redis

    IDatabase cache = ConnectionMultiplexer.Connect("127.0.0.1").GetDatabase();

    这条语句创建一个Redis的实例

    string 字符串操作

    cache.StringSet("qiao", "1111");

    Console.WriteLine(cache.StringGet("qiao"));

    这是一个简单的读取

    List列表操作

            IDatabase cache = ConnectionMultiplexer.Connect("127.0.0.1").GetDatabase();
                for (int i = 0; i < 20; i++)
                {
                    cache.ListRightPush("qiao", i);
                }
                cache.ListRightPush(
                    "qiao",
                    "1234567890"
                );
                string sss = cache.ListLeftPop("qiao");
                Console.WriteLine(sss);
    
                Console.ReadLine();

    这里使用 ListRightPush 操作向Redis中存入数据

    然后使用 ListLeftPop 从Redis中读取数据

    集合操作

    cache.SetAdd("jihe1", "ssss");向内存中插入一条数据

                cache.SetAdd("jihe1", "ssss");
                cache.SetAdd("jihe1", "ssss1");
                cache.SetAdd("jihe1", "ssss1");
                RedisValue[] redisValue = cache.SetMembers("jihe1");
                foreach (var item in redisValue)
                {
                    Console.WriteLine(item);
                }    

    输出 

    散列

    cache.HashSet("hashSet", "hashKey", "hashValue");
    cache.HashSet("hashSet", "hashKey1", "qiao");
    cache.HashSet("hashSet", "hashKey2", "an");
    cache.HashSet("hashSet", "hashKey3", "sheng");
     HashEntry[] HashGet = cache.HashGetAll("hashSet");
    foreach (var item in HashGet)
    {
        Console.WriteLine(item.Name + "  ---  " + item.Value);
    }
    Console.ReadLine();

    输出 

    有序集合

    cache.SortedSetAdd("ssAdd", "Keys11", 1111);向内存中存入一条数据
    cache.SortedSetAdd("ssAdd", "Keys11", 1111);
    cache.SortedSetAdd("ssAdd", "Keys22", 2222);
    cache.SortedSetAdd("ssAdd", "Keys33", 3333);
    cache.SortedSetAdd("ssAdd", "Keys44", 4444);
    RedisValue[] sss = cache.SortedSetRangeByRank("ssAdd");
    for (int i = 0; i < sss.Length; i++)
    {
        Console.WriteLine(sss[i] + "  ---   " + sss[i].HasValue);
        Console.WriteLine();
    }
  • 相关阅读:
    HBase入门笔记(四)完全分布式HBase集群安装配置
    is not in sudoer file
    PHP学习之八:执行运算符与字符加一
    Windows Phone 7回车键获取
    Asp.Net重定向
    WindowsPhone7开发之 Push+Notification
    Windows phone 7开发之(页面间跳转与传值)
    Windows Phone 7开发者注册Marketplace
    Windows Phone7开发之 容器控件
    Windows Phone7开发之 其他控件
  • 原文地址:https://www.cnblogs.com/ansheng/p/5356676.html
Copyright © 2011-2022 走看看