文章目录
Redis简介
redis服务端:
https://github.com/MSOpenTech/redis/releases/download/win-3.2.100/Redis-x64-3.2.100.msi
redis可视化工具:
https://github.com/uglide/RedisDesktopManager/releases/download/0.8.7/redis-desktop-manager-0.8.7.317.exe
Redis官方网址 http://redis.io
最新版Redis(windows版)下载网址 https://github.com/MSOpenTech/redis/releases
最新版redis可视化工具 下载网址 https://redisdesktop.com/download
下载安装Redis-x64-3.2.100.msi后,即可将redis-server.exe作为windows服务启动
Redis默认端口是 6379 ,正式环境请更改端口,以免被攻击。
安装过程
https://www.runoob.com/redis/redis-install.html
其它参考:
C#操作redis
Redis的使用初探
C# StackExchange.Redis 用法总结
1、连接Redis
使用NuGet安装ServiceStack.Redis,这是微软提供已经封装好的对redis操作类。
因为ServiceStack从4.0+版本就开始收费了(免费版有每小时6000次请求限制),所以使用了PM > Install-Package ServiceStack -Version 3.9.71
关于ServiceStack介绍
提到构建WebService服务,大家肯定第一个想到的是使用WCF,因为简单快捷嘛。首先要说明的是,本人对WCF不太了解,但是想快速建立一个WebService,于是看到了MSDN上的这一篇文章 Building Cross-Platform Web Services with ServiceStack,所以这里简要介绍一下如何使用ServiceStack快速建立一个WebService服务。
当然,在开始之前,首先要说明一下ServiceStack是个什么东西。 在国内用ServiceStack的似乎很少,大部分都是WCF或者ASP.NET WebAPI,唯一接触ServiceStack的可能是在C# 中调用Redis的时候,有个ServiceStack.Redis,之前还写过一篇 .NET中使用Redis 的拙文。这个ServiceStack.Redis其实就是ServiceStack的一个组件,专门用来跟Redis进行交互的,这部分内容引述自麦田守望者LEO
using ServiceStack.Redis;
using ServiceStack.Redis.Generic;
public static IRedisClientsManager clientsManager=null;
public static IRedisClient redisClient=null;
try
{
#region 集群访问方式
var host = String.Format("{2}@{0}:{1}", this.txtHost.Text, this.txtPort.Text, this.txtPwd.Text);
host = String.Format("{0}:{1}", this.txtHost.Text, this.txtPort.Text);//v3.0格式
clientsManager = new PooledRedisClientManager(new string[] {host});
redisClient = clientsManager.GetClient();
#endregion
#region 单个实例方式
//redisClient = new RedisClient(this.txtHost.Text, Int32.Parse(this.txtPort.Text),this.txtPwd.Text);
#endregion
}
catch(Exception ex)
{
MessageBox.Show("失败:"+ex.Message);
return;
}
MessageBox.Show("成功");
2、对Redis操作
保存
redisClient.Set(this.txtStringKey.Text, this.txtStringValue.Text);
读取
var strValue=redisClient.Get<string>(this.txtStringKey.Text);
MessageBox.Show(strValue);
测试Add、Set、Replace
using (IRedisClient RClient = clientsManager.GetClient())
{
RClient.Add("c1", "缓存1");
RClient.Set("c1", "缓存2");
RClient.Replace("c1", "缓存3");
MessageBox.Show(RClient.Get<string>("c1"));
RClient.Remove("c1");
MessageBox.Show("移除状态:" + (RClient.Get<string>("c1") == null));
}
测试List
using (IRedisClient RClient = clientsManager.GetClient())
{
//内部维护一个List<T>集合
RClient.AddItemToList("蜀国", "刘备");
RClient.AddItemToList("蜀国", "关羽");
RClient.AddItemToList("蜀国", "张飞");
List<string> ListString = RClient.GetAllItemsFromList("蜀国");
foreach (string str in ListString)
{
MessageBox.Show(str); //输出 刘备 关羽 张飞
}
}
测试Hash
using (IRedisClient RClient = clientsManager.GetClient())
{
//Hash<T>
RClient.AddItemToSet("魏国", "曹操");
RClient.AddItemToSet("魏国", "曹操");
RClient.AddItemToSet("魏国", "典韦");
HashSet<string> HashSetString = RClient.GetAllItemsFromSet("魏国");
foreach (string str in HashSetString)
{
MessageBox.Show(str); //输出 典韦 曹操
}
}
3、Redis事务
Redis Transaction测试
using (IRedisClient RClient = clientsManager.GetClient())
{
RClient.Add("key", 1);
using (IRedisTransaction IRT = RClient.CreateTransaction())
{
IRT.QueueCommand(r => r.Set("key", 20));
IRT.QueueCommand(r => r.Increment("key", 1));
IRT.Commit(); // 提交事务
}
MessageBox.Show(RClient.Get<string>("key"));
}
4、Redis并发锁
using (IRedisClient RClient = clientsManager.GetClient())
{
RClient.Add("mykey", 1);
// 支持IRedisTypedClient和IRedisClient
using (RClient.AcquireLock("testlock"))
{
//Response.Write("申请并发锁<br/>");
var counter = RClient.Get<int>("mykey");
Thread.Sleep(100);
RClient.Set("mykey", counter + 1);
MessageBox.Show(RClient.Get<int>("mykey").ToString());
}
}
5、Redis队列
队列写入
redisClient.EnqueueItemOnList("operLog", "操作时间:" + DateTime.Now.ToString());
MessageBox.Show("写入成功");
队列读取
string msg = redisClient.DequeueItemFromList("operLog");
MessageBox.Show(msg);
更多关于redis操作方法请关注Redis官方网址 http://redis.io