How to: Enable 2nd Level Cache (Programmatically)

This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.

This topic provides examples of how to work with the second level cache programmatically, i.e. by using the Telerik Data Access API.

How to Enable 2nd Level Cache

By default the 2nd level cache is disabled. To enable the 2nd level Cache, you need to extend your context class with another partial class with the same name and implement the CustomizeBackendConfiguration partial method. Using the config argument you can enable the 2nd level Cache. For example:

public partial class EntitiesModel
{
    static partial void CustomizeBackendConfiguration(ref  
        BackendConfiguration config)
    {
        config.SecondLevelCache.Enabled = true;
    }
}
Partial Public Class EntitiesModel
    Shared Sub New()
        backend.SecondLevelCache.Enabled = True
    End Sub
End Class

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.