Add cache ttl support (#3)

* Add ability to use `@cached(ttl: 120)`

* Update documentation.
This commit is contained in:
2023-10-10 19:21:25 +01:00
committed by GitHub
parent bc128493b0
commit 917ee1a431
6 changed files with 30 additions and 10 deletions
+9 -4
View File
@@ -63,11 +63,16 @@ func processGraphQLRequest(c *fiber.Ctx) error {
}
}
opType, opName, cacheFromQuery, shouldBlock := parseGraphQLQuery(c)
opType, opName, cacheFromQuery, cache_time, shouldBlock := parseGraphQLQuery(c)
if shouldBlock {
return nil
}
if cache_time > 0 {
cfg.Logger.Debug("Cache time set via query", map[string]interface{}{"cache_time": cache_time})
cache_time = cfg.Cache.CacheTTL
}
wasCached := false
// Handling Cache Logic
@@ -81,7 +86,7 @@ func processGraphQLRequest(c *fiber.Ctx) error {
wasCached = true
} else {
cfg.Logger.Debug("Cache miss", map[string]interface{}{"hash": queryCacheHash, "user_id": extractedUserID})
proxyAndCacheTheRequest(c, queryCacheHash)
proxyAndCacheTheRequest(c, queryCacheHash, cache_time)
}
} else {
proxyTheRequest(c)
@@ -96,9 +101,9 @@ func processGraphQLRequest(c *fiber.Ctx) error {
}
// Additional helper function to avoid code repetition
func proxyAndCacheTheRequest(c *fiber.Ctx, queryCacheHash string) {
func proxyAndCacheTheRequest(c *fiber.Ctx, queryCacheHash string, cache_time int) {
proxyTheRequest(c)
cfg.Cache.CacheClient.Set(queryCacheHash, c.Response().Body(), time.Duration(cfg.Cache.CacheTTL)*time.Second)
cfg.Cache.CacheClient.Set(queryCacheHash, c.Response().Body(), time.Duration(cache_time)*time.Second)
c.Send(c.Response().Body())
}