Redis-3- C#

引入Nuget  C# for Redis:

https://github.com/ServiceStack/ServiceStack.Redis

 代码:

using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace RedisConsoleApp
{
    class Program
    {
        static RedisClient client = new RedisClient("127.0.0.1", 6379);

        static void Main(string[] args)
        {
            StringTest();
            HashTest();
            QueueTest();
            SetTest();
            SortedSetTest();
        }

        private static void StringTest() 
        {
            Console.WriteLine("*****************************************字符串类型*****************************************");
            client.Set<string>("name", "zouqj");

            string userName = client.Get<string>("name");

            Console.WriteLine(userName);

            UserInfo userInfo = new UserInfo() { UserName = "张三", UserPwd = "123" };

            client.Set<UserInfo>("UserInfo", userInfo);
            UserInfo user = client.Get<UserInfo>("UserInfo");
            Console.WriteLine(user.UserName);


            List<UserInfo> list = new List<UserInfo>() { 
                new UserInfo() { UserName = "李四", UserPwd = "12345" }, 
                new UserInfo(){UserName="王五", UserPwd="12345"} 
            };

            client.Set<List<UserInfo>>("list", list);

            List<UserInfo> userInfoList = client.Get<List<UserInfo>>("list");

            foreach (UserInfo u in userInfoList)
            {
                Console.WriteLine(u.UserName);
            }

            Console.ReadLine();
        }

        private static void HashTest() 
        {

            Console.WriteLine("***************************************Hash********************************************************");

            client.SetEntryInHash("userInfoId", "name", "zhangsan");
            var lstKeys = client.GetHashKeys("userInfoId");
            lstKeys.ForEach(k => Console.WriteLine(k));

            var lstValues = client.GetHashValues("userInfoId");
            lstValues.ForEach(v=>Console.WriteLine(v));

            client.Remove("userInfoId");
            Console.ReadLine();


        }

        private static void QueueTest()
        {

            Console.WriteLine("**************************************************队列 先进先出********************************************************");
            client.EnqueueItemOnList("test", "饶成龙");
            client.EnqueueItemOnList("test", "周文杰");

            long length = client.GetListCount("test");
            for (int i = 0; i < length; i++)
            {
                Console.WriteLine(client.DequeueItemFromList("test"));
            }

            Console.WriteLine("***********************************************栈 先进后出************************************************************");
            client.PushItemToList("name1", "邹琼俊");
            client.PushItemToList("name1", "周文杰");
            long length1 = client.GetListCount("name1");

            for (int i = 0; i < length1; i++)
            {
                Console.WriteLine(client.PopItemFromList("name1"));
            }

            Console.ReadLine();
        }

        private static void SetTest()
        {
            client.AddItemToSet("HighSchool", "卢沛");
            client.AddItemToSet("HighSchool", "卢沛1");
            client.AddItemToSet("HighSchool", "卢沛2");
            client.AddItemToSet("HighSchool", "卢沛3");
            client.AddItemToSet("HighSchool", "卢沛4");
            client.AddItemToSet("HighSchool", "卢沛5");
            client.AddItemToSet("HighSchool", "卢沛6");
            client.AddItemToSet("HighSchool", "卢沛7");
            client.AddItemToSet("HighSchool", "卢沛8");
            client.AddItemToSet("HighSchool", "卢沛9");
            client.AddItemToSet("HighSchool", "卢沛10");

            HashSet<string> hashset1 = client.GetAllItemsFromSet("HighSchool");
            Console.WriteLine("*****************************************************卢沛和以下人员是高中同学****************************");
            ConsoleHashSetInfo(hashset1);

            //求并集
            client.AddItemToSet("College", "卢沛");
            client.AddItemToSet("College", "卢沛10");
            client.AddItemToSet("College", "卢沛11");
            client.AddItemToSet("College", "卢沛12");
            client.AddItemToSet("College", "卢沛13");
            client.AddItemToSet("College", "卢沛14");

            HashSet<string> hashset2 = client.GetUnionFromSets(new string[] { "HighSchool", "College" });

            Console.WriteLine("*****************************************************卢沛和以下人员是高中/大学同学****************************");
            ConsoleHashSetInfo(hashset2);

            //求交集
            HashSet<string> hashset3 = client.GetIntersectFromSets(new string[] { "HighSchool", "College" });
            Console.WriteLine("*****************************************************卢沛和以下人员是高中和大学同学****************************");
            ConsoleHashSetInfo(hashset3);

            //求差集
            HashSet<string> hashset4 = client.GetDifferencesFromSet("HighSchool", new string[] { "College" });
            Console.WriteLine("*****************************************************卢沛和以下人员只是高中同学****************************");
            ConsoleHashSetInfo(hashset4);

            Console.ReadLine();

        }

        private static void ConsoleHashSetInfo(HashSet<string> hs)
        {
            foreach (string str in hs)
            {
                if (str == "卢沛")
                {
                    continue;
                }

                Console.WriteLine(str);
            }
        }

        private static void SortedSetTest()
        {
            client.AddItemToSortedSet("friends", "熊平", 1);
            client.AddItemToSortedSet("friends", "熊平3", 3);
            client.AddItemToSortedSet("friends", "熊平5", 5);
            client.AddItemToSortedSet("friends", "熊平2", 3);
            client.AddItemToSortedSet("friends", "熊平", 1);
            client.AddItemToSortedSet("friends", "熊平4", 4);
            List<string> list = client.GetAllItemsFromSortedSet("friends");
            foreach (string str in list)
            {
                Console.WriteLine(str);
            }

            Console.ReadLine();
        }

        class UserInfo
        {
            public string UserName { get; set; }

            public string UserPwd { get; set; }
        }
    }
}

  

猜你喜欢

转载自www.cnblogs.com/yanmantianxia/p/8932383.html