🌟 Enterprise Feature 🌟 This feature is bundled with GraphQL-Enterprise.
GraphQL::Enterprise::ObjectCache
is an application-level cache for GraphQL-Ruby servers. It works by storing a cache fingerprint for each object in a query, then serving a cached response as long as those fingerprints don’t change. The cache can also be customized with TTLs.
ObjectCache
can greatly reduce GraphQL response times by serving cached responses when the underlying data for a query hasn’t changed.
Usually, a GraphQL query alternates between data fetching and calling application logic:
But with ObjectCache
, it checks the cache first, returning a cached response if possible:
This reduces latency for clients and reduces the load on your database and application server.
Before running a query, ObjectCache
creates a fingerprint for the query using GraphQL::Query#fingerprint
and Schema.context_fingerprint_for(ctx)
. Then, it checks the backend for a cached response which matches the fingerprint. If a match is found, the ObjectCache
fetches the objects previously visited by this query and compares their current fingerprints to the ones in the cache. If the fingerprints all match, then the cached response returned.
If there is no cached response or if the fingerprints don’t match, then the incoming query is re-evaluated. While it’s executed, ObjectCache
gathers the IDs and fingerprints of each object it encounters. When the query is done, the result and the new object fingerprints are written to the cache.
To get started with the object cache: