Pre-loading Reference Data with LoadIntoCache
The LoadIntoCache API fetches the result set data from the source and caches it without returning it to the application. This API allows subsequent FromCache calls to yield fresh data. The LoadIntoCache method is particularly suitable for frequently updated data like customer orders or sensitive data like payment details. In such scenarios, using stale data can result in incorrect business transactions, hence, a fresh copy of the data must be present in the cache at all times. This extension method loads your complete working set of data into the cache. Then, this method queries the database, stores the result in the cache, and returns it to the application.
Pre-loading data using LoadIntoCache is critical for reference data scenarios, as it prevents cold-start latency by loading the required working set into the cache before application requests are received.
Prerequisites
- Install the following NuGet packages in your client application:
- Enterprise: EntityFrameworkCore.NCache
- OpenSource: EntityFrameworkCore.NCache.OpenSource
- Include the following namespaces in your application:
- The cache must be running.
- Make sure that the data being added is serializable.
- For API details, refer to: CachingOptions, StoreAs, LoadIntoCache, LoadIntoCacheAsync.
Implementation Examples for LoadIntoCache
- The following example fetches the customer orders from the database and loads the result set data into the cache as a collection. It also returns the cache key generated internally which can be saved for future use.
using (var context = new NorthwindContext())
{
var options = new CachingOptions
{
StoreAs = StoreAs.Collection
};
var resultSet = (from custOrder in context.Orders
where custOrder.Customer.CustomerId == someCustomerId
select custOrder).LoadIntoCache(out string cacheKey, options);
}
- The following example loads the specific order details from the database into the cache as separate entities with caching options specified.
using (var context = new NorthwindContext())
{
var options = new CachingOptions
{
StoreAs = StoreAs.SeparateEntities
};
var resultSet = (from custOrder in context.Orders
where custOrder.Customer.CustomerId == someCustomerId
select custOrder).LoadIntoCache(options);
}
- The
GroupBycan be used with projection forLoadIntoCacheas implemented below.
using (var context = new NorthwindContext())
{
var resultSet = context.Products
.Where(p => p.UnitPrice == 10)
.GroupBy(p => p.ProductName)
.Select(group => group.Key)
.LoadIntoCache(out cacheKey, options)
.ToList();
}
You can execute this method at regular intervals whenever you expect that data to change. For instance, if you foresee data modifications within a week, running it again after that period ensures the data in the cache is updated.
Implementing Asynchronous LINQ APIs
The asynchronous APIs have the same functionality as their synchronous counterparts, but with asynchronous behavior. The parameters passed to these methods are also the same.
Important
Due to its asynchronous nature, cacheKey is not returned in any of the asynchronous calls like LoadIntoCacheAsync as out parameters are not allowed with methods with async signature.
LoadIntoCacheAsync
The following example loads specific order details from the database into the cache as separate entities with the caching options specified.
using (var context = new NorthwindContext())
{
var options = new CachingOptions
{
StoreAs = StoreAs.SeparateEntities
};
var task = (from custOrder in context.Orders
where custOrder.Customer.CustomerId == someCustomerId
select custOrder).LoadIntoCacheAsync(options);
task.Wait();
var resultSet = task.Result.ToList();
}
See Also
.NET: Alachisoft.NCache.EntityFrameworkCore namespace.
.NET: Alachisoft.NCache.Runtime.Caching namespace.