see on GitHub

How to setup aggressive caching?

Standard cache configuration

The RavenDB client provides a caching mechanism out of the box. You can decide if Raven should cache a request or not for a particular URL. The default caching configuration is to cache all request:

documentStore.Conventions.ShouldCacheRequest = url => true;

The second cache option is the number of cached requests. The default value is 2048:

documentStore.MaxNumberOfCachedRequests = 2048;

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

Aggressive mode

The aggressive caching feature goes even further. Enabling it causes that the client can do not even ask the server and simply return the response directly from a local cache, without any usage of 304 Not Modified status. It means that a result will be returned very fast. The way it works is that the client subscribes to server notifications and by taking advantage of them is able to invalidate cached documents when they are changed. Hence 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.

Important

Despite the fact that the aggressive cache uses the notifications to invalidate the cache, it is still possible to get a 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");
}

Now, 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 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))) { }

Disabling the use of notifications

You can disable the usage of Changes API by the aggressive cache to track cached items and invalidate the cache when needed:

documentStore.Conventions.ShouldAggressiveCacheTrackChanges = false;

Note that it makes that it becomes more likely to get stale results.

Invalidate on SaveChanges

In addition to the use of notifications, the aggressive cache also revalidates the cache after calling session.SaveChanges(). This option is configurable by the following convention:

documentStore.Conventions.ShouldSaveChangesForceAggressiveCacheCheck = true;

Its default value is true. This will work nicely as long as you have just a single client. For multiple clients you should use ShouldAggressiveCacheTrackChanges.