• Facebook
  • Twitter
  • Youtube
  • LinedIn
  • RSS
  • Docs
  • Comparisons
  • Blogs
  • Download
  • Contact Us
Download
Show / Hide Table of Contents

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

  • .NET
  • Install the following NuGet packages in your client application:
    • Enterprise: EntityFrameworkCore.NCache
    • OpenSource: EntityFrameworkCore.NCache.OpenSource
  • Include the following namespaces in your application:
    • Alachisoft.NCache.EntityFrameworkCore
    • Alachisoft.NCache.Runtime.Caching
  • 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.
  • .NET
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.
  • .NET
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 GroupBy can be used with projection for LoadIntoCache as implemented below.
  • .NET
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.

  • .NET
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.

Contact Us

PHONE

+1 214-619-2601   (US)

+44 20 7993 8327   (UK)

 
EMAIL

sales@alachisoft.com

support@alachisoft.com

NCache
  • Edition Comparison
  • NCache Architecture
  • Benchmarks
Download
Pricing
Try Playground

Deployments
  • Cloud (SaaS & Software)
  • On-Premises
  • Kubernetes
  • Docker
Technical Use Cases
  • ASP.NET Sessions
  • ASP.NET Core Sessions
  • Pub/Sub Messaging
  • Real-Time ASP.NET SignalR
  • Internet of Things (IoT)
  • NoSQL Database
  • Stream Processing
  • Microservices
Resources
  • Magazine Articles
  • Third-Party Articles
  • Articles
  • Videos
  • Whitepapers
  • Shows
  • Talks
  • Blogs
  • Docs
Customer Case Studies
  • Testimonials
  • Customers
Support
  • Schedule a Demo
  • Forum (Google Groups)
  • Tips
Company
  • Leadership
  • Partners
  • News
  • Events
  • Careers
Contact Us

  • EnglishChinese (Simplified)FrenchGermanItalianJapaneseKoreanPortugueseSpanish

  • Contact Us
  •  
  • Sitemap
  •  
  • Terms of Use
  •  
  • Privacy Policy
© Copyright Alachisoft 2002 - . All rights reserved. NCache is a registered trademark of Diyatech Corp.
Back to top