SecondLevelCache Configuration
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.
The SecondLevelCacheConfiguration class allows you to configure the second level cache functionality. Telerik Data Access has a caching mechanism, the 2nd Level Cache, which reduces the calls made to the relational server, by conserving data already loaded from the database. Database access is therefore, necessary only when the retrieving data is currently not available in the cache. This increases the efficiency and performance of your application. The 2nd Level Cache functionality can be configured in two ways:
- Configuring the 2nd Level Cache Functionality via Code
- Configuring the 2nd Level Cache Functionality via Config File
The SecondLevelCacheConfiguration object is accessible through the SecondLevelCache property that is exposed by the BackendConfiguration class.
public partial class EntitiesModel
{
static EntitiesModel()
{
BackendConfiguration.SecondLevelCacheConfiguration cacheConfig = backend.SecondLevelCache;
}
}
Partial Public Class EntitiesModel
Shared Sub New()
Dim cacheConfig As BackendConfiguration.SecondLevelCacheConfiguration = backend.SecondLevelCache
End Sub
End Class
The SecondLevelCacheConfiguration class exposes the following properties:
- Enabled - controls if the second level cache is enabled. Default value is False.
- NumberOfObjects - controls the maximum number of objects which can be contained in the 2nd level cache. Default value is 10000.
- CacheQueryResults - controls if the second level cache should also cache query results. Default value is False.
- NumberOfQueryResults - controls the maximum number of queries that can be contained in the 2nd level cache. Default value is 1000.
- NumberOfObjectsPerQueryResults - controls the maximum number of object in a cached query result. Default value is 500.
-
Synchronization - gets the SynchronizationConfiguration object that allows you to configure the 2nd level cache in distributed environments. The SynchronizationConfiguration class exposes the following properties:
- Enabled - controls if the distributed second level cache synchronization is enabled. Default value is False.
- Name - the cache cluster transport type used (e.g. MSMQ).
- MulticastAddress - controls the multicast address that will be used by MSMQ. It must be in the following format "IPAddress:Port" (e.g. "224.1.1.1:666"). For a list of reserved multicast addresses, please read here. It is recommended to use a unique setting per database.
- Localpath - controls the name of the receiving message queue. If nothing is specified, a default name is created by using the multicast address and the process id.
- App - controls the application value on the message for filtering purposes in external clients. All participants in a L2 cache cluster must use the same value. Default value is 0.
- ExpirationMSec - controls the number of milliseconds the message has time to be received. Default value is 2000.
- StatusDurationMSec - controls the number of milliseconds the status information is waited for. Default value is 2000.
- Tracing - controls if messages are send with the UseTracing property. It controls MSMQ tracing facilities. This can be used for debugging purposes. Default value is False.
- AdministrationQueue - controls the name of the administrative queue that is used.
- InitialMessageTimeoutMsec - controls the number of milliseconds to wait for the initial message. Default value is 2000.
Configuring the 2nd Level Cache Functionality via Code
To configure the 2nd level cache functionality programatically, 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 setup the Level 2 Cache. For example:
public partial class EntitiesModel
{
static partial void CustomizeBackendConfiguration(ref BackendConfiguration config)
{
config.SecondLevelCache.Enabled = true;
config.SecondLevelCache.NumberOfObjects = 15000;
config.SecondLevelCache.CacheQueryResults = true;
config.SecondLevelCache.NumberOfQueryResults = 2000;
config.SecondLevelCache.NumberOfObjectsPerQueryResults = 550;
config.SecondLevelCache.Synchronization.Enabled = true;
config.SecondLevelCache.Synchronization.Name = "MSMQ";
config.SecondLevelCache.Synchronization.MulticastAddress = "224.1.1.1:666";
config.SecondLevelCache.Synchronization.App = 1;
config.SecondLevelCache.Synchronization.ExpirationMSec = 3000;
config.SecondLevelCache.Synchronization.StatusDurationMSec = 4000;
config.SecondLevelCache.Synchronization.InitialMessageTimeoutMsec = 1000;
}
}
Partial Public Class EntitiesModel
Private Shared Sub CustomizeBackendConfiguration(ByRef config As BackendConfiguration)
config.SecondLevelCache.Enabled = True
config.SecondLevelCache.NumberOfObjects = 15000
config.SecondLevelCache.CacheQueryResults = True
config.SecondLevelCache.NumberOfQueryResults = 2000
config.SecondLevelCache.NumberOfObjectsPerQueryResults = 550
config.SecondLevelCache.Synchronization.Enabled = True
config.SecondLevelCache.Synchronization.Name = "MSMQ"
config.SecondLevelCache.Synchronization.MulticastAddress = "224.1.1.1:666"
config.SecondLevelCache.Synchronization.App = 1
config.SecondLevelCache.Synchronization.ExpirationMSec = 3000
config.SecondLevelCache.Synchronization.StatusDurationMSec = 4000
config.SecondLevelCache.Synchronization.InitialMessageTimeoutMsec = 1000
End Sub
End Class
Configuring the 2nd Level Cache Functionality via Config File
Another way to configure the 2nd level cache functionality is to define the configuration in the App/web config file. For example:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="openAccessConfiguration"
type="Telerik.OpenAccess.Config.OpenAccessConfigSectionHandler, Telerik.OpenAccess"
requirePermission="false" />
</configSections>
<openAccessConfiguration>
<backendConfiguration name="SecondLevelCacheConfiguration">
<secondLevelCache enabled="true"
numberOfObjects="1000"
cacheQueryResults="true"
numberOfQueryResults="2000"
numberOfObjectsPerQueryResults="550">
<synchronization enabled="true"
name="MSMQ"
multicastAddress="224.1.1.1:666"
app="1"
expirationMSec="3000"
statusDurationMSec="4000"
initialMessageTimeoutMsec="1000">
</synchronization>
</secondLevelCache>
</backendConfiguration>
</openAccessConfiguration>
<connectionStrings>
</connectionStrings>
</configuration>
Again, you need to extend your context class with another partial class with the same name and add the following static constructor:
public partial class EntitiesModel
{
static EntitiesModel()
{
BackendConfiguration.MergeBackendConfigurationFromConfigFile(
backend,
ConfigurationMergeMode.ConfigFileDefinitionWins,
"SecondLevelCacheConfiguration" );
}
}
Partial Public Class EntitiesModel
Shared Sub New()
BackendConfiguration.MergeBackendConfigurationFromConfigFile(backend,
ConfigurationMergeMode.ConfigFileDefinitionWins, "SecondLevelCacheConfiguration")
End Sub
End Class
This will merge the settings from the config file to the backend static variable of the context class that holds the rest of the backend configuration.