Skip to content
Marc edited this page Aug 10, 2020 · 24 revisions

Welcome!

This is the MongoDB distributed Caching wiki. In this page:

Are you using .net core 3.0?

Then upgrade the package to the version 2.1.2 or higher.

Or upgrade the MongoDB driver to the version 2.8.1 or higher.

This version of the driver fixes an issue when using .net core 3.0 (Learn more) and the version 2.1.2 of the package upgrades the MongoDB driver dependency to the version 2.8.1.

Usage

  1. Create the TTL index in your database. Running the following command MongoDB will remove periodically all expired values.

db.<collection_name>.createIndex( { "EffectiveExpirationTimeUtc": 1 }, { "name": "CacheTTLIndex", expireAfterSeconds: 0 } )

For performance reasons, this driver does not remove the old documents by default. You should create a TTL index in order to clean the old values as is detailed above.

  • If you can't create the TTL index you can use another strategy to remove the old values in the database. Learn more.
  • Is strongly recommended to read the performance notes.

Then you can install the package running the command:

dotnet add package MarkCBB.Extensions.Caching.MongoDB --version 2.1.1

To use it as a session store in an ASP.NET CORE project:

You can clone this sample repo or follow the steps detailed in its readme.

Performance notes

First three points are the most important.

Ensuring index in queries

Is strongly recommended to create an index to these columns in this order: _id, SlidingTimeTicks, AbsoluteExpirationTimeUtc, EffectiveExpirationTimeUtc and Value. With this index all queries can run as a covered queries, learn more.

Example:

db.<collection_name>.createIndex( { "_id": 1, "SlidingTimeTicks": 1, "AbsoluteExpirationTimeUtc" : 1, "EffectiveExpirationTimeUtc": 1, "Value" : 1 }, {"name": "CacheIndex"})

TTL index, removing expired values

For performance reasons and to take the benefit of the built-in TTL index in mongoDB, by default this implementation does not check nor remove the expired values in every request to the database. To keep the collection clean is strongly recommended to use the TTL index as is detailed in the first step of the usage section.

But, as an alternative and if you don't have the possibility to create the TTL index, you can set the option DeleteOldValues to true. If this option is true, in each and every request to the database (including queries) a query to remove all expired values will be send to the database.

Memory tables

You could get high and predictable performance setting up mongoDB instances with In-Memory Storage Engine. Learn more how to set it up, possible configurations, benefits, limitations...

Absolute Expiration vs Sliding Expiration

Use absolute expiration whenever is possible. Absolute expiration has better performance than sliding expiration because no update is needed in any communication to the database.

Note that if the sliding expiration is used, the expiration time is updated every time including when the data is only read.

Write concern and journaling

Be careful setting these values if you use the MongoClientSettings to connect with MongoDB, this may cause hangs.

Enabling journaling you may find a poor performance.

Setting write concern to 0 means that no persistence confirmation will be expected. So the process will continue even with the data not available to be queried. This causes unexpected behaviors when using mongoDB caching to store web session data.

The default value is W1 and is the recommended value for a good balance between performance and data persistence. Learn more.

Keep in mind the different behavior of write concern and journaling when working with memory tables. Learn more

Options & settings

Name Description / Notes Required Default value
ConnectionString Connection string to the database. Not used if MongoClientSettings are provided. Required if no value is provided in option MongoClientSettings No default value.
DatabaseName database name used to store the values Not required "NetCoreCache"
CollectionName Name of the collection used to store the values Not required "CacheItems"
MaxRetries Max retries in case of cluster exception (for example during the replica set election process). The default value combined with the default millis to wait means two minutes with one retry every half second Not required 240
MillisToWait Millis to wait in every retry in case of retry. The default value combined with the MaxRetries to wait 240 default means two minutes with one retry every half second Not required 500
DeleteOldValues If true the process will send a query in every request to the database to delete all expired elements. Is strongly recommended to don't use this feature and use the built-in TTL index learn more Not required false
MongoClientSettings MongoClientSettings to establish a connection. If a value is provided in the property MongoClientSettings, the ConnectionString option is not used and all information regarding the connection must be provided in this object Required if ConnectionString is not provided null