第一步:在asp.net core项目中nuget引入Microsoft.Extensions.Caching.Memory
第二步:在项目里引入NetCoreCacheService.dll(本人封装)
第三步:调用
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using NetCoreChache.Models;
using NetCoreCacheService;
namespace NetCoreChache.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
//存入字符串
MemoryCacheService.SetChacheValue("sname", "刘琪琪");
//获取字符串缓存
ViewBag.sbname = MemoryCacheService.GetCacheValue("sname");
//存入泛型列表
List<TsetModel> list = new List<TsetModel>();
for (int i = 0; i < 100; i++)
{
TsetModel model = new TsetModel();
model.Id = i;
model.Name = "刘奇" + i;
list.Add(model);
}
MemoryCacheService.SetChacheValue("ulist", list);
//获取list缓存
var newList = MemoryCacheService.GetList<TsetModel>("ulist");
return View(newList);
}
public IActionResult About()
{
TsetModel m = new TsetModel();
m.Id = 100;
m.Name = "刘奇";
//存入model
MemoryCacheService.SetChacheValue("um", m);
//获取model
return View(MemoryCacheService.Get<TsetModel>("um"));
}
}
}
附件:百度网盘-链接:https://pan.baidu.com/s/1dtfB84_wUQgEG3mdCfQDXA 密码:8qws
结束语:祝辛苦的我们每天开心快乐!!!