我是靠谱客的博主 单身故事,最近开发中收集的这篇文章主要介绍.NET平台下Redis使用(七)【StackExchange.Redis测试Redis五种数据类型】信念之于人,犹翅膀之于鸟,信念是飞翔的翅膀Program.cs代码:RedisDemo.cs代码:RedisHelper.cs代码:UserInfoDto.cs代码:运行结果如图:,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
信念之于人,犹翅膀之于鸟,信念是飞翔的翅膀
Program.cs代码:
class Program
{
static void Main(string[] args)
{
//字符串
//RedisDemo.StringTest();
//Hash
//RedisDemo.HashTest();
//List
RedisDemo.ListTest();
Console.ReadKey();
//Set
//RedisDemo.SetTest();
//SortedSet
//RedisDemo.SortedSet();
}
}
RedisDemo.cs代码:
public class RedisDemo
{
public static void StringTest()
{
using (ConnectionMultiplexer conn = RedisHelper.RedisConn)
{
string key = "StringTest";
var db = conn.GetDatabase();
if (db.KeyExists(key))
db.KeyDelete(key);
db.StringAppend(key, DateTime.Now.ToString());
Console.WriteLine("写入字符串结束");
Console.ReadLine();
Console.WriteLine(db.StringGet(key));
Console.ReadLine();
}
}
public static void HashSetTest()
{
List<UserInfoDto> list = new List<UserInfoDto>();
for (int i = 0; i < 100; i++)
{
list.Add(new UserInfoDto()
{
Id = i,
LastLoginTime = DateTime.Now,
Password = "password" + i.ToString(),
StaffId = "StaffId_" + i.ToString(),
StaffName = "StaffName_" + i.ToString()
});
}
using (ConnectionMultiplexer conn = RedisHelper.RedisConn)
{
string key = "HashSetTest";
var db = conn.GetDatabase();
db.KeyDelete(key);
//string listKey = IdentityMap.CreateKey<UserInfoDto>();
HashEntry[] items = new HashEntry[list.Count];
for (int i = 0; i < list.Count - 1; i++)
{
string json = JsonConvert.SerializeObject(list[i]);
db.HashSet(key, list[i].Id, json);
//db.HashSet(key, "password", list[i].Password);
//db.HashSet(key, "StaffId", list[i].StaffId);
Console.WriteLine(db.HashGet(key, list[i].Id));
}
}
Console.ReadLine();
}
public static void SetTest()
{
List<UserInfoDto> list = new List<UserInfoDto>();
DateTime dt = DateTime.Now;
for (int i = 0; i < 10; i++)
{
list.Add(new UserInfoDto()
{
Id = i,
LastLoginTime = dt,
Password = "password" + i.ToString(),
StaffId = "StaffId_" + i.ToString(),
StaffName = "StaffName_" + i.ToString()
});
}
using (ConnectionMultiplexer conn = RedisHelper.RedisConn)
{
string key = "SetTest:";
var db = conn.GetDatabase();
db.KeyDelete(key);
//string listKey = IdentityMap.CreateKey<UserInfoDto>();
HashEntry[] items = new HashEntry[list.Count];
for (int i = 0; i < list.Count; i++)
{
string json = JsonConvert.SerializeObject(list[i]);
db.KeyDelete(key + list[i].Id.ToString());
//db.SetAdd(key + list[i].Id.ToString(), json);
db.SetAdd(key + list[i].Id.ToString() + ":name", list[i].StaffName);
db.SetAdd(key + list[i].Id.ToString() + ":StaffId", list[i].StaffId);
var result = db.SetScan(key, "*password99*").FirstOrDefault();
Console.WriteLine(result);
}
}
Console.WriteLine("Complete!");
Console.ReadLine();
}
public static void ListTest()
{
using (ConnectionMultiplexer conn = RedisHelper.RedisConn)
{
string key = "ListTest";
var db = conn.GetDatabase();
for (int i = 0; i < 10; i++)
{
db.ListLeftPush(key, "string_"+i.ToString());
}
Console.WriteLine("写入完成");
Console.ReadLine();
while (0 != db.ListLength(key))
{
//从redis数据库弹出List里的数据
var str = db.ListRightPop(key);
Console.WriteLine(str);
}
Console.ReadLine();
}
}
public static void SortedSet()
{
List<UserInfoDto> list = new List<UserInfoDto>();
for (int i = 0; i < 10; i++)
{
list.Add(new UserInfoDto()
{
Id = i,
LastLoginTime = DateTime.Now,
Password = "password" + i.ToString(),
StaffId = "StaffId_" + i.ToString(),
StaffName = "StaffName_" + i.ToString()
});
}
using (ConnectionMultiplexer conn = RedisHelper.RedisConn)
{
string key = "SortedSetTest:";
var db = conn.GetDatabase();
db.KeyDelete("SortedSetTest");
foreach (var item in list)
{
string json = JsonConvert.SerializeObject(item);
db.KeyDelete(key + item.Id.ToString());
db.KeyDelete("SortedSetTest" + item.Id.ToString() + ":name");
db.KeyDelete("SortedSetTest" + item.Id.ToString() + ":StaffId");
//db.SetAdd(key + list[i].Id.ToString(), json);
db.SortedSetAdd(key + item.Id.ToString() + ":name", item.StaffName, item.Id);
db.SortedSetAdd(key + item.Id.ToString() + ":StaffId", item.StaffName, item.Id);
}
Console.WriteLine("写入完成");
Console.ReadLine();
Console.WriteLine("读取两条记录");
var result = db.SortedSetRangeByRank(key, 9, 10);
for (int i = 0; i < result.Length; i++)
{
Console.WriteLine(result[i]);
}
var result2 = db.SortedSetRangeByRankWithScores(key, 0, -1, Order.Descending);
var result3 = db.SortedSetScan(key, "*99*", 10).ToList();
for (int i = 0; i < result3.Count; i++)
{
Console.WriteLine(result3[i]);
}
Console.ReadLine();
}
}
}
RedisHelper.cs代码:
public class RedisHelper
{
static ConfigurationOptions configurationOptions = ConfigurationOptions.Parse("127.0.0.1" + ":" + "6379");
static ConnectionMultiplexer redisConn;
public static ConnectionMultiplexer RedisConn
{
get
{
return ConnectionMultiplexer.Connect(configurationOptions);
}
}
}
UserInfoDto.cs代码:
public class UserInfoDto
{
public int Id { get; set; }
public string StaffId { get; set; }
public string StaffName { get; set; }
public string Password { get; set; }
public System.DateTime LastLoginTime { get; set; }
}
运行结果如图:
最后
以上就是单身故事为你收集整理的.NET平台下Redis使用(七)【StackExchange.Redis测试Redis五种数据类型】信念之于人,犹翅膀之于鸟,信念是飞翔的翅膀Program.cs代码:RedisDemo.cs代码:RedisHelper.cs代码:UserInfoDto.cs代码:运行结果如图:的全部内容,希望文章能够帮你解决.NET平台下Redis使用(七)【StackExchange.Redis测试Redis五种数据类型】信念之于人,犹翅膀之于鸟,信念是飞翔的翅膀Program.cs代码:RedisDemo.cs代码:RedisHelper.cs代码:UserInfoDto.cs代码:运行结果如图:所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复