<legend id='b6bTM'><style id='b6bTM'><dir id='b6bTM'><q id='b6bTM'></q></dir></style></legend>
  • <small id='b6bTM'></small><noframes id='b6bTM'>

    1. <tfoot id='b6bTM'></tfoot>
      <i id='b6bTM'><tr id='b6bTM'><dt id='b6bTM'><q id='b6bTM'><span id='b6bTM'><b id='b6bTM'><form id='b6bTM'><ins id='b6bTM'></ins><ul id='b6bTM'></ul><sub id='b6bTM'></sub></form><legend id='b6bTM'></legend><bdo id='b6bTM'><pre id='b6bTM'><center id='b6bTM'></center></pre></bdo></b><th id='b6bTM'></th></span></q></dt></tr></i><div id='b6bTM'><tfoot id='b6bTM'></tfoot><dl id='b6bTM'><fieldset id='b6bTM'></fieldset></dl></div>

        • <bdo id='b6bTM'></bdo><ul id='b6bTM'></ul>

        HttpRuntime.Cache 等效于 asp.net 5、MVC 6

        时间:2023-07-11

        <small id='qTqQT'></small><noframes id='qTqQT'>

      1. <legend id='qTqQT'><style id='qTqQT'><dir id='qTqQT'><q id='qTqQT'></q></dir></style></legend>
          <bdo id='qTqQT'></bdo><ul id='qTqQT'></ul>

              <tbody id='qTqQT'></tbody>

                  <tfoot id='qTqQT'></tfoot>
                  <i id='qTqQT'><tr id='qTqQT'><dt id='qTqQT'><q id='qTqQT'><span id='qTqQT'><b id='qTqQT'><form id='qTqQT'><ins id='qTqQT'></ins><ul id='qTqQT'></ul><sub id='qTqQT'></sub></form><legend id='qTqQT'></legend><bdo id='qTqQT'><pre id='qTqQT'><center id='qTqQT'></center></pre></bdo></b><th id='qTqQT'></th></span></q></dt></tr></i><div id='qTqQT'><tfoot id='qTqQT'></tfoot><dl id='qTqQT'><fieldset id='qTqQT'></fieldset></dl></div>
                  本文介绍了HttpRuntime.Cache 等效于 asp.net 5、MVC 6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  所以我刚刚从 ASP.Net 4 迁移到 ASP.Net 5.我目前正在尝试更改一个项目,以便它在新的 ASP.Net 中工作,但当然会有大量的错误.

                  So I've just moved from ASP.Net 4 to ASP.Net 5. Im at the moment trying to change a project so that it works in the new ASP.Net but of course there is going to be a load of errors.

                  有谁知道 HttpRuntime 的等效扩展是什么,因为我似乎无法在任何地方找到它.我用来缓存对象客户端.

                  Does anyone know what the equivalent extension is for HttpRuntime as I cant seem to find it anywhere. I'm using to cache an object client side.

                  HttpRuntime.Cache[Findqs.QuestionSetName] 
                  

                  'Findqs' 只是一个通用对象

                  'Findqs' is just a general object

                  推荐答案

                  您可以使用 IMemoryCache 实现来缓存数据.这有不同的实现,包括内存缓存、redis、sql 服务器缓存等.

                  You can an IMemoryCache implementation for caching data. There are different implementations of this including an in-memory cache, redis,sql server caching etc..

                  快速简单的实现是这样的

                  Quick and simple implemenation goes like this

                  更新您的 project.json 文件并在 dependencies 部分下添加以下 2 项.

                  Update your project.json file and add the below 2 items under dependencies section.

                  "Microsoft.Extensions.Caching.Abstractions": "1.0.0-rc1-final",
                  "Microsoft.Extensions.Caching.Memory": "1.0.0-rc1-final"
                  

                  保存此文件将执行 dnu 恢复,所需的程序集将添加到您的项目中.

                  Saving this file will do a dnu restore and the needed assemblies will be added to your project.

                  进入 Startup.cs 类,通过调用 ConfigureServices 方法中的 services.AddCaching() 扩展方法启用缓存.

                  Go to Startup.cs class, enable caching by calling the services.AddCaching() extension method in ConfigureServices method.

                  public void ConfigureServices(IServiceCollection services)
                  {
                      services.AddCaching();
                      services.AddMvc();
                  }
                  

                  现在您可以通过构造函数注入将 IMemoryCache 注入到您的 lass 中.该框架将为您解析一个具体的实现并将其注入到您的类构造函数中.

                  Now you can inject IMemoryCache to your lass via constructor injection. The framework will resolve a concrete implementation for you and inject it to your class constructor.

                  public class HomeController : Controller
                  {
                      IMemoryCache memoryCache;
                      public HomeController(IMemoryCache memoryCache)
                      {
                          this.memoryCache = memoryCache;
                      }
                      public IActionResult Index()
                      {   
                          var existingBadUsers = new List<int>();
                          var cacheKey = "BadUsers";
                          List<int> badUserIds = new List<int> { 5, 7, 8, 34 };
                          if(memoryCache.TryGetValue(cacheKey, out existingBadUsers))
                          {
                              var cachedUserIds = existingBadUsers;
                          }
                          else
                          {
                              memoryCache.Set(cacheKey, badUserIds);
                          }
                          return View();
                      }
                  } 
                  

                  理想情况下,您不想在控制器中混合缓存.您可以将其移动到另一个类/层以保持所有内容的可读性和可维护性.你仍然可以在那里进行构造函数注入.

                  Ideally you do not want to mix your caching within your controller. You may move it to another class/layer to keep everything readable and maintainable. You can still do the constructor injection there.

                  官方的asp.net mvcrepo有更多示例供大家参考.

                  The official asp.net mvc repo has more samples for your reference.

                  这篇关于HttpRuntime.Cache 等效于 asp.net 5、MVC 6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:未经授权时,ASP.Net core MVC6 Redirect to Login 下一篇:Asp.Net Core Web API 2.2 控制器不返回完整的 JSON

                  相关文章

                  <small id='AmJXS'></small><noframes id='AmJXS'>

                    <i id='AmJXS'><tr id='AmJXS'><dt id='AmJXS'><q id='AmJXS'><span id='AmJXS'><b id='AmJXS'><form id='AmJXS'><ins id='AmJXS'></ins><ul id='AmJXS'></ul><sub id='AmJXS'></sub></form><legend id='AmJXS'></legend><bdo id='AmJXS'><pre id='AmJXS'><center id='AmJXS'></center></pre></bdo></b><th id='AmJXS'></th></span></q></dt></tr></i><div id='AmJXS'><tfoot id='AmJXS'></tfoot><dl id='AmJXS'><fieldset id='AmJXS'></fieldset></dl></div>

                    1. <legend id='AmJXS'><style id='AmJXS'><dir id='AmJXS'><q id='AmJXS'></q></dir></style></legend>
                      • <bdo id='AmJXS'></bdo><ul id='AmJXS'></ul>

                    2. <tfoot id='AmJXS'></tfoot>