Data Access has been discontinued. Please refer to this page for more information.

Working with Cache

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.

In this topic:

Working with the Level One Cache

The OpenAccessContext exposes the Cache property that returns an ILevelOneCache object that is directly bound to the current context. The ILevelOneCache interfaces exposes two methods - Release and ReleaseAll that allow you to mark a specific entity or all entities as no longer needed by the context.

Working with the Level Two Cache

All OpenAccessContext instances from the same database can share a secondary cache. This cache is generally called "2nd level cache" or "L2 cache". The L2 cache holds copies of the database content of application objects. That means that the values for the various fields of the user object (like a Person instance) are duplicated in the L2 cache. The cache is populated during read access and gives fast retrieval of commonly used objects. The cache resides in the application process and the cache content is indexed by the object id. Additionally, the cache can contain complete query results. The query string and the parameters are used to index the results.

The 2nd level cache is designed to implement a multithreaded application, where, every single thread has its own context. In order to, avoid too many calls to the relational server, Telerik Data Access has a caching mechanism, the 2nd level cache, which is shared by all contexts in a single process. This is optimal if you implement a Web Application, which typically has a thread pooling and opens a new context for all incoming calls. Here, database access is necessary only when the data, which needs to be retrieved is not currently available in the cache, thereby increasing the efficiency and performance of your application.

The OpenAccessContext exposes the LevelTwoCache property that returns an ILevelTwoCache object representing the level two cache bound to all contexts using the same connection.

Enabling the 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 FluentModel
{
    static partial void CustomizeBackendConfiguration(ref BackendConfiguration config)
    {
        config.SecondLevelCache.Enabled = true;
    }
}
Partial Public Class FluentModel
    Shared Sub New()
        backend.SecondLevelCache.Enabled = True
    End Sub
End Class

Checking if an Object is Cached

The ILevelTwoCache.IsCached method allows you to check if an object is cached. Note that to use the IsCached method, you need to construct an ObjectKey. A sample code demonstrating the IsCached method is shown below:

using (FluentModel dbContext = new FluentModel())
{
   Category category  = dbContext.Categories.FirstOrDefault();
   Telerik.OpenAccess.ObjectKey objectKey = dbContext.CreateObjectKey(category);
   bool isInSecondLevelCache = dbContext.LevelTwoCache.IsCached(objectKey);
}

Using dbContext As New FluentModel()
 Dim _category As Category = dbContext.Categories.FirstOrDefault()
 Dim objectKey As Telerik.OpenAccess.ObjectKey = dbContext.CreateObjectKey(_category)
 Dim isInSecondLevelCache As Boolean = dbContext.LevelTwoCache.IsCached(objectKey)
End Using

Evicting Information for Persistent Objects From the 2nd Level Cache

The ILevelTwoCache object exposes two methods (Evict and EvictAll<T>) that allow you to evict information for instances of a certain type from the second level cache.