Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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
  • User 2 accesses webpage, cache contains data, data loaded from cache
  • User 3 accesses webpage, cache contains data, data loaded from cache
  • User 1 reloads webpage, cache contains data, data loaded from cache
  • Cache expires
  • User 3 refreshes webpage, cache is empty, data is loaded/calculated, data is cached

I know that ASP.Net has a built-in cache mechanism. 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.

like image 236
Germstorm Avatar asked Jul 21 '10 15:07

Germstorm


People also ask

What is cache sharing?

Cache sharing allows each cache to share its contents with the other caches and avoid duplicate caching. It is common for a point of presence on the web to have more traffic than a single server can handle. One solution is to add more servers.

Where is ASP.NET cache stored?

The cached data is stored in server memory. Class Caching : Web pages or web services are compiled into a page class in the assembly, when run for the first time. Then the assembly is cached in the server.

Is .NET MemoryCache thread safe?

The AddOrGetExisting method from the . NET Framework is thread-safe (according to the documentation).

What is the difference between output cache and ASP.NET data cache?

ASP.NET supports caching of data as well web pages. Applications Data caching enables caching of data whereas Page Output Caching enables Web pages.


2 Answers

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:

System.Runtime.Caching

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. 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. It comes with a MemoryCache provider built-in.

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):

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. 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). Not to mention, it's free!

like image 66
eduncan911 Avatar answered Oct 03 '22 01:10

eduncan911


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.

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, e.g. AppFabric, NCache or similar.

like image 21
M4N Avatar answered Oct 03 '22 02:10

M4N