Class QueryCache
Global LINQ query cache. Buckets queries by a shallow composite key, then verifies candidates inside each bucket via Compare(IDataContext, IQueryExpressions, out IQueryExpressions?).
public sealed class QueryCache
- Inheritance
-
objectQueryCache
- Extension Methods
Remarks
Lookup is O(1) at the bucket-map level. Inside each bucket, candidates are still verified with the original Compare(IDataContext, IQueryExpressions, out IQueryExpressions?) equality check.
Expiration:
- Each entry stores its own base timeout, captured from the context that created it.
- Each entry stores an explicit expiration deadline.
- Cache hits extend the deadline using the current hotness tier.
- Sweeps update hit-rate estimates, extend deadlines when an entry has earned a hotter tier, remove expired entries, and optionally trim the cache to a global capacity.
- Sweeps never shorten an already-earned deadline.
Capacity:
- Each bucket is capped by LinqToDB.Internal.Linq.QueryCache.BucketCap.
- Full buckets evict the entry with the earliest expiration deadline, then oldest access time, then lowest hit rate.
- The whole cache is also approximately capped by LinqToDB.Internal.Linq.QueryCache.DefaultMaxEntries, overridable through MaxEntriesOverride.
Threading:
- ConcurrentDictionary<TKey, TValue> owns the bucket map.
- Each bucket has its own lock for copy-on-write updates.
- LinqToDB.Internal.Linq.QueryCache.Bucket.Entries is published with volatile semantics.
- The global sweep is single-flighted and runs on the thread pool.
Fields
Default
Process-wide default cache used by Query<T>.
public static readonly QueryCache Default
Field Value
Properties
IdleTimeoutOverride
Per-instance override for the base idle timeout. When null, each new entry captures CacheSlidingExpirationOrDefault from the invoking IDataContext.
public TimeSpan? IdleTimeoutOverride { get; set; }
Property Value
MaxEntriesOverride
Per-instance override for the approximate global cache entry cap.
When null, the cache uses LinqToDB.Internal.Linq.QueryCache.DefaultMaxEntries.
Set to 0 to prevent new entries from being cached.
public int? MaxEntriesOverride { get; set; }
Property Value
- int?
SweepIntervalOverride
Per-instance override for the global-sweep interval. When null, the cache uses LinqToDB.Internal.Linq.QueryCache.DefaultSweepIntervalMs.
public TimeSpan? SweepIntervalOverride { get; set; }
Property Value
Methods
ClearAll()
Empties the entire cache.
public void ClearAll()
ClearForType(Type)
Empties the cache for entries of the given result type.
public void ClearForType(Type resultType)
Parameters
resultTypeType
GetMissCount(Type)
Counts queries that missed the cache, per result type, to mirror the legacy
Query<T>.CacheMissCount surface.
public long GetMissCount(Type resultType)
Parameters
resultTypeType
Returns
IncrementMissCount(Type)
public void IncrementMissCount(Type resultType)
Parameters
resultTypeType
TryAdd(Type, IDataContext, Query, IQueryExpressions, QueryFlags)
Adds query to the cache if no equivalent non-expired entry exists.
Expired entries in the destination bucket are removed before adding.
public void TryAdd(Type resultType, IDataContext dataContext, Query query, IQueryExpressions expressions, QueryFlags queryFlags)
Parameters
resultTypeTypedataContextIDataContextqueryQueryexpressionsIQueryExpressionsqueryFlagsQueryFlags
TryFind(Type, IDataContext, IQueryExpressions, QueryFlags, out Query?, out IQueryExpressions?)
Looks up a cached query that matches the supplied expressions
under dataContext.
public bool TryFind(Type resultType, IDataContext dataContext, IQueryExpressions expressions, QueryFlags queryFlags, out Query? query, out IQueryExpressions? matchedExpressions)
Parameters
resultTypeTypedataContextIDataContextexpressionsIQueryExpressionsqueryFlagsQueryFlagsqueryQuerymatchedExpressionsIQueryExpressions