C# API Redis缓存(控制器)
先在NuGet包里搜索Redis,找到图片中的安装包,点击安装
添加引用using ServiceStack.Redis;就可以使用RedisClient
//连接redis,输入本机和端口号
RedisClient client = new RedisClient(“127.0.0.1”,6379);
//redis缓存 存值
[Route("api/addredis")]
[HttpPost]
public IHttpActionResult addredis(productModel t)
{
//新建一个Model类接收前台传来的值
productModel model = new productModel();
model.Name = t.Name;
model.Code = t.Code;
model.Money = t.Money;
model.Time = DateTime.Now;
DateTime outtime = DateTime.Now.AddSeconds(30);
client.Add<productModel>("key",model,outtime);
return Ok(1);
}
//redis缓存 读取值
[Route("api/getredis")]
[HttpGet]
public IHttpActionResult getredis()
{
var slist = client.Get("key");
var b = Encoding.UTF8.GetString(slist);
var asas = JsonConvert.DeserializeObject<productModel>(b);
return Ok(asas);
}
如果有什么疑问,欢迎评论