目录
String:字符串,最简单的key-value存储,像一些项目中常用的配置信息可以用string存储
Hash:包含键值对的无序散列表,一般key为ID或者其他标识,value为详情,如商品信息,个人信息等
List:因为list是有序的,所以适合存储一些有序且数据相对固定的数据。如省市区表,数据字典等
Set:无序集合,可以简单理解成ID-List模式,如皮皮虾个人有多少个好友,且set能对两个set提供交集、并集、差集操作,如两个人共同好友等
SortedSet:有序集合,增加了一个score参数,会自动根据score的值进行排序,比较适应于置顶数据等操作。
实例(以vs2019工具开发):
注意:完整项目在公众号中输入:“c#redis代码”即可获取
创建控制台应用程序
首先使用NuGet下载ServiceStack.Redis
添加引用:
using ServiceStack.Redis;
using System;
using System.Threading;
建立链接
static void Main(string[] args)
{
RedisClient client = new RedisClient("127.0.0.1", 6379);
client.FlushAll();// 命令用于清空整个 Redis 服务器的数据(删除所有数据库的所有 key )。
}
实例-String
测试string类型,设置过期时间
#region 【1】测试String类型-过期时间
//写入key-value,并设置过期时间
client.Add<string>("StringValueTime", "已设置过期时间,30秒后消失", DateTime.Now.AddMilliseconds(30000));
//测试
while (true)
{
//判断key是否存在
if (client.ContainsKey("StringValueTime"))
{
Console.WriteLine("String.键:StringValue,值:{0} {1}", client.Get<string>("StringValueTime"), DateTime.Now);
//线程停止十秒【这里小伙伴可以深入了解下sleep和wait的区别】
//1、sleep()属于Thread,wait()属于 Class;
//2、sleep()仅仅是睡眠,不涉及到锁的释放问题,睡眠时间结束自动恢复到运行状态。
//wait()绑定了某个对象的锁,等待该对象的notify(),notifyAll()来唤醒自己,等待的时间是未知的,甚至出现死锁
Thread.Sleep(10000);
}
else
{
Console.WriteLine("键:StringValue,值:已过期 {0}", DateTime.Now);
break;
}
}
#endregion
效果:
String类型,无过期时间
#region 【2】测试String类型-无过期时间
//写入string
client.Add<string>("StringValue", " 最简单的键值对");
Console.WriteLine("数据类型为:String.键:StringValue,值:{0}", client.Get<string>("StringValue"));
#endregion
效果:
String-写入实体类
Student stud = new Student() { id = "1001", name = "李四" };
client.Add<Student>("StringEntity", stud);//添加
Student Get_stud = client.Get<Student>("StringEntity");//读取
Console.WriteLine("数据类型为:String.键:StringEntity,值:{0} {1}", Get_stud.id, Get_stud.name);
public class Student
{
public string id { get; set; }
public string name { get; set; }
}
效果:
实例-Hash
public static void SetHash()
{
client.SetEntryInHash("HashID", "Name", "张三");
client.SetEntryInHash("HashID", "Age", "24");
client.SetEntryInHash("HashID", "Sex", "男");
client.SetEntryInHash("HashID", "Address", "上海市XX号XX室");
//根据hashid获取key
List<string> HaskKey = client.GetHashKeys("HashID");
foreach (string key in HaskKey)
{
Console.WriteLine("HashID--Key:{0}", key);
}
//根据hashid获取value
List<string> HaskValue = client.GetHashValues("HashID");
foreach (string value in HaskValue)
{
Console.WriteLine("HashID--Value:{0}", value);
}
//过去hashid
List<string> AllKey = client.GetAllKeys(); //获取所有的key。
foreach (string Key in AllKey)
{
Console.WriteLine("AllKey--Key:{0}", Key);
}
}
效果:
实例-List
public static void SetList()
{
/*
* list是一个链表结构,主要功能是push,pop,获取一个范围的所有的值等,操作中key理解为链表名字。
* Redis的list类型其实就是一个每个子元素都是string类型的双向链表。我们可以通过push,pop操作从链表的头部或者尾部添加删除元素,
* 这样list既可以作为栈,又可以作为队列。Redis list的实现为一个双向链表,即可以支持反向查找和遍历,更方便操作,不过带来了部分额外的内存开销,
* Redis内部的很多实现,包括发送缓冲队列等也都是用的这个数据结构
*/
client.EnqueueItemOnList("QueueListId", "1.张三"); //入队【先进先出】
client.EnqueueItemOnList("QueueListId", "2.张四");
client.EnqueueItemOnList("QueueListId", "3.王五");
client.EnqueueItemOnList("QueueListId", "4.王麻子");
int q = (int)client.GetListCount("QueueListId");
for (int i = 0; i < q; i++)
{
Console.WriteLine("QueueListId出队值:{0}", client.DequeueItemFromList("QueueListId")); //出队(队列先进先出)
}
client.PushItemToList("StackListId", "1.张三"); //入栈【后进先出】
client.PushItemToList("StackListId", "2.张四");
client.PushItemToList("StackListId", "3.王五");
client.PushItemToList("StackListId", "4.王麻子");
int p = (int)client.GetListCount("StackListId");
for (int i = 0; i < p; i++)
{
Console.WriteLine("StackListId出栈值:{0}", client.PopItemFromList("StackListId")); //出栈(栈先进后出)
}
}
效果:
实例-Set
public static void Set()
{ /*
它是string类型的无序集合。set是通过hash table实现的,添加,删除和查找,对集合我们可以取并集,交集,差集
*/
client.AddItemToSet("Set1001", "小A");
client.AddItemToSet("Set1001", "小B");
client.AddItemToSet("Set1001", "小C");
client.AddItemToSet("Set1001", "小D");
HashSet<string> hastsetA = client.GetAllItemsFromSet("Set1001");
foreach (string item in hastsetA)
{
Console.WriteLine("Set无序集合ValueA:{0}", item); //出来的结果是无序的
}
client.AddItemToSet("Set1002", "小K");
client.AddItemToSet("Set1002", "小C");
client.AddItemToSet("Set1002", "小A");
client.AddItemToSet("Set1002", "小J");
HashSet<string> hastsetB = client.GetAllItemsFromSet("Set1002");
foreach (string item in hastsetB)
{
Console.WriteLine("Set无序集合ValueB:{0}", item); //出来的结果是无序的
}
HashSet<string> hashUnion = client.GetUnionFromSets(new string[] { "Set1001", "Set1002" });
foreach (string item in hashUnion)
{
Console.WriteLine("求Set1001和Set1002的并集:{0}", item); //并集
}
HashSet<string> hashG = client.GetIntersectFromSets(new string[] { "Set1001", "Set1002" });
foreach (string item in hashG)
{
Console.WriteLine("求Set1001和Set1002的交集:{0}", item); //交集
}
HashSet<string> hashD = client.GetDifferencesFromSet("Set1001", new string[] { "Set1002" }); //[返回存在于第一个集合,但是不存在于其他集合的数据。差集]
foreach (string item in hashD)
{
Console.WriteLine("求Set1001和Set1002的差集:{0}", item); //差集
}
HashSet<string> hashF = client.GetDifferencesFromSet("Set1002", new string[] { "Set1001" }); //[返回存在于第二个集合,但是不存在于其他集合的数据。差集]
foreach (string item in hashF)
{
Console.WriteLine("求Set1002和Set1001的差集:{0}", item); //差集
}
}
效果:
实例-SortedSet
public static void SortedSet()
{
/*
sorted set 是set的一个升级版本,它在set的基础上增加了一个顺序的属性,这一属性在添加修改.元素的时候可以指定,
* 每次指定后,zset(表示有序集合)会自动重新按新的值调整顺序。可以理解为有列的表,一列存 value,一列存顺序。操作中key理解为zset的名字.
*/
client.AddItemToSortedSet("SortedSet1001", "4.宇仔");
client.AddItemToSortedSet("SortedSet1001", "1.刘仔");
client.AddItemToSortedSet("SortedSet1001", "3.猪仔");
client.AddItemToSortedSet("SortedSet1001", "2.星仔");
List<string> listSetSorted = client.GetAllItemsFromSortedSet("SortedSet1001");
foreach (string item in listSetSorted)
{
Console.WriteLine("SortedSet有序集合{0}", item);
}
}
效果:
这里推给大家redis及可视化工具下载安装教程:
安装包可在公众号回复“redis及可视化工具”即可获取
http://inkww.cn/NoteInfo/Priview?keyvalue=2e21e9d43ae04f8ba3
有兴趣的可以关注“墨水直达”,里面有许多免费的编程资料可以领取哦~