Entity Framework Second Level Cache with NCache

Entity Framework is a popular object-relational mapping framework for .NET by Microsoft. It enables you to work with relational data through domain objects. And, it eliminates the need for most of the database persistence code that you would otherwise need to write.

Entity Framework's popularity has resulted in it being used frequently in high transaction applications where performance and scalability are critical. But, in most high transaction cases the database quickly becomes a bottleneck. This is because unlike the application tier where you can add more application servers as you need to scale, you cannot do the same with the database tier.

The only way to achieve this scalability is with and distributed cache like NCache. NCache is an extremely fast and scalable distributed cache for .NET applications. It lets you cache application data, reduce those expensive database trips, and improve your application performance and scalability.

Although Entity Framework does not provide a Second Level (2nd Level) Cache provider model, NCache has implemented a custom ADO.NET provider for Entity Framework with which NCache can plug-in and cache results of Entity Framework queries. This allows you to start using distributed cache in your Entity Framework application without making any code changes.

View Documentation

Why Use NCache as Entity Framework Second Level Cache?

If your Entity Framework application is running in a multi-server environment then you need a distributed cache as an Entity Framework Second Level Cache. And, NCache is an ideal Entity Framework Second Level Cache for multi-server configurations and you should use it for these reasons:

  1. NCache is extremely fast: NCache is an extremely fast distributed cache and gives you sub-millisecond response times.
  2. NCache provides linear scalability: NCache never becomes a scalability bottleneck. It lets you add servers to the cache cluster to achieve linear scalability and handle extreme transaction loads.
  3. Cache size can grow infinitely: NCache provides cache partitioning and pools memory of all cache servers together for it. So, you can grow cache storage by simply adding more servers to the cache cluster.
  4. NCache replicates data intelligently: NCache lets you intelligently replicate the cache and without compromising performance. So, you won't lose any cache data even if a cache server goes down.
  5. NCache gives you 100% uptime: NCache has a self healing dynamic cache cluster with no single point of failure. As a result, NCache allows you to add or remove cache servers at runtime without stopping your application or the cache.

Use NCache as Entity Framework Second Level Cache without Programming

The best thing about using NCache with Entity Framework is that there is no programming required on your part. You simply modify your application's configuration files and specify NCache as your Second Level Cache provider. Here is an example of app.config changes:

	<interceptor type="Alachisoft.NCache.Integrations.EntityFramework.Caching.EFCommandInterceptor, 
	                   Alachisoft.Integrations.EntityFramework.CachingProvider" />

Use Analysis Mode and Caching Mode

Since NCache Entity Framework Second Level Cache provider plugs in as a custom ADO.NET provider, it allows you to run it in analysis mode where it logs all the different queries your application is issuing to Entity Framework. These queries are logged in a file along with a count of how many times this query was called during this analysis mode. Below is an example:

<!--Call-count = 21-->
 <query query-text="" ["SELECT FROM Products"] | 
        [stored-procedure-name = ""] 
        caching = "true"
        expiration-type="absolute" | 
        sliding = "" 
        dbsyncdependency = "true"/>

This allows you to determine which queries you want to cache and for how long. You can also specify database synchronization for some or all of your queries.

NCache Features for Entity Framework Second Level Cache

By plugging in NCache Entity Framework Second Level Cache provider through app.config changes, you are going to gain an enterprise level distributed cache for your application. Here are some features NCache provides for Entity Framework apps.

  1. Synchronize cache with database: Some data that is in the cache may be changed in the database without your application's knowledge or involvement. And, for this data, you can specify the corresponding classes for database synchronization feature of NCache.  This allows NCache to then connect with the database, monitor data changes, and then update the cache automatically to ensure that data in the cache is always synchronized with the database. NCache provides SqlDependency for SQL Server, OracleDependency for Oracle, and DbDependency for any OLEDB compliant databases.
  2. Absolute Expiration: Absolute expiration in NCache is specified separately for each cached item and is a date-time value when NCache automatically expires the item. For Entity Framework, NCache asks you to specify an "interval" value and then uses "Now() + interval" formulate to calculate the date-time value for absolute expiration.
  3. Sliding Expiration: Sliding expiration in NCache is specified separately for each cached item and is an interval value. NCache expires the cached item if it has neither been fetched nor updated for this interval. You can specify this interval through NCache Entity Framework Second Level Cache provider configuration file.
  4. Compact Serialization: Whenever any .NET object is cached in an out-of-process or distributed cache, it must first be serialized. And, regular .NET serialization is slow because it uses .NET Reflection at runtime. NCache provides a way for you to register your classes with NCache and NCache generates serialization code when your application connects to the cache. NCache then compiles this code in memory and uses it for serialization. This is almost 10 times faster than regular .NET serialization. And, you can use this feature with Entity Framework. Just register your Entity Framework Entity classes with NCache for Compact Serialization.

Using NCache Directly from Entity Framework Applications

Although, using NCache as Entity Framework Second Level Cache is very quick and easy, it only provides you a limited set of NCache features. The reason for this is that NCache is bound by the fact that it is plugged in as a custom ADO.NET provider and must deal at SQL query level. There are numerous NCache features that you are unable to use if you decide to use NCache as Entity Framework Second Level Cache Provider.

The alternative for you is to use NCache directly from within your Entity Framework application by making NCache API calls like all other non-Entity Framework .NET applications do. Although, there is a small programming effort to take this path, you might decide that the benefits outweigh the extra programming effort.

NCache Features Missed in Entity Framework Second Level Cache

Below is a list of features you would miss if you used it as Entity Framework Second Level Cache versus if you used NCache directly from your application.

  1. Bulk operations
  2. Cache Dependencies
    • Key based
    • File based
    • Custom
  3. Groups and subgroups
  4. Tags
  5. Named Tags
  6. Object Query Language (OQL) and LINQ based searches
  7. Lock and Unlock of cached items
  8. Cache item versioning
  9. Streaming API
  10. Read-through, Write-through, and Write-behind
  11. Event Notifications
    • Item based (update and remove)
    • General purpose (add, update, remove)
    • Custom
  12. Continuous Query
  13. Runtime Data Sharing
  14. Cache Loader

What to Do Next?

© Copyright Alachisoft 2002 - . All rights reserved. NCache is a registered trademark of Diyatech Corp.