HttpRuntime.Cache等效为asp.net 5,6 MVC [英] HttpRuntime.Cache Equivalent for asp.net 5, MVC 6

查看:412
本文介绍了HttpRuntime.Cache等效为asp.net 5,6 MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我刚刚从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 文件,并在相关性添加以下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 法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 通过构造器注入你的姑娘。该框架将解决一个具体的实施为你和它注入到类的构造函数

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 MVC的回购有供您参考更多的样品。

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆