views:

53

answers:

2

Both Page.Cache and Page.Application can store an application's "global" data, shared among requests and threads.

How should one storage area be chosen over the other considering scenarios of data synchronization in the multi-threaded ASP.NET environment?

Looking for best practice and experienced recommendation.

A: 

You typically would store the data in Page.Application Items Collection when you need it within the same request. Page.Cache is typically used in data caching scenarios when you want to use it across multiple requests.

Keith Rousseau
+1  A: 

If the data

  • is stable during the life of the application
  • must always be available and must not be purged

better store it in HttpApplicationState.

If the data

  • not necessarily is needed for the life of the application
  • changes frequently
  • can be purged if needed (for example low system memory)
  • can be discarded if seldom used
  • should be invalidated/refreshed under some conditions (dependency rule: time span, date, file timestamp, ...)

then use Cache.

Other important points:

  • Large amounts of data may better be stored in Cache, the server then can purge it if low on memory.
  • Cache is safe for multithreaded operations. Page.Application needs locking.

See also this article on etutorials.org for more details.

marapet
The salient reason for me was the 'Other important points' section where you brought to my attention the implicit thread-safeness of Cache.
John K