首页 > 其他分享 >radis简单学习笔记

radis简单学习笔记

时间:2022-12-03 17:11:31浏览次数:68  
标签:radis ServiceStack Redis 笔记 学习 stopwatch new model StackExchange

原来写接口只用了本机缓存cache

来学习一下radis,用法应该跟cache一样吧,为了配套负载均衡的多服务器是多个服务器都可以读取缓存

一、下载

找了好长时间

github有的时候能上有的时候就上不去

等了一会可以访问了就给下载下来了

分享一下:

绿色版链接:绿色版下载链接  提取码:bz04

msi版链接:安装包版下载链接  提取码:7lu6

二、安装

我原来安装过。

绿色版:待补充

msi安装包:很简单一直Next就行

三、使用

C#  调用

在查询相关文档时发现有

stackExchange.Radis 和 ServiceStack.Redis两种引用文件

查阅文档时发现统一说ServiceStack.Redis这个是要收费的每小时限制6000次读写,stackExchange.Radis则免费

文档:

关于c#:StackExchange.Redis和ServiceStack.Redis之间的区别

C#教程之ServiceStack.Redis 和 StackExchange.Redis 性能比

Redis中ServiceStack.Redis和StackExchange.Redis区别是什么

C# ServiceStack.Redis和StackExchange.Redis 使用心得

『性能』ServiceStack.Redis 和 StackExchange.Redis 性能比较

Redis中ServiceStack.Redis和StackExchange.Redis区别详解

眼见为实写了一个Demo测试一下:

每小时确实有读写限制提示内容:

ServiceStack.LicenseException:“The free-quota limit on '6000 Redis requests per hour' has been reached. Please see https://servicestack.net to upgrade to a commercial license or visit https://github.com/ServiceStackV3/ServiceStackV3 to revert back to the free ServiceStack v3.”

四、总结

StackExchange.Redis:免费,我所了解的是只能存字符串,对象或者集合需要转JSON存储

ServiceStack.Redis:每小时6000条限制,但是操作比StackExchange.Redis好用。

附上测试代码:

 

using Newtonsoft.Json;
using ServiceStack.Redis;
using StackExchange.Redis;
using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            Thread thread = new Thread(ServiceStackRedis);
            Thread thread1 = new Thread(StackExchangeRedis);
            thread.Start();
            thread1.Start();
        }

        private void StackExchangeRedis()
        {
            string a = "";
            Class1 aa = JsonConvert.DeserializeObject<Class1>(a);
            ConnectionMultiplexer client = ConnectionMultiplexer.Connect("127.0.0.1:6379,defaultDatabase=0");
            IDatabase database1 = client.GetDatabase();
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            for (int i = 0; i < 10000; i++)
            {
                string str = database1.StringGet("StackExchangeRedis");
                string resultStr = "";
                Class1 model = new Class1();
                if (string.IsNullOrEmpty(str))
                {
                    model = new Class1() { name = "张三", age = i };
                }
                else
                {
                    model = JsonConvert.DeserializeObject<Class1>(str);
                    model.age = i;
                }
                resultStr = JsonConvert.SerializeObject(model);
                database1.StringSet("StackExchangeRedis", resultStr);
            }
            stopwatch.Stop();
            TimeSpan timeSpan = stopwatch.Elapsed;
            label3.Text = Convert.ToString(timeSpan.TotalSeconds);
        }

        private void ServiceStackRedis()
        {
            RedisClient client = new RedisClient("127.0.0.1", 6379);       //本机IP,Redis默认端口是6379
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            for (int i = 0; i < 10000; i++)
            {
                Class1 model = client.Get<Class1>("ServiceStackRedis");
                if (model == null)
                {
                    model = new Class1() { name = "张三",age = i};
                }
                else
                {
                    model.age = i;
                }
                client.Set<object>("ServiceStackRedis", model);
            }
            stopwatch.Stop();
            TimeSpan timeSpan = stopwatch.Elapsed;
            label4.Text = Convert.ToString(timeSpan.TotalSeconds);
        }
    }
}

 

  

 

标签:radis,ServiceStack,Redis,笔记,学习,stopwatch,new,model,StackExchange
From: https://www.cnblogs.com/xiaosongboke/p/16947759.html

相关文章