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

HttpRuntime.Cache Equivalent for asp.net 5, MVC 6(HttpRuntime.Cache 等效于 asp.net 5、MVC 6)

本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:HttpRuntime.Cache 等效于 asp.net 5、MVC 6

基础教程推荐