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   itjeff  阅读(136)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示