使用NuGet安装ServiceStack.Redis,这是微软提供已经封装好的对redis操作类。包含4个dll
连接redis服务器,读取以及存储
public static ServiceStack.Redis.RedisClient client = new ServiceStack.Redis.RedisClient("127.0.0.1", 6379);
static void Main(string[] args)
{
AddString(client);
Console.ReadLine();
}
/// <summary>
/// 添加字符串
/// </summary>
/// <param name="client"></param>
private static void AddString(RedisClient client)
{
client.Add("address", "qihaohao");
Console.WriteLine(client.Get<string>("address"));
}
/// <summary>
/// 添加对象
/// </summary>
/// <param name="client"></param>
private static void AddPerson(RedisClient client)
{
var person = new Person() { Name = "qhh", Age = 26 };
client.Add("person", person);
var cachePerson = client.Get<Person>("person");
Console.WriteLine("name=" + cachePerson.Name + "----age=" + cachePerson.Age);
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
/// <summary>
/// 哈希结构数据
/// </summary>
/// <param name="client"></param>
private static void Addhash(RedisClient client)
{
client.SetEntryInHash("HashId", "Name", "QHH");
client.SetEntryInHash("HashId", "Age", "26");
var hash = client.GetHashValues("HashId");
foreach (var item in hash)
{
Console.WriteLine(item);
}
}
/// <summary>
/// 队列
/// </summary>
/// <param name="client"></param>
private static void AddQueue(RedisClient client)
{
client.EnqueueItemOnList("QueueListId", "1.qhh");
client.EnqueueItemOnList("QueueListId", "2.qihaohao");
client.EnqueueItemOnList("QueueListId", "3.qihh");
var queue = client.GetListCount("QueueListId");
for (int i = 0; i < queue; i++)
{
Console.WriteLine(client.DequeueItemFromList("QueueListId"));
}
}
/// <summary>
/// 栈
/// </summary>
/// <param name="client"></param>
private static void AddStack(RedisClient client)
{
client.PushItemToList("StackListId", "1.qhh"); //入栈
client.PushItemToList("StackListId", "2.qihaohao");
client.PushItemToList("StackListId", "3.qihh");
var stackCount = client.GetListCount("StackListId");
for (int i = 0; i < stackCount; i++)
{
Console.WriteLine(client.PopItemFromList("StackListId"));
}
}
/// <summary>
/// 集合
/// </summary>
/// <param name="client"></param>
private static void AddSet(RedisClient client)
{
client.AddItemToSet("Set1001", "qhh");
client.AddItemToSet("Set1001", "qihaohao");
client.AddItemToSet("Set1001", "qihh");
var set = client.GetAllItemsFromSet("Set1001");
foreach (var item in set)
{
Console.WriteLine(item);
}
}
/// <summary>
/// 集合
/// </summary>
/// <param name="client"></param>
private static void AddSortSet(RedisClient client)
{
client.AddItemToSortedSet("SetSorted1001", "1.qhh");
client.AddItemToSortedSet("SetSorted1001", "2.qihaohao");
client.AddItemToSortedSet("SetSorted1001", "3.qihh");
var sortset = client.GetAllItemsFromSortedSet("SetSorted1001");
foreach (var item in sortset)
{
Console.WriteLine(item);
}
var sortset1 = client.GetRangeFromSortedSet("SetSorted1001", 0, 0);
foreach (var item in sortset1)
{
Console.WriteLine(item);
}
var list = client.GetRangeFromSortedSetDesc("SetSorted1001", 0, 0);
foreach (var item in list)
{
Console.WriteLine(item);
}
}
/// <summary>
/// 返回两个集合的并集
/// </summary>
/// <param name="client"></param>
private static void Union(RedisClient client)
{
client.AddItemToSet("Set8001", "A");
client.AddItemToSet("Set8001", "B");
client.AddItemToSet("Set8001", "C");
client.AddItemToSet("Set8001", "D");
client.AddItemToSet("Set8002", "E");
client.AddItemToSet("Set8002", "F");
client.AddItemToSet("Set8002", "G");
client.AddItemToSet("Set8002", "D");
var setunion = client.GetUnionFromSets("Set8001", "Set8002");
foreach (var item in setunion)
{
Console.WriteLine(item);
}
}
/// <summary>
/// 返回两个集合的 交集
/// </summary>
/// <param name="client"></param>
private static void JJ(RedisClient client)
{
var hashG = client.GetIntersectFromSets(new string[] { "Set8001", "Set8002" });
foreach (var item in hashG)
{
Console.WriteLine(item);
}
}
/// <summary>
/// 返回两个集合的差集
/// </summary>
/// <param name="client"></param>
private static void CJ(RedisClient client)
{
var hashD = client.GetDifferencesFromSet("Set8001", new string[] { "Set8002" }); //[返回存在于第一个集合,但是不存在于其他集合的数据。差集]
foreach (var item in hashD)
{
Console.WriteLine(item);
}
}