mirror of
https://github.com/directus/directus.git
synced 2026-01-26 20:48:04 -05:00
* Move permissions extraction to accountability
* Fix permissions retrieval for public user
* Fetch user / role context in permissions middleware
* Remove unnecessary parseFilter
* Rename schemaCache to systemCache
* Add permissions caching
* Add system cache invalidation on permission changes
* Improve caching perf by reducing scope
* Add note to docs
* Clarify compatibility with conditional fields/filters
* Fix lint warning
* Allow nested vars in system-filter-input
* Add custom getter function that resolves arrays
* Add is-dynamic-variable util
* Export new util
* Cleanup parse filter
* Fix build
* Move debounce up to use-items
* Remove unused prop
* 🧹
* Fix input pattern usage w/ vars
* Remove debounce from search-input, increase throttle
74 lines
2.1 KiB
TypeScript
74 lines
2.1 KiB
TypeScript
import Keyv, { Options } from 'keyv';
|
|
import ms from 'ms';
|
|
import env from './env';
|
|
import logger from './logger';
|
|
import { getConfigFromEnv } from './utils/get-config-from-env';
|
|
import { validateEnv } from './utils/validate-env';
|
|
|
|
let cache: Keyv | null = null;
|
|
let systemCache: Keyv | null = null;
|
|
|
|
export function getCache(): { cache: Keyv | null; systemCache: Keyv } {
|
|
if (env.CACHE_ENABLED === true && cache === null) {
|
|
validateEnv(['CACHE_NAMESPACE', 'CACHE_TTL', 'CACHE_STORE']);
|
|
cache = getKeyvInstance(ms(env.CACHE_TTL as string));
|
|
cache.on('error', (err) => logger.warn(err, `[cache] ${err}`));
|
|
}
|
|
|
|
if (systemCache === null) {
|
|
systemCache = getKeyvInstance(undefined, '_system');
|
|
systemCache.on('error', (err) => logger.warn(err, `[cache] ${err}`));
|
|
}
|
|
|
|
return { cache, systemCache };
|
|
}
|
|
|
|
export async function flushCaches(): Promise<void> {
|
|
const { systemCache, cache } = getCache();
|
|
await systemCache?.clear();
|
|
await cache?.clear();
|
|
}
|
|
|
|
function getKeyvInstance(ttl: number | undefined, namespaceSuffix?: string): Keyv {
|
|
switch (env.CACHE_STORE) {
|
|
case 'redis':
|
|
return new Keyv(getConfig('redis', ttl, namespaceSuffix));
|
|
case 'memcache':
|
|
return new Keyv(getConfig('memcache', ttl, namespaceSuffix));
|
|
case 'memory':
|
|
default:
|
|
return new Keyv(getConfig('memory', ttl, namespaceSuffix));
|
|
}
|
|
}
|
|
|
|
function getConfig(
|
|
store: 'memory' | 'redis' | 'memcache' = 'memory',
|
|
ttl: number | undefined,
|
|
namespaceSuffix = ''
|
|
): Options<any> {
|
|
const config: Options<any> = {
|
|
namespace: `${env.CACHE_NAMESPACE}${namespaceSuffix}`,
|
|
ttl,
|
|
};
|
|
|
|
if (store === 'redis') {
|
|
const KeyvRedis = require('@keyv/redis');
|
|
|
|
config.store = new KeyvRedis(env.CACHE_REDIS || getConfigFromEnv('CACHE_REDIS_'), {
|
|
commandTimeout: 500,
|
|
});
|
|
}
|
|
|
|
if (store === 'memcache') {
|
|
const KeyvMemcache = require('keyv-memcache');
|
|
|
|
// keyv-memcache uses memjs which only accepts a comma separated string instead of an array,
|
|
// so we need to join array into a string when applicable. See #7986
|
|
const cacheMemcache = Array.isArray(env.CACHE_MEMCACHE) ? env.CACHE_MEMCACHE.join(',') : env.CACHE_MEMCACHE;
|
|
|
|
config.store = new KeyvMemcache(cacheMemcache);
|
|
}
|
|
|
|
return config;
|
|
}
|