【问题标题】:How to access cache in ASP.NET 5 MVC?如何访问 ASP.NET 5 MVC 中的缓存?
【发布时间】:2015-07-24 03:14:47
【问题描述】:

我正在尝试了解更多关于 ASP.NET 5 和新的 .NET Core 的信息,并试图弄清楚是否有内置的内存缓存。

我发现了有关 Microsoft.Framework.Caching.Memory.MemoryCache 的信息。 但是,可用的文档很少。

任何帮助将不胜感激。

【问题讨论】:

    标签: asp.net-core


    【解决方案1】:

    有两个缓存接口,IMemoryCacheIDistributedCacheIDistrbutedCache 旨在用于存在共享缓存的云托管场景中,该缓存在应用程序的多个实例之间共享。否则使用IMemoryCache

    您可以通过调用以下方法将它们添加到您的启动中:

    private static void ConfigureCaching(IServiceCollection services)
    {
        // Adds a default in-memory implementation of IDistributedCache, which is very fast but 
        // the cache will not be shared between instances of the application. 
        // Also adds IMemoryCache.
        services.AddCaching();
    
        // Uncomment the following line to use the Redis implementation of      
        // IDistributedCache. This will override any previously registered IDistributedCache 
        // service. Redis is a very fast cache provider and the recommended distributed cache 
        // provider.
        // services.AddTransient<IDistributedCache, RedisCache>();
    
        // Uncomment the following line to use the Microsoft SQL Server implementation of 
        // IDistributedCache. Note that this would require setting up the session state database.
        // Redis is the preferred cache implementation but you can use SQL Server if you don't 
        // have an alternative.
        // services.AddSqlServerCache(o =>
        // {
        //     o.ConnectionString = 
        //       "Server=.;Database=ASPNET5SessionState;Trusted_Connection=True;";
        //     o.SchemaName = "dbo";
        //     o.TableName = "Sessions";
        // });
    }
    

    IDistributedCache 是大多数人想要用来充分利用缓存的一个,但它有一个非常原始的接口(你只能用它获取/保存字节数组)和很少的扩展方法。有关详细信息,请参阅this 问题。

    您现在可以将IDistributedCacheIMemoryCache 注入您的控制器或服务并正常使用它们。使用它们非常简单,毕竟它们有点像字典。这是IMemoryCache 的示例:

    public class MyService : IMyService
    {
        private readonly IDatabase database;
        private readonly IMemoryCache memoryCache;
    
        public MyService(IDatabase database, IMemoryCache memoryCache)
        {
            this.database = database;
            this.memoryCache = memoryCache;
        }
    
        public string GetCachedObject()
        {
            string cachedObject;
            if (!this.memoryCache.TryGetValue("Key", out cachedObject))
            {
                cachedObject = this.database.GetObject();
                this.memoryCache.Set(
                    "Key", 
                    cachedObject, 
                    new MemoryCacheEntryOptions()
                    {
                        SlidingExpiration = TimeSpan.FromHours(1)
                    });
            }
    
            return cachedObject;
        }
    }
    

    【讨论】:

    • 这很有帮助,但我对数据库缓存不是很感兴趣。对我来说是没用的。如果数据已经存储在 DB 中并且我要付出高昂的访问成本,那么将其缓存在 DB 中的意义何在!
    • 我同意,不要使用 SQL Server。使用内存缓存是最快的选择,但是如果您有多个站点实例怎么办?这就是 Redis 缓存的用武之地。Redis 速度非常快,仅用于这些目的。如果您使用IDistributedCache,那么如果您因为用户众多而需要另一个站点实例,这很容易做到,那么您的站点就会变得可扩展。
    • SQL Server 2016 现在还具有内存模式,因此速度更快,现在是可行的缓存选项。
    【解决方案2】:

    【讨论】:

    • 这些示例都是控制台应用程序。我认为这不是 @sam360 所要寻找的
    猜你喜欢
    • 2019-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-30
    • 1970-01-01
    • 2015-12-08
    • 2016-06-24
    • 2016-09-26
    相关资源
    最近更新 更多