How to: Enable 2nd Level Cache
This topic provides examples of how to work with the second level cache programmatically, i.e. by using the Telerik Data Access API.
By default the 2nd level cache is disabled. To enable it you need to set to True the SecondLevelCache.Enabled property of the BackendConfiguration instance passed to the context constructor. For example:
public static BackendConfiguration GetBackendConfiguration()
{
BackendConfiguration backend = new BackendConfiguration();
backend.Backend = "MsSql";
backend.ProviderName = "System.Data.SqlClient";
backend.Logging.MetricStoreSnapshotInterval = 0;
backend.SecondLevelCache.Enabled = true;
backend.SecondLevelCache.CacheQueryResults = true;
CustomizeBackendConfiguration(ref backend);
return backend;
}
Public Shared Function GetBackendConfiguration() As BackendConfiguration
Dim backend As BackendConfiguration = New BackendConfiguration()
backend.Backend = "MsSql"
backend.ProviderName = "System.Data.SqlClient"
backend.SecondLevelCache.Enabled = True
CustomizeBackendConfiguration(backend)
Return backend
End Function
The configuration entries can be found as properties on the backend.SecondLevelCache object. You should set the desired configuration values by setting the appropriate properties on the SecondLevelCacheConfiguration object. The SecondLevelCache exposes the following properties:
- Enabled - controls if the second level cache is enabled.
- NumberOfObjects - controls the number of objects to be cached.
- NumberOfObjectsPerQueryResults - controls the maximum number of object in a cached query result. The 2nd level query cache does not cache query results with a higher amount of objects. The reason is, if it caches query results with million records, this uses a lot of memory and the result has to be read completely even if the application uses only the first 100 results. By default the 2nd level query cache will cache queries with maximum 500 objects.
- NumberOfQueryResults - controls the number of query results to cache.
-
Synchronization - gets the configuration for the synchronization of a distributed second level cache. The SynchronizationConfiguration object exposes the following properties. For more information, please refer to How to: Work with 2nd Level Cache in Distributed Environments.
- Enabled - controls if the distributed second level cache synchronization is enabled.
- AdministrationQueue - controls the name of the administrative queue used. Telerik Data Access supports multiple queue names that configure fallback queues. In environments where the second level cache of Telerik Data Access is used within IIS it is useful to be able to specify more than one queue for fallback scenarios. Overlapped recycling will be possible then. The queue names should be separated by semi-colon.
- ExpirationMSec - controls the number of milliseconds the message has time to be received.
- InitialMessageTimeoutMsec - controls the number of milliseconds to wait for the initial message.
- Localpath - controls the name of the receiving message queue. You can specify multiple values separated by semi-colon (';').
- MulticastAddress - controls the multicast address that is to be used by MSMQ.
- Tracing - controls if messages are send with the UseTracing property.