Disclaimer: This website requires Please enable JavaScript in your browser settings for the best experience.

HomeDev guideRecipesAPI Reference
Dev guideUser GuidesNuGetDev CommunityOptimizely AcademySubmit a ticketLog In
Dev guide

Cached templates

Use cached templates in Optimizely Graph to reduce query latency.

You can use cached templates to reduce request latency for your GraphQL queries. They speed up query execution, reducing latency and improving response times.

How cached templates work

In traditional GraphQL processing, the system translates queries into backend service requests, which can be slow and resource-intensive. Cached templates optimize this process by storing translated queries with variable placeholders. Instead of reprocessing the same query structure each time, the system substitutes new variable values into the cached template, reducing processing overhead and improving response times, even when variable values change.

Comparison of cached templates and cached queries

Both cached templates and cached queries improve the performance of frequently used queries but in different ways.

  • Cached templates – Use when you need flexibility in your queries, such as when the query parameters change frequently. By storing the query structure with placeholders, cached templates permit quick substitution of different variable values, offering efficiency in scenarios where query inputs change.
  • Cached queries – Use when you frequently execute queries with the same data and parameters. It ensures that the entire query, including its results, is readily available, reducing server load and improving execution speed.

Enable cached templates

To enable cached templates, include the following parameters in your Optimizely Graph request:

  1. Query string parameter – Add stored=true to your request URL.
    https://cg.optimizely.com/content/v2?auth=123456789&stored=true
    
  2. Request header – Include the following header:
    • Keycg-stored-query
    • Valuetemplate

The following code example shows a request using cached Templates:

-H "Content-Type: application/json" \
-H "cg-stored-query: template" \
-d '{
  "query": "query GetItem($id: ID!) { item(id: $id) { name, description } }",
  "variables": {
    "id": "12345"
  }
}'

📘

Note:

Cached templates are enabled by default for queries created in 2025 and later.

Limitations

Cached templates support the following variable types:

  • Boolean
  • Date
  • DateTime
  • String
  • Int
  • Float
  • Locales (variable name must be locale)
  • [Locales] (variable name must be locale)

📘

Note:

When the system detects a query with unsupported variable types, it automatically switches from using cached templates to cached queries to ensure the query can still be executed efficiently.