see on GitHub

How to Setup Aggressive Caching

Standard Cache Configuration

The RavenDB client provides a caching mechanism out of the box. The default caching configuration is to cache all requests.

The default value of number of cached requests per database is 512 MB, But you can change it by changing MaxHttpCacheSize in the store Conventions.

documentStore.Conventions.MaxHttpCacheSize = new Size(1024, SizeUnit.Megabytes);

The client utilizes the notion of the 304 Not Modified server's response and will serve the data from the cache if available.

Disable caching

To disable the caching you can change the MaxHttpCacheSize value to zero:

documentStore.Conventions.MaxHttpCacheSize = new Size(0, SizeUnit.Megabytes);

In this scenario all the requests will be sent to the server to fetch the data.

Aggressive Mode

The aggressive caching feature goes even further. Enabling it means that the client doesn't need to ask the server. It will simply return the response directly from a local cache, without any usage of 304 Not Modified status. Result will be returned very fast. The way it works: The client subscribes to server notifications. By taking advantage of them, he is able to invalidate cached documents when they are changed. The client knows when it can serve the response from the cache, and when it has to send the request to get the up-to-date result.


Despite the fact that the aggressive cache uses the notifications to invalidate the cache, it is still possible to get stale data because of the time needed to receive the notification from the server.

To activate the aggressive caching mode, use the code:

using (session.Advanced.DocumentStore.AggressivelyCacheFor(TimeSpan.FromMinutes(5)))
	Order user = session.Load<Order>("orders/1");

If there is a value in the cache for orders/1 that is at most 5 minutes old and we haven't got any change notification about it, we can directly return it. The same mechanism works on queries as well:

using (session.Advanced.DocumentStore.AggressivelyCacheFor(TimeSpan.FromMinutes(5)))
	List<Order> users = session.Query<Order>().ToList();

The usage of the notification system means that you can set an aggressive cache duration to a longer period. The document store exposes the method:

using (session.Advanced.DocumentStore.AggressivelyCache()) { }

which is equivalent to:

using (session.Advanced.DocumentStore.AggressivelyCacheFor(TimeSpan.FromDays(1))) { }