views:

41

answers:

2

I have a basic ASP.NET application which is used to request data which is stored on disk. This is loaded from files and sent as the response. I want to be able to store the data loaded from these files in memory to reduce the number of reads from disk.

All of the requests will be asking for the same data, so it makes sense to have a single cache of in-memory data which is accessible to all requests.

What is the best way to create a single accessible object instance which I can use to store and access this cached data? I've looked into HttpApplication, but apparently a new instance of this is created for parallel requests and so it doesn't fit my needs.

+1  A: 

First, you could cache the response. Look here: How to cache in ASP.NET by using Visual C# .NET

If you wish you could manually load files into memory and keep their content in static fields. Those will be shared between all clients of the same Application Domain.

Anyway, this will consume the server memory. Make sure you've got enough.

Developer Art
put them into session? how would that help then he would have a copy of the file in memory for every client(session)...
Petoj
Yep, you are right, my fault.
Developer Art
A: 

Use the this.Cache.Add / Get

This cache api is simple and works great in moste cases.

Petoj
Presumably this caches in memory? And there is one cache which all parallel requests can access? If so, thanks - that'll work just fine. :)
Barguast
Yes barguast this is true and aslong as you use the same key there will only be one instance loaded in memory!
Petoj