Automatically Caching Data
Automatically caching data is useful when you do not want to rebuild the cache for each query. When you query data for the first time, the provider automatically initializes and builds a cache in the background. When AutoCache = true, the provider uses the cache for subsequent query executions, resulting in faster response times.
Configuring Automatic Caching
Caching the Customers Table
The following example caches the Customers table in the file specified by the CacheLocation property of the connection string.
C#
String connectionString = "Cache Location=C:\\cache.db;AutoCache=true;Server=127.0.0.1;Port=27017;Database=test;User=test;Password=test;"; using (MongoDBConnection connection = new MongoDBConnection(connectionString)) { MongoDBCommand cmd = new MongoDBCommand("SELECT City, CompanyName FROM [CData].[Sample].Customers WHERE Country = 'US'", connection); MongoDBDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { Console.WriteLine("Read and cached the row with _id " + rdr["_id"]); } }
VB.NET
Dim connectionString As [String] = "Cache Location=C:\\cache.db;AutoCache=true;Server=127.0.0.1;Port=27017;Database=test;User=test;Password=test;" Using connection As New MongoDBConnection(connectionString) Dim cmd As New MongoDBCommand("SELECT City, CompanyName FROM [CData].[Sample].Customers WHERE Country = 'US'", connection) Dim rdr As MongoDBDataReader = cmd.ExecuteReader() While rdr.Read() Console.WriteLine("Read and cached the row with _id " + rdr("_id")) End While End Using
Common Use Case
A common use for automatically caching data is to improve driver performance when making repeated requests to a live data source, such as building a report or creating a visualization. With auto caching enabled, repeated requests to the same data may be executed in a short period of time, but within an allowable tolerance (CacheTolerance) of what is considered "live" data.