Add ability to set cache via query header.

This commit is contained in:
2023-11-14 09:52:51 +00:00
parent 93318df9fe
commit 9c9fa94140
2 changed files with 12 additions and 0 deletions
+2
View File
@@ -134,6 +134,8 @@ You can then start using the cache by setting the `ENABLE_GLOBAL_CACHE` environm
In the case of the `@cached` you can add additional parameters to the directive which will set the cache for specific queries to the provided time.
For example, `query MyCachedQuery @cached(ttl: 90) ....` will set the cache for the query to 90 seconds.
You can also set cache for specific query by using `X-Cache-Graphql-Query` header, which will set the cache for the query to the provided time, for example `X-Cache-Graphql-Query: 90` will set the cache for the query to 90 seconds.
Since version `0.5.30` the cache is gzipped in the memory, which should optimise the memory usage quite significantly.
### Security
+10
View File
@@ -2,6 +2,7 @@ package main
import (
"fmt"
"strconv"
"time"
fiber "github.com/gofiber/fiber/v2"
@@ -124,6 +125,15 @@ func processGraphQLRequest(c *fiber.Ctx) error {
cache_time = cfg.Cache.CacheTTL
}
if cache_time == 0 && !cacheFromQuery {
cacheQuery := c.Request().Header.Peek("X-Cache-Graphql-Query")
if cacheQuery != nil {
cache_time, _ = strconv.Atoi(string(cacheQuery))
cfg.Logger.Debug("Cache time set via header", map[string]interface{}{"cache_time": cache_time})
cacheFromQuery = true
}
}
wasCached := false
// Handling Cache Logic