代码下载地址

https://download.csdn.net/download/g313105910/18232424

一、测试服务器是否工作正常

redis-cli -h 10.0.0.7 -p 6379 -a "qwe123456"

二、超时时间设置

PSETEX key1 EXPIRY_IN_MILLISECONDS value1时间是毫秒,不绝对

PSETEX testvalue 30000 "2312"

超时请求数据就没了

三、建立测试项目并安装依赖

 使用NuGet安装ServiceStack.Redis,这是微软提供已经封装好的对redis操作类

创建链接

public static ServiceStack.Redis.RedisClient client = new ServiceStack.Redis.RedisClient("10.0.0.7", 6379,"qwe123456",1); 

1、String使用,设置过期 

代码

using System;
using System.Windows;

namespace RedisTest
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public static ServiceStack.Redis.RedisClient client = new ServiceStack.Redis.RedisClient("10.0.0.7", 6379,"qwe123456",1);

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //存储
            client.Set<string>("name", "george");
            //设置有效期时间长度
            client.Set<string>("ages", "george",new TimeSpan(0,1,0));
            //设置到期时间
            client.Set<string>("password", "qwe123456", new DateTime(2021, 4, 28, 18, 40, 0));
            //读取
            string name = client.Get<string>("name");
            string ages = client.Get<string>("ages");
            string pwd = client.Get<string>("password");
            Console.WriteLine($"name:{name},ages:{ages},pwd:{pwd},dateTime:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
        }
    }
}

2、List使用

代码

private void Button_Test_List(object sender, RoutedEventArgs e)
        {
            #region List队列操作
            client.EnqueueItemOnList("QueueList", "打印任务1"); //入队
            client.EnqueueItemOnList("QueueList", "打印任务2");
            client.EnqueueItemOnList("QueueList", "打印任务3");
            client.EnqueueItemOnList("QueueList", "打印任务4");
            long q = client.GetListCount("QueueList");
            Console.WriteLine("打印任务按照顺序打印开始");
            for (int i = 0; i < q; i++)
            {
                Console.WriteLine("QueueList出队值:{0}", client.DequeueItemFromList("QueueList"));
            }
            Console.WriteLine("打印任务按照顺序打印完成");
            #endregion
            #region 栈操作
            client.PushItemToList("StackList", "入栈操作1"); //入栈
            client.PushItemToList("StackList", "入栈操作2");
            client.PushItemToList("StackList", "入栈操作3");
            client.PushItemToList("StackList", "入栈操作4");
            Console.WriteLine("开始出栈");
            long p = client.GetListCount("StackList");
            for (int i = 0; i < p; i++)
            {
                Console.WriteLine("StackList出栈值:{0}", client.PopItemFromList("StackList"));
            }
            Console.WriteLine("出栈完成");
            #endregion
        }

3、Hash字典数据结构使用

代码

public static ServiceStack.Redis.RedisClient client = new ServiceStack.Redis.RedisClient("10.0.0.7", 6379,"qwe123456",1); 
private void Button_Test_List(object sender, RoutedEventArgs e)
        {
            Dictionary<string, string> configDic = new Dictionary<string, string>();
            configDic.Add("IP", "config_localhost");
            configDic.Add("Port", "config_1521");
            configDic.Add("ServiceName", "config_orcl");
            configDic.Add("UserName", "config_dfd");
            configDic.Add("Password", "config_cppepass");
            client.SetRangeInHash("config", configDic);
            Dictionary<string, string> testHash = client.GetAllEntriesFromHash("config");
            foreach (var item in testHash)
            {
                Console.WriteLine("Hash的key为:{0} 值为:{1}", item.Key, item.Value);
            }
        }

4、有序集合求交集

string类型的无序集合。AddItem是通过hash table实现的,可以进行添加、删除和查找。对集合我们可以取并集,交集,差集.集合可以进行 交集、并集获取两个用户共同的关注人等

代码

private void Button_Test_Item(object sender, RoutedEventArgs e)
        {
            #region Set操作
            client.AddItemToSet("微信用户1", "好友A");
            client.AddItemToSet("微信用户1", "好友B");
            client.AddItemToSet("微信用户1", "好友C");
            client.AddItemToSet("微信用户1", "好友D");

            client.AddItemToSet("微信用户2", "好友C");
            client.AddItemToSet("微信用户2", "好友F");
            client.AddItemToSet("微信用户2", "好友G");
            client.AddItemToSet("微信用户2", "好友D");
            var setunion = client.GetIntersectFromSets("微信用户1", "微信用户2");
            Console.WriteLine("微信用户1和微信用户2的共同好友为:");
            foreach (var item in setunion)
            {
                Console.WriteLine(item);
            }
            #endregion
        }

5、排序

从小大大

client.GetRangeWithScoresFromSortedSet("LiveSet", 0, 3)

从大到小

client.GetRangeWithScoresFromSortedSetDesc("LiveSet", 0, 3)

private void Button_Test_ItemTest(object sender, RoutedEventArgs e)
        {
            #region Set操作
            client.AddItemToSortedSet("LiveSet", "直播带货1", 124524);
            client.AddItemToSortedSet("LiveSet", "直播带货2", 5235352);
            client.AddItemToSortedSet("LiveSet", "直播带货3", 5646);
            client.AddItemToSortedSet("LiveSet", "直播带货4", 1766);
            client.AddItemToSortedSet("LiveSet", "直播带货5", 5332);
            client.IncrementItemInSortedSet("LiveSet", "直播带货6", new Random().Next(200, 500));
            Console.WriteLine("直播带货销量榜:");
            foreach (var item in client.GetRangeWithScoresFromSortedSetDesc("LiveSet", 0, 3))
            {
                Console.WriteLine($"名:{item.Key} 带货量:{item.Value}");
            }
            #endregion
        }

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐