Class QueryCacheExtensions
public static class QueryCacheExtensions
- Inheritance
-
QueryCacheExtensions
- Inherited Members
Methods
Cached<T>(IEntityQuery<T>, RequestOptions?)
Request caching for an entity query. Cached queries return results from the cache when available, implementing a stale-while-revalidate pattern: cached data is returned immediately, then refreshed in the background if stale.
public static CacheClient<T> Cached<T>(this IEntityQuery<T> query, RequestOptions? options = null) where T : IEntity<T>
Parameters
queryIEntityQuery<T>The entity query to cache.
optionsRequestOptionsOptional request options controlling cache behavior (priority, freshness requirements).
Returns
- CacheClient<T>
A CacheClient<T> supporting a subset of query operators.
Type Parameters
TThe entity type being queried.
Examples
Basic usage:
var result = await User.Query(ctx)
.Where(u => u.Active)
.Cached()
.ToListAsync();
if (result.State == ResultState.Stale)
{
Console.WriteLine("From cache, refreshing in background");
}
result.AddUpdateHandler(updatedUsers => RefreshUI(updatedUsers));
With Include():
var result = await Dog.Query(ctx)
.Include(d => d.BestFriend)
.Cached()
.ToListAsync();
Control cache behavior:
// Wait for fresh data (according to policy)
var fresh = await User.Query(ctx)
.Cached(new RequestOptions { Requirements = ResultState.Fresh })
.SingleOrDefaultAsync();
// Bypass cache completely, force reload
var uncached = await User.Query(ctx)
.Cached(new RequestOptions { Requirements = ResultState.Uncached })
.SingleOrDefaultAsync();
Remarks
The cache stores query results to avoid redundant database round-trips. When a cached query is executed, it returns a CacheResult<TValue, TUpdate> containing the data and a ResultState indicating freshness (Fresh, Stale, or Uncached).
The cache is automatically updated when entities are saved. Background refresh is triggered for stale data according to the configured ReadPolicy.