EF Core Extension Methods : Query Deferred API
Note
This feature is available in NCache Enterprise and Professional editions.
In Entity Framework, immediate resolution methods like Count
and
FirstOrDefault
cannot be cached since their expected behavior is to cache the
count result or only the first item. NCache EF Core Extension Methods offer deferred queries to resolve this issue. The resolution is deferred instead of being immediate which caches the expected result instead of the entities themselves. This means that the deferred APIs have dedicated FromCache
and FromCacheOnly
APIs that cache the result.
Points to be noted about this API:
Query deferred results cannot be stored as separate entities as they are not entities themselves so
Cache.Insert
API will fail for them.FromCache
andLoadIntoCache
ignoreStoreAs
inCachingOptions
for query deferred APIs.FromCacheOnly
only supports Aggregate operators inQueryDeferred
.
Aggregate Operators
Aggregate operators are supported by both methods - FromCache
and
FromCacheOnly
.
Operator | Example |
---|---|
DeferredAverage |
database.Products.Select(o => o.UnitPrice).DeferredAverage().FromCache(options); |
DeferredCount |
database.Customers.Select(c => c.Country).GroupBy(c => c).DeferredCount().FromCacheOnly(); |
DeferredMin |
database.Orders.Where(o => o.CustomerId == "VINET").Select(o => o.RequiredDate).DeferredMin().FromCache(options); |
DeferredMax |
database.Orders.Select(o => o.RequiredDate).DeferredMax().FromCacheOnly(); |
DeferredSum |
database.OrderDetails.Select(o => o.UnitPrice).DeferredSum().FromCache(options); |
Element Operators
Element operators are only supported by FromCache()
.
Operator | Example |
---|---|
DeferredElementAtOrDefault |
database.Customers.DeferredElementAtOrDefault(c => c.City == "London").FromCache(options); |
DeferredFirst |
database.Customers.DeferredFirst(c => c.ContactTitle == "Sales Representative").FromCache(options); |
DeferredFirstOrDefault |
database.Customers.DeferredFirstOrDefault(c => c.ContactTitle == "Sales Representative").FromCache(options); |
DeferredLast |
database.Customers.DeferredLast(c => c.City == "London").FromCache(options); |
DeferredLastOrDefault |
database.Customers.DeferredLastOrDefault(c => c.City == "London").FromCache(options); |
DeferredSingle |
database.Customers.DeferredSingle(c => c.CustomerId == "ALFKI").FromCache(options); |
DeferredSingleOrDefault |
database.Customers.DeferredSingleOrDefault(c => c.CustomerId == "ANATR").FromCache(options); |
Miscellaneous LINQ Operators
The following operators are only supported by FromCache()
.
Operator | Example |
---|---|
DeferredAll |
database.Products.DeferredAll(expression).FromCache(options); |
DeferredLongCount |
database.Products.DeferredLongCount().FromCache(options); |
DeferredContains |
database.Products.DeferredContains(new Products { ProductId = 1 }); |
See Also
Caching Options for EF Core Caching
EF Core Caching Extension Methods
Cache Handle from EF Core Context
Logging Entity Framework Core Caching