Using EF Core Extension Methods
NCache offers high-performance EF Core Extension Methods that help minimize database queries and increase application scalability by integrating a Distributed Cache directly into the LINQ data pipeline. The extension methods enable you to cache result sets, implement atomic operations, and control data expiration.
The following section explains NCache supported APIs for EF Core Caching. These include LINQ APIs, and cache-only APIs, and provide flexibility for specifying caching options.
NCache provides synchronous and asynchronous extension methods for caching queries in EF Core. The sync APIs are extension methods for the IQueryable interface, while the async APIs return a task instance for these methods. NCache allows for sub-millisecond response times for distributed .NET applications by alleviating database bottlenecks by providing both transactional cache functionality (FromCache) and reference data handling (LoadIntoCache).
NCache’s Entity Framework Core Caching Provider contains the following APIs for caching queries:
| Synchronous APIs | Asynchronous APIs |
|---|---|
FromCache |
FromCacheAsync |
LoadIntoCache |
LoadIntoCacheAsync |
FromCacheOnly |
FromCacheOnlyAsync |
The FromCache extension method is ideal to use when handling transactional data (with frequent read and write operations). For example, maintaining flight records, where each unique flight requires continuous database logging of new entries. Alternatively, for reference data (where reads are more frequent than writes), use LoadIntoCache and FromCacheOnly extension methods. Just like in a product catalog, where products are treated as reference data, as they are rarely modified and consistently read from the database. To use these reference data extension methods, you need to define the query indexes within your application. This can be done dynamically by adding the [QueryIndexable] tag.
Strategies for Storing EF Core Data in Distributed Cache
In FromCache, LoadIntoCache, FromCacheOnly, and their Async counterparts, you can use the EF Core caching queries to store data in the cache for subsequent queries.
You can store the data in the cache in two ways: store the entire data set as a collection against a single key in the data store using the Insert call. Or, you can add each entity separately against multiple keys in bulk. You can make this choice by using the CachingOption StoreAs set to StoreAs.Collection or StoreAs.SeparateEntities. The ability to store this information as SeparateEntities or as a Collection allows developers to balance atomic update granularity against bulk retrieval throughput.
You can specify and enable bulkInsertChunkSize when dealing with a considerable amount of entities, e.g., 100,000. This ensures that data is loaded into the cache chunk-by-chunk, regardless of the dataset's size, making the data storage process more efficient and manageable.
Note
The bulkInsertChunkSize property divides the bulk of entities into smaller chunks and updates the cache chunk-by-chunk. As such, it allows the operation to workaround the connection timeout by caching a chunk of entities within the 90-second limit, preventing it from triggering the cancellation token. By default, the bulkInsertChunkSize is 1000.
Important
Since the application only receives a response once the entire bulk is cached, it is best to use an Async extension method to avoid any significant delay when dealing with a large dataset.
In This Section
FromCache
Learn how to use FromCache in EF Core with NCache to cache query results, synchronize data, handle failures, and improve application performance.