C#使用Redis最为常用的5种数据类型

Redis的client四个引用
ServiceStack.Common.dll
ServiceStack.Interfaces.dll
ServiceStack.Redis.dll
ServiceStack.Text.dll
也可以使用这个三个引用
ServiceStack.dll
ServiceStack.Interfaces.dll
ServiceStack.ServiceInferface.dll
链接: https://pan.baidu.com/s/13cbYSLyjiC6_M4s6wQxyxQ 提取码: kc9u
注:这个是using Newtonsoft.Json;
需要加Newtonsoft.Json.dll,自己可以网上下载一下
Redis最为常用的数据类型主要有以下五种:
String
Hash
List
Set
Sorted set

using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace ConsoleApplication2
{
    class Program
    {
        static RedisClient redisClient = new RedisClient("127.0.0.1", 6379); // 设置Redis服务IP和端口

        static void Main(string[] args)
        {
            #region String类型
            Console.WriteLine("String类型======================开始");
            //整形数据
            redisClient.Set<int>("pwd",123456);
            int psw = redisClient.Get<int>("pwd");
            Console.WriteLine(psw);
           
            //实体对象
            UserInfo user = new UserInfo() { UserName = "vinkong", UserPwd = "159753" };
            redisClient.Set<UserInfo>("userInfo", user);
            UserInfo a = redisClient.Get<UserInfo>("userInfo");
            Console.WriteLine(a.UserName+"==="+a.UserPwd);
            //List对象
            List<UserInfo> list = new List<UserInfo>() { new UserInfo {UserName ="vingkong1",UserPwd="123456"},
             new UserInfo{UserName="vinkong2",UserPwd="123789"},new UserInfo{UserName ="vinkong3",UserPwd="159741"}};
            redisClient.Set<List<UserInfo>>("list", list);
            List<UserInfo> b = redisClient.Get<List<UserInfo>>("list");
            foreach (var item in b)
            {  
                Console.WriteLine(item.UserName+"==="+item.UserPwd);
            }
            Console.WriteLine("String类型======================结束");
            #endregion

            #region Hash类型
            Console.WriteLine("Hash类型======================开始");
            //hashtale
            for (int i = 0; i < 10; i++)
            {
                redisClient.SetEntryInHash("hashTable", "test" + i.ToString(), JsonConvert.SerializeObject(new
                {
                    id = i + 1,
                    name = "wolfy" + i.ToString()
                })); 
            }
            //获取hashTabel
            List<string> woftlist = redisClient.GetHashValues("hashTable");
            foreach (var item in woftlist)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("Hash类型======================结束");
            #endregion


            #region List类型

            Console.WriteLine("List类型======================开始");
            //队列的使用
            redisClient.EnqueueItemOnList("s", "a");
                redisClient.EnqueueItemOnList("s","v");
                int count = redisClient.GetListCount("s");
                for (int i = 0; i < count; i++)
                {
                    Console.WriteLine(redisClient.DequeueItemFromList("s"));
                }
            //栈的使用
                redisClient.PushItemToList("name2", "李四");
                redisClient.PushItemToList("name2", "张三");
                int count2 = redisClient.GetListCount("name2");   
            for (int i = 0; i < count2; i++)
                {
                    Console.WriteLine(redisClient.PopItemFromList("name2"));
                }

            Console.WriteLine("List类型======================结束");
            #endregion
            #region Set类型
            Console.WriteLine("Set类型======================开始");
            //它是string 类型的无序集合。set是通过hash table实现的,添加、删除、和查找、对集合我们可以去并集、交集、差集
             redisClient.AddItemToSet("a3", "ddd");
             redisClient.AddItemToSet("a3", "ccc");
             redisClient.AddItemToSet("a3", "tttt");
             redisClient.AddItemToSet("a3", "sssh");
             redisClient.AddItemToSet("a3", "hhhh");
             HashSet<string> hs = redisClient.GetAllItemsFromSet("a3");
             foreach (var item in hs)
             {
                 Console.WriteLine(item);
             }
             Console.WriteLine("============并集");
            //求并集
             redisClient.AddItemToSet("a4","ddd");
             redisClient.AddItemToSet("a4", "ccc");
             redisClient.AddItemToSet("a4", "hhhh");
             redisClient.AddItemToSet("a4", "4444");
             HashSet<string> hs2 = redisClient.GetUnionFromSets(new string[] { "a3", "a4" });
             foreach (var item in hs2)
             {
                 Console.WriteLine(item);
             }
             Console.WriteLine("============交集");
             HashSet<string> hs3 = redisClient.GetIntersectFromSets(new string[]{"a3","a4"});
             foreach (var item in hs3)
             {
                 Console.WriteLine(item);
             }
             //返回存在于第一个集合,但是不存在于其他集合的数据。差集
            Console.WriteLine("============差集");
            HashSet<string> hs4 = redisClient.GetDifferencesFromSet("a3", new string[] { "a4" });
            foreach (var item in hs4)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("Set类型======================结束");
            #endregion

            #region Sorted Set类型
            Console.WriteLine("Sorted Set类型======================开始");
            //当你需要一个有序的并且不重复的集合列表,那么可以选择sorted set数据结构
            
           //默认不设置序号,则会按照插入顺序来展示
            redisClient.AddItemToSortedSet("SetSorted","vinkong1");
            redisClient.AddItemToSortedSet("SetSorted","vinkong2");
            redisClient.AddItemToSortedSet("SetSorted","vinkong3");
            List<string> hs5 = redisClient.GetAllItemsFromSortedSet("SetSorted");
            foreach (var item in hs5)
            {
                Console.WriteLine("SetSorted 不设置序号{0}",item);
            }
            //设置序号
            redisClient.AddItemToSortedSet("SetSorted", "vinkong1",4);
            redisClient.AddItemToSortedSet("SetSorted", "vinkong2",1);
            redisClient.AddItemToSortedSet("SetSorted", "vinkong3",3);
            List<string> hs6 = redisClient.GetAllItemsFromSortedSet("SetSorted");
            //按序号由小到大展示
            foreach (var item in hs6)
            {
                Console.WriteLine("SetSorted 设置序号{0}",item);
            }
            Console.WriteLine("Sorted Set类型======================结束");
            #endregion
            Console.ReadKey();
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值