1. 程式人生 > 其它 >radis簡單學習筆記

radis簡單學習筆記

原來寫介面只用了本機快取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);
        }
    }
}