Cache Optimizely Graph query results with the C# SDK
The Graph C# SDK caches query results in memory. Caching reduces the number of requests to Optimizely Graph for queries that run often, such as listing pages and search pages. Caching is on by default. You do not need to configure it.
This topic applies to Optimizely.Graph.Cms.Query 13.2.0 and later.
How the cache works
The following rules apply to every query that you run with IGraphContentClient:
- Duration – The SDK keeps a result for five minutes after the query runs.
- Separate results for each user – When a query runs as a user, the SDK keeps cached results separate for each user and the roles of that user. Anonymous visitors share one cached result. Each signed-in user has their own. An anonymous visitor and a signed-in editor never share a cached result. You can use caching with WithDisplayFilters(), AsCurrentUser(), and AsUser(). See Authentication requests in C# SDK.
- Storage – Each server keeps its own cache in memory.
- Clearing – When content changes in the CMS, the SDK clears the cached results on all servers.
- Supported methods – The cache applies to the following methods:
- GetAsync
- GetAsContentAsync
- GetSingleAsync
- GetSingleOrDefaultAsync
- Unsupported methods – The cache does not apply to the following methods:
- GetAsyncEnumerable
- GetAsContentAsyncEnumerable
Turn off the cache for a query
You can disable the cache for a single query to get fresh results from Optimizely Graph for that query.
To do this, add .WithoutCache() to a query. The SDK then sends the query to Optimizely Graph each time you run it:
var result = await _graphClient
.QueryContent<BlogPostPage>()
.Where(x => x.Author == "John Developer")
.WithDisplayFilters()
.WithoutCache()
.Limit(10)
.GetAsContentAsync();WithoutCache() affects only the query that you add it to. Other queries keep the default cache.
Change the cache settings for a query
Add .WithCacheOptions() to a query to change how the SDK caches its result. For instance, the following query keeps a result for one minute:
var result = await _graphClient
.QueryContent<BlogPostPage>()
.Where(x => x.Author == "John Developer")
.WithDisplayFilters()
.WithCacheOptions(options => options.AbsoluteExpiration = TimeSpan.FromMinutes(1))
.Limit(10)
.GetAsContentAsync();You can set the following options:
- AbsoluteExpiration – The time after which the SDK removes the result, no matter how often the query runs. The default is five minutes.
- SlidingExpiration – The time after which the SDK removes the result if no query reads it. Each read restarts the time.
- CacheKeySuffix – Text that the SDK adds to the cache key. Use it to keep separate cached results for the same query.
Set only one of AbsoluteExpiration and SlidingExpiration. If you set neither, the default of five minutes applies.
Common issues
The following table lists common problems with the query cache and their solutions.
Issue | Resolution |
|---|---|
A query does not return content that you published just now. | Optimizely Graph indexes published content after a short delay. A query that runs during this delay can cache the earlier result for up to five minutes. Add .WithoutCache() to the query, or set a shorter AbsoluteExpiration. |
A query throws an ArgumentException when it runs. | You set both AbsoluteExpiration and SlidingExpiration in WithCacheOptions(). Set only one of them. |