Redis 使用demo

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

namespace RedisTest
{
    class Program
    {
        static void Main(string[] args)
        {
            
            //读取数据,如果缓存存在直接从缓存中读取,否则从数据库读取然后写入redis
            using (var redisClient = RedisManager.GetClient())
            {
                //Sorted Set类型,可手动指定score,优先级更高。没有指定就按照默认的字母排序。
                redisClient.AddItemToSortedSet("a5", "ffff", 10);
                redisClient.AddItemToSortedSet("a5", "bbbb");
                redisClient.AddItemToSortedSet("a5", "gggg");
                redisClient.AddItemToSortedSet("a5", "cccc", 20);
                redisClient.AddItemToSortedSet("a5", "waaa", 1);
                System.Collections.Generic.List<string> list = redisClient.GetAllItemsFromSortedSet("a5");
                foreach (string str in list)
                {
                    Console.WriteLine(str);
                }



                    ////Set类型
                    ////对Set类型进行操作
                    //redisClient.AddItemToSet("a3", "ddd");
                    //redisClient.AddItemToSet("a3", "ccc");
                    //redisClient.AddItemToSet("a3", "tttt");
                    //redisClient.AddItemToSet("a3", "sssh");
                    //redisClient.AddItemToSet("a3", "hhhh");
                    //System.Collections.Generic.HashSet<string> hashset = redisClient.GetAllItemsFromSet("a3");
                    //foreach (string str in hashset)
                    //{
                    //    Console.WriteLine(str);
                    //}
                    ////求并集
                    //redisClient.AddItemToSet("a3", "ddd");
                    //redisClient.AddItemToSet("a3", "ccc");
                    //redisClient.AddItemToSet("a3", "tttt");
                    //redisClient.AddItemToSet("a3", "sssh");
                    //redisClient.AddItemToSet("a3", "hhhh");
                    //redisClient.AddItemToSet("a4", "hhhh");
                    //redisClient.AddItemToSet("a4", "h777");
                    //hashset = redisClient.GetUnionFromSets(new string[] { "a3", "a4" });
                    //foreach (string str in hashset)
                    //{
                    //    Console.WriteLine(str);
                    //}
                    ////求交集
                    //hashset = redisClient.GetIntersectFromSets(new string[] { "a3", "a4" });
                    //foreach (string str in hashset)
                    //{
                    //    Console.WriteLine(str);
                    //}
                    ////求差集.
                    //hashset = redisClient.GetDifferencesFromSet("a3", new string[] { "a4" });
                    //foreach (string str in hashset)
                    //{
                    //    Console.WriteLine(str);
                    //}


                    ////List数据类型
                    ////作为队列使用
                    //redisClient.EnqueueItemOnList("name", "zhangsan");
                    //redisClient.EnqueueItemOnList("name", "lisi");
                    //long count = redisClient.GetListCount("name");
                    //for (int i = 0; i < count; i++)
                    //{
                    //    Console.WriteLine(redisClient.DequeueItemFromList("name"));
                    //}

                    ////作为栈使用
                    //redisClient.PushItemToList("name2", "wangwu");
                    //redisClient.PushItemToList("name2", "maliu");
                    //count = redisClient.GetListCount("name2");
                    //for (int i = 0; i < count; i++)
                    //{
                    //    Console.WriteLine(redisClient.PopItemFromList("name2"));
                    //}


                    ////Hash类型
                    //redisClient.SetEntryInHash("user", "userInfo", "aaaaaaaaaab");
                    //redisClient.SetEntryInHash("user", "address", "bbbb");
                    //redisClient.SetEntryInHash("user", "address", "shang hai");
                    //List<string> list1 = redisClient.GetHashKeys("user");
                    //var userinfo = redisClient.GetValueFromHash("user", "userInfo");//获取值
                    //var address = redisClient.GetValueFromHash("user", "address");//获取值



                    ////String类型 --存储List<Model>数据
                    ////string UserName;
                    //Student student1 = new Student() { ID=1, Name="张三", Age=21};
                    //Student student2 = new Student() { ID = 2, Name = "李四", Age = 22 };
                    //Student student3 = new Student() { ID = 3, Name = "王五", Age = 23 };
                    //List<Student> students = new List<Student>() { student1, student2, student3};
                    //redisClient.Set<Student>("student1", student1);
                    //var s1 =  redisClient.Get<Student>("student1");
                    //printStuInfo(s1);

                    //redisClient.Set<List<Student>>("students", students);
                    //var list = redisClient.Get<List<Student>>("students");
                    //printStuList(list);

                    ////String类型 --存储简单的字符串
                    //UserName = redisClient.Get<string>("UserInfo_123");
                    //if (string.IsNullOrEmpty(UserName)) //初始化缓存
                    //{
                    //    //TODO 从数据库中获取数据,并写入缓存
                    //    UserName = "张三";
                    //    redisClient.Set<string>("UserInfo_123", UserName, DateTime.Now.AddSeconds(10));
                    //    Console.WriteLine("数据库数据:" + "张三");
                    //    //return;
                    //}
                    //Console.WriteLine("Redis缓存数据:" + UserName);
                }

            Console.ReadLine();
        }

        public static void printStuInfo(Student s)
        {
            if (s != null)
            {
                Console.WriteLine($"ID={s.ID},Name={s.Name},Age={s.Age}");
            }
        }

        public static void printStuList(List<Student> list)
        {
            if (list != null)
            {
                foreach (var s in list)
                    printStuInfo(s);
            }
        }
    }
}

 

posted on 2021-02-25 13:31  itjeff  阅读(132)  评论(0编辑  收藏  举报

导航