Skip to content

Commit

Permalink
feat: allow disabling cache
Browse files Browse the repository at this point in the history
  • Loading branch information
caiorcferreira committed Sep 15, 2021
1 parent 82cb42e commit a5e38aa
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docs/restql/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ RestQL uses cache to avoid excessive database calls and grammar parsing. The cac

For fetching mappings from the database restQL uses a stale-cache strategy, which runs an update task in background when the TTL for an entry expire and only replace the cached value if the fetching is successful. This allows restQL to stay updated but not break if the database goes offline.

You can customize each cache maximum size and, for the mappings cache, other parameters.
You can customize each cache maximum size and, for the mappings cache, other parameters. You can also disable all caching using `cache.disable: true` or `RESTQL_CACHE_DISABLE=true`.

**Queries and Parser**

Expand Down
2 changes: 2 additions & 0 deletions internal/platform/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ type Config struct {
} `yaml:"logging"`

Cache struct {
Disable bool `yaml:"disable" env:"RESTQL_CACHE_DISABLE"`

Mappings struct {
MaxSize int `yaml:"maxSize" env:"RESTQL_CACHE_MAPPINGS_MAX_SIZE"`
Expiration time.Duration `yaml:"expiration" env:"RESTQL_CACHE_MAPPINGS_EXPIRATION"`
Expand Down
40 changes: 31 additions & 9 deletions internal/platform/web/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,10 @@ func API(log restql.Logger, cfg *conf.Config) (fasthttp.RequestHandler, error) {
})

mappingReader := persistence.NewMappingReader(log, cfg.Env, cfg.TenantMappings, db)
tenantCache := cache.New(log, cfg.Cache.Mappings.MaxSize,
cache.TenantCacheLoader(mappingReader),
cache.WithExpiration(cfg.Cache.Mappings.Expiration),
cache.WithRefreshInterval(cfg.Cache.Mappings.RefreshInterval),
cache.WithRefreshQueueLength(cfg.Cache.Mappings.RefreshQueueLength),
)
cacheMr := cache.NewMappingsReaderCache(log, tenantCache)
cacheMr := addMappingsReaderCache(log, cfg, mappingReader)

queryReader := persistence.NewQueryReader(log, cfg.Queries, db)
queryCache := cache.New(log, cfg.Cache.Query.MaxSize, cache.QueryCacheLoader(queryReader))
cacheQr := cache.NewQueryReaderCache(log, queryCache)
cacheQr := addQueryReaderCache(log, cfg, queryReader)

e := eval.NewEvaluator(log, cacheMr, cacheQr, r, parserCache, lifecycle)

Expand All @@ -83,6 +76,35 @@ func API(log restql.Logger, cfg *conf.Config) (fasthttp.RequestHandler, error) {
return app.RequestHandler(), nil
}

func addMappingsReaderCache(log restql.Logger, cfg *conf.Config, mappingReader persistence.MappingsReader) eval.MappingsReader {
if cfg.Cache.Disable {
return mappingReader
}

log.Info("mappings cache enabled")

tenantCache := cache.New(log, cfg.Cache.Mappings.MaxSize,
cache.TenantCacheLoader(mappingReader),
cache.WithExpiration(cfg.Cache.Mappings.Expiration),
cache.WithRefreshInterval(cfg.Cache.Mappings.RefreshInterval),
cache.WithRefreshQueueLength(cfg.Cache.Mappings.RefreshQueueLength),
)
cacheMr := cache.NewMappingsReaderCache(log, tenantCache)
return cacheMr
}

func addQueryReaderCache(log restql.Logger, cfg *conf.Config, queryReader persistence.QueryReader) eval.QueryReader {
if cfg.Cache.Disable {
return queryReader
}

log.Info("queries cache enabled")

queryCache := cache.New(log, cfg.Cache.Query.MaxSize, cache.QueryCacheLoader(queryReader))
cacheQr := cache.NewQueryReaderCache(log, queryCache)
return cacheQr
}

// registerAdminEndpoints adds handlers for administrative operations
func registerAdminEndpoints(adm *administrator, apiApp app) app {
apiApp.Handle(http.MethodGet, "/admin/tenant", adm.AllTenants)
Expand Down

0 comments on commit a5e38aa

Please sign in to comment.