简体   繁体   English

ASP.Net缓存共享

[英]ASP.Net Cache sharing

I have an ASP.Net 4.0 web application which very frequently loads data from the database and does heavy calculations on it. 我有一个ASP.Net 4.0 Web应用程序,它经常从数据库加载数据并对其进行繁重的计算。 I want to cache this loaded and prepared data in a central cache that can be accessed by every user and computer who uses the application. 我想将这些已加载和准备好的数据缓存在中央缓存中,每个使用该应用程序的用户和计算机都可以访问该缓存。

Simple use-case: 简单的用例:

  • User 1 accesses webpage, cache is empty, data is loaded/calculated, data is cached 用户1访问网页,缓存为空,数据被加载/计算,数据被缓存
  • User 2 accesses webpage, cache contains data, data loaded from cache 用户2访问网页,缓存包含数据,从缓存加载的数据
  • User 3 accesses webpage, cache contains data, data loaded from cache 用户3访问网页,缓存包含数据,从缓存加载的数据
  • User 1 reloads webpage, cache contains data, data loaded from cache 用户1重新加载网页,缓存包含数据,从缓存加载的数据
  • Cache expires 缓存过期
  • User 3 refreshes webpage, cache is empty, data is loaded/calculated, data is cached 用户3刷新网页,缓存为空,加载/计算数据,缓存数据

I know that ASP.Net has a built-in cache mechanism. 我知道ASP.Net有一个内置的缓存机制。 What I don't know is whether it can be shared between different users accessing the site on different computer at the same time. 我不知道的是它是否可以在同一时间访问不同计算机上的站点的不同用户之间共享。 I would also like to know how the system behaves in a web farm environment. 我还想知道系统在Web场环境中的行为方式。

While others have mentioned the built in ASP.NET Cache (System.Web.Caching), please note that .NET 4.0 introduces a whole new caching framework designed to work outside of the System.Web.Caching namespace: 虽然其他人已经提到了内置的ASP.NET缓存(System.Web.Caching),但请注意.NET 4.0引入了一个全新的缓存框架,旨在在System.Web.Caching命名空间之外工作:

System.Runtime.Caching System.Runtime.Caching

http://msdn.microsoft.com/en-us/library/system.runtime.caching(VS.100).aspx http://msdn.microsoft.com/en-us/library/system.runtime.caching(VS.100).aspx

Now, this is much more of a beast than the simple System.Web.Caching's Cache item. 现在,这比简单的System.Web.Caching的Cache项更像是野兽。 But, you get the advantage of having multiple cache stores, locking of cache objects/keys, and the extensibility points of creating your own external cache providers - such as one for Microsoft's new Velocity distributed caching system . 但是,您可以获得多个缓存存储,锁定缓存对象/密钥以及创建自己的外部缓存提供程序的可扩展性的优势 - 例如Microsoft的新Velocity分布式缓存系统 It comes with a MemoryCache provider built-in. 它内置了一个MemoryCache提供程序。

It's really not that difficult to use. 这真的不是那么难以使用。 Straight from MSDN's article on the built-in MemoryCache provider (again, you can implement your own) using it in a WinForms application (you'd change the code for your web app): 直接来自MSDN关于内置MemoryCache提供程序的文章(同样,您可以实现自己的)在WinForms应用程序中使用它(您将更改Web应用程序的代码):

http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.aspx http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.aspx

ObjectCache cache = MemoryCache.Default;
string fileContents = cache["filecontents"] as string;

if (fileContents == null)
{
    CacheItemPolicy policy = new CacheItemPolicy();

    List<string> filePaths = new List<string>();
    filePaths.Add("c:\\cache\\example.txt");

    policy.ChangeMonitors.Add(new 
    HostFileChangeMonitor(filePaths));

    // Fetch the file contents.
    fileContents = 
        File.ReadAllText("c:\\cache\\example.txt");

    cache.Set("filecontents", fileContents, policy);
}

Label1.Text = fileContents;

I've implemented NCache (mentioned above), as well as Memcached. 我已经实现了NCache(如上所述),以及Memcached。 I can tell you that Microsoft's Velocity is really the way to go for partitioning the data and setting up redundancy within the cache partitions itself (very cool). 我可以告诉你,微软的Velocity实际上是分区数据和在缓存分区内设置冗余的方式(非常酷)。 Not to mention, it's free! 更不用说,它是免费的!

As Eric wrote, you can use ASP.NET's built-in cache for what you what - it is shared between all sessions of your web app. 正如Eric所写,您可以使用ASP.NET的内置缓存来实现您的目标 - 它在您的Web应用程序的所有会话之间共享。

If you really want to (or need to) have a common cache for multiple machines (web farm), then you'd need some distributed caching solution, eg AppFabric , NCache or similar. 如果您真的想(或需要)为多台计算机(Web场)提供公共缓存,那么您需要一些分布式缓存解决方案,例如AppFabricNCache或类似的。

是的,内置的ASP.NET缓存在应用程序中的所有用户(线程)之间共享。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM