Allow extensions to access the configured directus cache #23139
stuikomma
started this conversation in
Feature Requests
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Summary
Extensions may benefit from using a cache for various reasons (caching, synchronization across instances (e.g. locking mechanisms)). However, accessing the default directus cache is not straightforward and requires reimplementing the getCache function.
Ideally, a preconfigured cache could be provided to extensions.
Basic Example
Caching
Simple endpoint that uses the cache to cache responses of an upstream API, but limits access to users who are logged in.
Locking
Hook extensions that triggers a long-running action.
Motivation
Allowing caching in extensions using the interface that directus already provides. This makes the implementation for extension developers much easier, safer and incurs less overhead because we use existing libraries instead of bundling our own.
Detailed Design
Directus provides detailed configuration options for the cache. See https://docs.directus.io/self-hosted/config-options.html#cache.
The extensions cache could be configured the same way:
CACHE_<EXTENSION_NAME>_TTLCACHE_TTLCACHE_<EXTENSION_NAME>_NAMESPACE<EXTENSION_TYPE>_<EXTENSION_NAME>Namespacing the extensions cache will prevent a malicious/broken extension from tampering with directus' internal cache.
An implement for e.g. endpoint extensions could look like this:
https://github.com/directus/directus/blob/71236927d2fd3430bd546d8bc022d0d562aab2da/api/src/extensions/manager.ts#L874C1-L881C6
The cache should be provided for these extensions types:
Requirements List
Must Have:
Should Have:
Could Have:
Won't Have:
Drawbacks
--> There are no changes required in existing extensions.
Alternatives
Developers can already roll their own implementation of getCache. This allows for full customization, but a solution provided by directus would be easier and safer to use. E.g. an extension with a custom caching implementation might inadvertently write into the system cache, or, worse, allow users to read from the cache without proper access control.
Adoption Strategy
This is entirely optional to use for extension developers.
The documentation should be updated so extension developers can learn about the new option to get hold of the cache.
Unresolved Questions
No response
All reactions