radis简单学习笔记
原来写接口只用了本机缓存cache
来学习一下radis,用法应该跟cache一样吧,为了配套负载均衡的多服务器是多个服务器都可以读取缓存
一、下载
找了好长时间
github有的时候能上有的时候就上不去
等了一会可以访问了就给下载下来了
分享一下:
绿色版链接:绿色版下载链接 提取码:bz04
msi版链接:安装包版下载链接 提取码:06dz
二、安装
我原来安装过。
绿色版:待补充
msi安装包:很简单一直Next就行
更改redis密码
# 进入 redis 命令行 redis-cli.exe # 连接 redis 服务器,输入当前密码 auth old-password # 修改 redis 服务器密码 config set requirepass new-password # 查看当前 redis 服务器密码 config get requirepass # 退出 redis 命令行 exit
报错:ERR Client sent AUTH, but no password is set
还有一种方式就是一劳永逸的方式,就是直接修改配置文件里的参数。在redis.windows.conf(我的是这个配置文件)或者redis.conf(我看网上有说是这个配置文件的)的配置文件中找到requirepass这个参数,设置参数密码,然后保存配置文件,重启Redis。
# requirepass foobared
改为
requirepass 123456 //123456是设置的密码
原来Redis启动时需要指定配置文件,否则还会使用默认配置,而我在Windows里启动.exe应用程序时,还是习惯性的双击应用程序启动,导致Redis一直使用的是默认配置。这样我们就需要在命令行窗口通过命令行的方式来启动并指定配置文件:
C:\Program Files (x86)\Redis-x64-3.2.100>redis-server.exe redis.windows.conf
三、使用
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);
}
}
}

浙公网安备 33010602011771号