mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
Add optional cache max value size limit configuration (#13871)
Resolves #13708
This commit is contained in:
@@ -74,6 +74,7 @@ const allowedEnvironmentVars = [
|
||||
'CACHE_REDIS_PORT',
|
||||
'CACHE_REDIS_PASSWORD',
|
||||
'CACHE_MEMCACHE',
|
||||
'CACHE_VALUE_MAX_SIZE',
|
||||
// storage
|
||||
'STORAGE_LOCATIONS',
|
||||
'STORAGE_.+_DRIVER',
|
||||
@@ -206,6 +207,7 @@ const defaults: Record<string, any> = {
|
||||
CACHE_CONTROL_S_MAXAGE: '0',
|
||||
CACHE_SCHEMA: true,
|
||||
CACHE_PERMISSIONS: true,
|
||||
CACHE_VALUE_MAX_SIZE: false,
|
||||
|
||||
AUTH_PROVIDERS: '',
|
||||
AUTH_DISABLE_DEFAULT: false,
|
||||
|
||||
@@ -8,16 +8,27 @@ import { getCacheControlHeader } from '../utils/get-cache-headers';
|
||||
import logger from '../logger';
|
||||
import { ExportService } from '../services';
|
||||
import { getDateFormatted } from '../utils/get-date-formatted';
|
||||
import { stringByteSize } from '../utils/get-string-byte-size';
|
||||
import { parse as parseBytesConfiguration } from 'bytes';
|
||||
|
||||
export const respond: RequestHandler = asyncHandler(async (req, res) => {
|
||||
const { cache } = getCache();
|
||||
|
||||
let exceedsMaxSize = false;
|
||||
|
||||
if (env.CACHE_VALUE_MAX_SIZE !== false) {
|
||||
const valueSize = stringByteSize(JSON.stringify(res.locals.payload));
|
||||
const maxSize = parseBytesConfiguration(env.CACHE_VALUE_MAX_SIZE);
|
||||
exceedsMaxSize = valueSize > maxSize;
|
||||
}
|
||||
|
||||
if (
|
||||
req.method.toLowerCase() === 'get' &&
|
||||
env.CACHE_ENABLED === true &&
|
||||
cache &&
|
||||
!req.sanitizedQuery.export &&
|
||||
res.locals.cache !== false
|
||||
res.locals.cache !== false &&
|
||||
exceedsMaxSize === false
|
||||
) {
|
||||
const key = getCacheKey(req);
|
||||
|
||||
|
||||
6
api/src/utils/get-string-byte-size.ts
Normal file
6
api/src/utils/get-string-byte-size.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
/**
|
||||
* Returns the byte size for a given input string
|
||||
*/
|
||||
export function stringByteSize(string: string): number {
|
||||
return Buffer.byteLength(string, 'utf-8');
|
||||
}
|
||||
Reference in New Issue
Block a user