【问题标题】:Update cache upon modified entity修改实体后更新缓存
【发布时间】:2019-05-02 23:47:13
【问题描述】:

我正在使用 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);
        }
    }
}

【问题讨论】:

标签: caching asp.net-core browser-cache memorycache


【解决方案1】:

您可以通过共享私有方法更新删除/创建/编辑上的缓存值:

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

            }

        }
    }

【讨论】:

  • 我收到一个错误,创建新电影后,更新缓存后,我收到以下信息:pastebin.com/…
猜你喜欢
  • 2014-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多