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好用。
附上测试代码:
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | 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); } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!