.NET Core中的HttpContext和Caching> = 1.0

o-o*_*o-o 5 .net c# caching http asp.net-core

我试图将一些库从旧的基于MVC5 System.Web的堆栈移植到.Net Core.我遇到的一个问题是缓存的变化.例如,在MVC5中,我能够读取和写入i18n相关数据:

[代码片段1]

public static Dictionary<string, IEnumerable<CoreDictionaryResource>> DictionaryResourcesCache {
    get { return (Dictionary<string, IEnumerable<CoreDictionaryResource>>)HttpContext.Current.Cache(string.Concat(_Dictionary, DictionaryID, CultureID)); }
    set { HttpContext.Current.Cache(string.Concat(_Dictionary, DictionaryID, CultureID)) = value; }
}
Run Code Online (Sandbox Code Playgroud)

但是,我可靠地告知它System.Web并且它HttpContext不包含Cache字段.我可以看到一个Current字段,然后在这个范围内的一大堆字段Application,Session但是唉没有Cache.

我已经完成了必要的操作,Startup.cs并且应用程序配置为在内存缓存和会话中使用.我知道会话有效,因为我有其他POCO缓存使用

[代码片段2]

 return System.Web.HttpContext.Current.Session.GetObject<User>("AuthenticatedUser");
Run Code Online (Sandbox Code Playgroud)

GetObject我创建的扩展中的哪个位置.

我咆哮着试图用来HttpContext从缓存读出的错误的树,或者我需要使用IDistributedCache如此处, 这里SO.

但实际上我只是在[代码片段1]中移植方法......

您可以在新的.Net Core上提供有关缓存的任何指针都非常有用.

仅供参考我不想在控制器和视图中使用任何逻辑.我正在构建的应用程序使用单独的DLL进行数据访问和逻辑,因此请不要将任何带有DI的示例发布到控制器中.在遇到MVC堆栈之前,此问题更多地处于基础架构级别.

谢谢你们和gals.

Gle*_*lls 9

内存缓存功能仍然存在,它刚刚移动了一下.如果你添加

"Microsoft.Extensions.Caching.Memory": "1.1.0"
Run Code Online (Sandbox Code Playgroud)

你的project.json文件和添加

        services.AddMemoryCache();
Run Code Online (Sandbox Code Playgroud)

对于Startup.ConfigureServices方法,您将设置一个单例内存缓存实例,其工作方式与旧实例非常相似.您可以通过依赖注入来实现它,因此具有构造函数的控制器可以获取实例.

public class HomeController: Controller 
{
    private IMemoryCache _cache;
    public HomeController(IMemoryCache cache) 
    {
        _cache = cache;
    }

}
Run Code Online (Sandbox Code Playgroud)

然后,您可以在上面的类中使用_cache来获取全局可用的单例类.您可能还需要查看其他类型的缓存,包括用于进程外存储的Redis缓存.


Roh*_*ith 5

您应该只使用内存缓存,因为 HttpContext 缓存对象实际上是 appdomain 缓存对象,尽管它是使用 HttpContext 公开的

来自 msdn https://msdn.microsoft.com/en-us/library/system.web.httpcontext.cache(v=vs.110).aspx

每个应用程序域有一个 Cache 类的实例。因此,Cache 属性返回的 Cache 对象就是应用程序域中所有请求的 Cache 对象。

我们应该使用

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Caching.Memory;
using System;
using Microsoft.Extensions.FileProviders;

namespace CachingQuestion
{
public class Startup
{
    static string CACHE_KEY = "CacheKey";

    public void ConfigureServices(IServiceCollection services)
    {
        //enabling the in memory cache 
        services.AddMemoryCache();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        var fileProvider = new PhysicalFileProvider(env.ContentRootPath);

        app.Run(async context =>
        {
            //getting the cache object here
            var cache = context.RequestServices.GetService<IMemoryCache>();
            var greeting = cache.Get(CACHE_KEY) as string;


        });
    }
}

 public class Program
 {
    public static void Main(string[] args)
    {
          var host = new WebHostBuilder()
            .UseKestrel()
            .UseStartup<Startup>()
            .Build();

        host.Run();
    }
}
}
Run Code Online (Sandbox Code Playgroud)