1、引入nuget包
搜索 servicestackredis 安装redis包
2、连接redis服务器,读取以及存储
public static ServiceStack.Redis.RedisClient client = new ServiceStack.Redis.RedisClient("127.0.0.1", 6379, "mimashi123."); // GET: Home public ActionResult Index() { //读取 string name = client.Get<string>("name"); string pwd = client.Get<string>("password"); //存储 client.Set<string>("name1", "admin1"); client.Set<string>("password1", "admin123"); List<Phone> phoneList = new List<Phone>(); Phone p = new Phone("小米5", "小米", "3500"); Phone p1 = new Phone("小米2A", "小米", "2500"); Phone p2 = new Phone("荣耀20Pro", "华为", "3500"); phoneList.Add(p); phoneList.Add(p1); phoneList.Add(p2); client.Set<List<Phone>>("phoneList", phoneList); List<Phone> pList = client.Get<List<Phone>>("phoneList"); return View(); }
实践思路:例如手机信息表,可以把整个表的数据以集合的形式存入redis 优化读取速度,当某个手机信息发生改变时,修改数据库信息,变更redis内的值。
参考地址:https://www.cnblogs.com/wangjifeng23/p/9105165.html