C#操作redis代码汇总
一 Redis是一种支持多种数据结构的键值对数据库
1.1Redis下载地址 :https://github.com/MicrosoftArchive/Redis
建议下载 .msi结尾的应用程序进行安装,会自动安装Redis服务
Redis默认是不能外网访问的
把Redis部署到本地请忽视下面
设置防火墙端口例外
更改redis.conf 文件
bind 127.0.0.1
protected-mode yes
更改为
# bind 127.0.0.1
protected-mode no
然后重启Redis服务,
1.2 Redis支持的数据类型:string、list、set、sortedset、geo(Redis 3.2以上版本),注意不同方法写入的值不能用混了,比如有写list的方法写入的值用获取字符串的方法去获取就有问题了。
1.3 Redis的优点:
支持多种复杂类型的数据结构
高命中的数据是运行在内存中的,数据最终还是可以保存到硬盘中,服务器重启后数据不会丢失
服务器是单线程的,来自所有客户端的所有命令都是串行执行的,不用担心并发修改的问题
支持消息订阅/通知机制,可以用作消息队列
key/value 最大长度允许512M
1.4 Redis的缺点:
Redis是单线程的,因此单个Redis的实例只能使用服务器的一个CPU核,不能充分发挥服务器的性能
二 在 .Net中操作Redis
2.1 在 .net中主要使用两个开源的组件来操作Redis
1. StackExChange.Redis:依赖的组件少,操作接近原生的Redis操作
2. ServiceStack.Redis:依赖的组件较多,封装的程度较高
NuGet命令安装组件 Install-Package StackExChange.Redis
using System;
using System.Collections.Generic;
using ServiceStack.Redis;
namespace SysBuild
{
class Program
{
//linux服务器地址
static private string host = "182.180.50.168";
//static private string host = "127.0.0.1";
static private int port = 6379;
static RedisClient redisClient = new RedisClient(host, port);//redis服务IP和端口
static void Main(string[] args)
{
//创建一个键a,指定值
redisClient.Set<string>("a", "1");
//获取键a对应的值
var a1 = redisClient.Get<string>("a");
//删除键a
redisClient.Remove("a");
//创建一个键a,指定值,并指定10s有效
redisClient.Set<string>("b", "1", new TimeSpan(0, 0, 60));
//队列操作之入队
redisClient.EnqueueItemOnList("list", "haha");
redisClient.EnqueueItemOnList("list", "haha111111111");
redisClient.EnqueueItemOnList("list", "haha22");
redisClient.EnqueueItemOnList("list", "hahset33333333333333333");
//读取队列深度
var a = redisClient.GetListCount("list");
//队列操作之出队
for (int i = 0; i < a; i++)
{
Console.WriteLine(redisClient.DequeueItemFromList("list"));
}
redisClient.SetEntryInHash("Hash", "Name", "wujf");
redisClient.SetEntryInHash("Hash", "Age", "99");
redisClient.SetEntryInHash("Hash", "Sex", "男");
redisClient.SetEntryInHash("Hash", "Address", "上海市XX号XX室");
//集合类指定超时的方法
redisClient.ExpireEntryIn("Hash", new TimeSpan(0, 0, 100));
var dic = redisClient.GetAllEntriesFromHash("Hash");
Console.WriteLine("Key-----Value");
foreach (var keyVal in dic)
{
Console.WriteLine(string.Format("{0}-----{1}", keyVal.Key, keyVal.Value));
}
List<string> haskKey = redisClient.GetHashKeys("Hash");
List<string> haskVal = redisClient.GetHashValues("Hash");
foreach (string key in haskKey)
{
Console.WriteLine("HashID--Key:{0}", key);
}
foreach (string val in haskVal)
{
Console.WriteLine("HashID--val:{0}", val);
}
//栈使用,先进后出
redisClient.PushItemToList("stack", "1");
redisClient.PushItemToList("stack", "2");
redisClient.PushItemToList("stack", "3");
redisClient.PushItemToList("stack", "4");
int count = redisClient.GetListCount("stack");
for (int i = 0; i < count; i++)
{
Console.WriteLine(redisClient.PopItemFromList("stack"));
}
//对Set类型进行操作
redisClient.AddItemToSet("set", "ddd");
redisClient.AddItemToSet("set", "ccc");
redisClient.AddItemToSet("set", "tttt");
redisClient.AddItemToSet("set", "sssh");
redisClient.AddItemToSet("set", "hhhh");
HashSet<string> hashset = redisClient.GetAllItemsFromSet("set");
foreach (string str in hashset)
{
Console.WriteLine(str);
}
//求并集
redisClient.AddItemToSet("set1", "aa");
redisClient.AddItemToSet("set1", "bb");
redisClient.AddItemToSet("set2", "bb");
redisClient.AddItemToSet("set2", "cc");
hashset = redisClient.GetUnionFromSets(new string[] { "set1", "set2" });
foreach (string str in hashset)
{
Console.WriteLine(str);
}
//求交集
hashset = redisClient.GetIntersectFromSets(new string[] { "set1", "set2" });
//求差集.
hashset = redisClient.GetDifferencesFromSet("set1", new string[] { "set2" });
//Sorted Set类型排序
redisClient.AddItemToSortedSet("sortList", "1");
redisClient.AddItemToSortedSet("sortList", "9");
redisClient.AddItemToSortedSet("sortList", "3");
redisClient.AddItemToSortedSet("sortList", "8");
List<string> sortList = redisClient.GetAllItemsFromSortedSet("sortList");
foreach (string str in sortList)
{
Console.WriteLine(str);
}
Console.ReadKey();
}
}
}
另:运行过程推荐使用Redis客户端看看数据变化(ps:我用的redisclient)
推荐api说明
http://www.cnblogs.com/kissdodog/p/3572084.html

浙公网安备 33010602011771号