1.Install-Package Microsoft.Extensions.Caching.Redis -Version 2.2.0
2.注入
services.AddDistributedRedisCache(opt =>
{
opt.Configuration = Configuration.GetConnectionString("RedisServer");
//opt.InstanceName = "db0";
});
3.在Action中读写;
public ActionResult<string> Get(int id)
{
_distributedCache.SetString("key{id}", JsonConvert.SerializeObject(new object{}));
var obj = JsonConvert.DeserializeObject<object>(_distributedCache.GetStringAsync("key{id}").Result);
return Content(obj.ToString());
}
4.其他构造