1. 程式人生 > >.net core 快取技術 、記憶體快取 本人親測

.net core 快取技術 、記憶體快取 本人親測

第一步:在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

結束語:祝辛苦的我們每天開心快樂!!!