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

Querying Cached Entities with FromCacheOnly

For data that is less frequently updated like customer details, regularly fetching it from the data source is costly. FromCacheOnly queries the entities present in the cache and does not approach the data source in any case. Essentially, if the entity does not exist in the cache, it will still not be fetched from the data source.

Note

This API only works when entities are stored separately, i.e., the caching option StoreAs is set to SeparateEntities.

Important

The entities must be indexed in the NCache Management Center before they are used with FromCacheOnly.

  • The following example fetches the customer information from the cache if the Customer entity exists. If not, the result set returned will be empty.

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, FromCacheOnly, FromCacheOnlyAsync.
  • .NET
using (var context = new NorthwindContext())
{
    var resultSet = (from cust in context.Customers
                     where cust.CustomerId == someCustomerId
                     select cust).FromCacheOnly();
}

Considerations for FromCacheOnly

FromCacheOnly supports aggregate functions. However, these functions only cache the result and not the entities themselves. Hence, NCache provides the QueryDeferred method that defers the query so the entities in the cache get the result in response to a query. For more details on deferred APIs, please refer to the chapter Query Deferred APIs. FromCacheOnly supports the following functions:

  • Group By
  • Order By Ascending
  • Order By Descending
  • Sum
  • Min
  • Max
  • Average
  • Count
  • Contains
  • Like

Keep in mind the following considerations while using LINQ with FromCacheOnly:

  • Except for Count, all aggregate functions must have integer values. Count needs to be provided with an entity to count.

  • A single LINQ expression for FromCacheOnly cannot use more than one aggregate function.

  • Projections in LINQ expressions can only be performed if a GroupBy operator and an aggregate function are used for FromCacheOnly.

  • Multiple projections are not supported. Only one attribute can be projected in a LINQ expression (that is carried forward).

  • .NET
using (var context = new NorthwindContext())
{
    var result = context.Employees
        .Select(e => e.EmployeeId)  // Multiple projections have not been performed
        .GroupBy(eid => eid)        // eId was projected so GroupBy had to be used
        .DeferredMin()              // MIN provided with a numeric attribute
        .FromCacheOnly();
}
  • GroupBy is primarily used for aggregating functions (grouping the result of queries using columns) and can only be used if the projection is complete beforehand and an aggregate function is used. GroupBy has to be the last operator in the LINQ expression except if OrderBy is used. In such cases, the GroupBy function can be shifted forward (after OrderBy) or backward (before OrderBy).
  • .NET
using (var context = new NorthwindContext())
{
    var result = context.Products
        .Where(data => data.ProductName == "Tofu")
        .GroupBy(data => new { data.ProductName})
        .Select(group => new
        {
            _String = group.Key.ProductName,
            Count = group.Count()
        })
        .FromCacheOnly();
}
  • OrderBy (both ascending and descending) can only be used in a LINQ expression for FromCacheOnly if GroupBy is used.

  • More than one GroupBy and OrderBy operator cannot be used in a single LINQ expression for FromCacheOnly.

  • Joins are not supported so the Include() method will not work.

  • For LINQ expressions containing the DateTime instances, all nodes in a cache cluster must have the same DateTime format set up. Otherwise, it will result in a format exception. This problem arises when the DateTime.Now and DateTime.Parse() methods are used in a multi-node cache cluster. To avoid it, synchronize the DateTime format on both machines. Moreover, use DateTime.ParseExact() over DateTime.Parse(), as the ParseExact() API restricts the user from specifying a format for DateTime.

  • The Like operator in EF Core performs pattern matching within character strings. Usually, it is employed with Where in SELECT statements to filter rows based on specific patterns or substrings within a column's value. Additionally, it allows the use of wildcards to match one or more characters in a string:

  • .NET
using (var context = new NorthwindContext())
{
    var result = context.Customers
        .Where(c => EF.Functions.Like(c.CompanyName, "Alfreds Futterkiste"))
        .FromCacheOnly()
        .ToList();
}
  • The Contains operator can be used similarly to the Like operator to employ pattern-based searches within a given criteria.
  • .NET
using (var context = new NorthwindContext())
{
    var result = context.Customers
        .Where(b => (b.CompanyName.Contains("Alfreds Futterkist")))
        .FromCacheOnly()
}
  • The Contains operator can also be used to search through a dictionary.
  • .NET
using (var context = new NorthwindContext())
{
    var customerList = new List<string> { "CustomerId", "Alfreds Futterkiste" };

    var result = context.Customers
        .Where(b => customerList.Contains(b.CompanyName))
        .FromCacheOnly();
}

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.

FromCacheOnlyAsync

The following example fetches the customer information only from the cache if the Customer entity exists. If not, the result set returned will be empty, as the database is ignored.

  • .NET
using (var context = new NorthwindContext())
{
    var options = new CachingOptions
    {
        StoreAs = StoreAs.SeparateEntities
    };

    var task = (from cust in context.Customers
                where cust.CustomerId == someCustomerId
                select cust).FromCacheOnlyAsync();
    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