根据修改的实体更新缓存

Update cache upon modified entity

我正在使用 IMemoryCache 和 运行 一个 asp-net 核心项目。在主页上,我列出了一些缓存了大约 10 分钟的电影。有没有办法更新缓存,如果电影已经 created/deleted/edit,如果那 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);
        }
    }
}

您可以通过共享私有方法更新 Delete/Create/Edit 上的缓存值:

    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));

            }

        }
    }