ASP.NET Page.Cache versus Page.Application storage

2019-05-13 21:13发布

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.

2条回答
可以哭但决不认输i
2楼-- · 2019-05-13 21:14

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.

查看更多
贼婆χ
3楼-- · 2019-05-13 21:36

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.

查看更多
登录 后发表回答