Caching 修改实体后更新缓存

Caching 修改实体后更新缓存,caching,asp.net-core,browser-cache,memorycache,Caching,Asp.net Core,Browser Cache,Memorycache,我正在使用IMemoryCache并运行一个asp net核心项目。在主页上我列出了一些电影,它们被缓存了大约10分钟。如果电影已经创建/删除/编辑,如果这10分钟还没有过去,有没有办法更新缓存 using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Caching.Memory; using MovieManagement.Models; using MovieManagement.Models.Home; using MovieM

我正在使用IMemoryCache并运行一个asp net核心项目。在主页上我列出了一些电影,它们被缓存了大约10分钟。如果电影已经创建/删除/编辑,如果这10分钟还没有过去,有没有办法更新缓存

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using MovieManagement.Models;
using MovieManagement.Models.Home;
using MovieManagement.Services.Contracts;
using MovieManagement.ViewModels;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;

namespace MovieManagement.Controllers
{
    public class HomeController : Controller
    {
        private readonly IMovieService movieService;
        private readonly IMemoryCache cacheService;

        public HomeController(IMovieService movieService, IMemoryCache cache)
        {
            this.movieService = movieService ?? throw new ArgumentNullException(nameof(movieService));
            this.cacheService = cache ?? throw new ArgumentNullException(nameof(cache));
        }

        public async Task<IActionResult> Index()
        {
            var model = new HomeIndexViewModel();


            var cachedMovies = await this.cacheService.GetOrCreateAsync("Movies", async entry =>
            {
                entry.AbsoluteExpiration = DateTime.UtcNow.AddSeconds(20);
                var movies = await this.movieService.GetTopRatedMovies();
                return movies;
            });

            model.Movies = cachedMovies;

            return this.View(model);
        }
    }
}
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.Extensions.Caching.Memory;
使用电影管理模式;
使用MovieManagement.Models.Home;
使用MovieManagement.Services.Contracts;
使用MovieManagement.ViewModels;
使用制度;
使用System.Collections.Generic;
使用系统诊断;
使用System.Threading.Tasks;
名称空间MovieManagement.Controllers
{
公共类HomeController:控制器
{
私有只读IMovieService movieService;
专用只读IMemoryCache缓存服务;
公共家庭控制器(IMovieService movieService、iMomoryCache缓存)
{
this.movieService=movieService??抛出新参数nullexception(nameof(movieService));
this.cacheService=cache??抛出新的ArgumentNullException(nameof(cache));
}
公共异步任务索引()
{
var模型=新的HomeIndexViewModel();
var cachedMovies=wait this.cacheService.GetOrCreateAsync(“电影”,异步条目=>
{
entry.AbsoluteExpiration=DateTime.UtcNow.AddSeconds(20);
var movies=wait this.movieService.GetTopRatedMovies();
返回电影;
});
model.Movies=cachedMovies;
返回此.View(model);
}
}
}

您可以通过共享专用方法在删除/创建/编辑时更新缓存值:

    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Caching.Memory;
    using MovieManagement.Models;
    using MovieManagement.Models.Home;
    using MovieManagement.Services.Contracts;
    using MovieManagement.ViewModels;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Threading.Tasks;

    namespace MovieManagement.Controllers
    {
        public class HomeController : Controller
        {
            private readonly IMovieService movieService;
            private readonly IMemoryCache cacheService;

            public HomeController(IMovieService movieService, IMemoryCache cache)
            {
                this.movieService = movieService ?? throw new ArgumentNullException(nameof(movieService));
                this.cacheService = cache ?? throw new ArgumentNullException(nameof(cache));
            }

            public async Task<IActionResult> Index()
            {
                var model = new HomeIndexViewModel();


                var cachedMovies = await this.cacheService.GetOrCreateAsync("Movies", async entry =>
                {
                    entry.AbsoluteExpiration = DateTime.UtcNow.AddMinutes(10);
                    var movies = await this.movieService.GetTopRatedMovies();
                    return movies;
                });

                model.Movies = cachedMovies;

                return this.View(model);
            }


            [HttpPost]
            public async Task<IActionResult> Delete(int id)
            {

                this.movieService.Delete(id);

                UpdateCachedMovies();

                return RedirectToAction(nameof(Index));

            }

            [HttpPost]
            public async Task<IActionResult> Create(Movie model)
            {

                this.movieService.Add(model);

                UpdateCachedMovies();

                return RedirectToAction(nameof(Index));

            }

            private async void UpdateCachedMovies()
            {
                  this.cacheService.Set("Movies", this.movieService.GetTopRatedMovies(), DateTime.UtcNow.AddMinutes(10));

            }

        }
    }
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.Extensions.Caching.Memory;
使用电影管理模式;
使用MovieManagement.Models.Home;
使用MovieManagement.Services.Contracts;
使用MovieManagement.ViewModels;
使用制度;
使用System.Collections.Generic;
使用系统诊断;
使用System.Threading.Tasks;
名称空间MovieManagement.Controllers
{
公共类HomeController:控制器
{
私有只读IMovieService movieService;
专用只读IMemoryCache缓存服务;
公共家庭控制器(IMovieService movieService、iMomoryCache缓存)
{
this.movieService=movieService??抛出新参数nullexception(nameof(movieService));
this.cacheService=cache??抛出新的ArgumentNullException(nameof(cache));
}
公共异步任务索引()
{
var模型=新的HomeIndexViewModel();
var cachedMovies=wait this.cacheService.GetOrCreateAsync(“电影”,异步条目=>
{
entry.AbsoluteExpiration=DateTime.UtcNow.AddMinutes(10);
var movies=wait this.movieService.GetTopRatedMovies();
返回电影;
});
model.Movies=cachedMovies;
返回此.View(model);
}
[HttpPost]
公共异步任务删除(int-id)
{
this.movieService.Delete(id);
UpdateCachedMovies();
返回重定向到操作(名称(索引));
}
[HttpPost]
公共异步任务创建(电影模型)
{
this.movieService.Add(model);
UpdateCachedMovies();
返回重定向到操作(名称(索引));
}
私有异步void UpdateCachedMovies()
{
this.cacheService.Set(“Movies”,this.movieService.gettopatedmovies(),DateTime.UtcNow.AddMinutes(10));
}
}
}

您看过这个SO问题吗?问题:是的,看过了,但对我的情况没有帮助,谢谢。我在创建新电影后,在更新缓存时出现错误,我得到以下信息: