c# Redis实战
一、Redis入门番外篇——快来使用一下Redis可视化工具(RedisDesktopManager)
二、安装Redis
三、Redis 教程
Redis总结(二)C#中如何使用redis
C# 使用 ServiceStack.Redis 自由切换db
四、实操
1、切换数据库
public static RedisClient client = new RedisClient(); static void Main(string[] args) { //切换到数据库 0 client.ChangeDb(0); client.Set<UserInfo>("test", new UserInfo() { Name = "ho", Age = 20 }); //切换到数据库 1 client.ChangeDb(1); client.Set<string>("test", "abc"); var v = client.Get<string>("test"); //切换到数据库 0 client.ChangeDb(0); var q = client.Get<string>("test"); //一起输出 Console.WriteLine(q); Console.WriteLine(v); Console.ReadLine(); }
2、读取
1)获取字符串格式
string str3 = RedisCacheHelper.Get<string>("zhong");
2)获取实体类
UserInfo userinfo = Redis.Get<UserInfo>("userinfo");
2)获取List实体类
List<UserInfo> userinfo = RedisHelper.Get<List<UserInfo>>("userinfo");
3、写入
1)写入字符串
RedisCacheHelper.Add("zhong", "zhongzhongzhong", DateTime.Now.AddDays(1));
2)写入实体类
UserInfo数据实体定义:
public class UserInfo { public long Id { set; get; } public string UserName { get; set; } public int Age { get; set; }} Redis.Set<UserInfo>("userinfo", new UserInfo() { UserName = "李四", Age = 45 });
2)写入List实体类
List<UserInfo> user = new List<UserInfo>(); UserInfo user1 = new UserInfo(); user1.Age = 11; user1.UserName = "22"; user.Add(user1); UserInfo user2 = new UserInfo(); user2.Age = 11; user2.UserName = "33"; user.Add(user2); RedisHelper.Add<List<UserInfo>>("userinfo", user, sDate);