CacheLink class

A Link that provides caching capabilities for GraphQL requests.

CacheLink intercepts GraphQL requests and responses, storing them according to the configured CachePolicy. It integrates seamlessly into the gql_link chain and can be combined with other links like DedupeLink and HttpLink.

Example:

final cacheLink = CacheLink(
  store: InMemoryCacheStore(maxSize: 100),
  defaultPolicy: CachePolicy.cacheFirst,
  defaultTtl: Duration(minutes: 5),
);

final link = Link.from([
  DedupeLink(),
  cacheLink,
  HttpLink('https://api.example.com/graphql'),
]);

Constructors

CacheLink({CacheStore? store, CachePolicy defaultPolicy = CachePolicy.cacheFirst, Duration? defaultTtl})
Creates a cache link.

Properties

defaultPolicy CachePolicy
Default cache policy when not specified in request context.
final
defaultTtl Duration?
Default TTL for cached entries when not specified in request context.
final
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
store CacheStore
The cache storage backend.
final

Methods

clear() → void
Clears all cache entries.
concat(Link next) → Link
Adds next after this link
inherited
dispose() Future<void>
Can be called to clean up resources
inherited
evict(String key) → void
Evicts a specific cache entry.
generateCacheKey(Request request) String
Generates a deterministic cache key for a request.
getStats() Map<String, dynamic>
Returns cache statistics.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
read(String key) CacheEntry?
Reads a cache entry directly by key.
request(Request request, [NextLink? forward]) Stream<Response>
A function called when a request reaches this Link
route(LinkRouter route) → Link
Route requests after this link
inherited
split(bool test(Request request), Link left, [Link right = const PassthroughLink()]) → Link
Split requests after this link
inherited
toString() String
A string representation of this object.
inherited
write(String key, Map<String, dynamic> data, {Duration? ttl}) → void
Writes data to cache directly.

Operators

operator ==(Object other) bool
The equality operator.
inherited