Skip to content

Commit 44a83ec

Browse files
Neo1975dfornaciari
andauthored
Added a configurable handle of the cache to enable/disable by environ… (#441)
* Added a configurable handle of the cache to enable/disable by environment variable ENABLE_CACHE * fixing lint --------- Co-authored-by: Daniele Fornaciari <[email protected]>
1 parent f8c376f commit 44a83ec

11 files changed

+35
-6
lines changed

src/config.ts

+2
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ const parseConfig = (config: EnvironmentVariables & Record<string, string>): Run
126126
ACL_CONTEXT_BUILDER_PATH = defaults.ACL_CONTEXT_BUILDER_PATH,
127127
LANGUAGES_DIRECTORY_PATH = defaults.LANGUAGES_DIRECTORY_PATH,
128128
SERVICE_CONFIG_PATH = defaults.SERVICE_CONFIG_PATH,
129+
ENABLE_CACHE = defaults.ENABLE_CACHE,
129130
} = config
130131
let serviceConfig: unknown = defaults.PUBLIC_HEADERS_MAP
131132

@@ -157,6 +158,7 @@ const parseConfig = (config: EnvironmentVariables & Record<string, string>): Run
157158
ACL_CONTEXT_BUILDER: getAclContextBuilder(ACL_CONTEXT_BUILDER_PATH),
158159
ACL_CONTEXT_BUILDER_PATH,
159160
CONTENT_TYPE_MAP: validateContentTypeMap(contentTypeMap),
161+
ENABLE_CACHE,
160162
LANGUAGES_CONFIG: validateLanguages(LANGUAGES_DIRECTORY_PATH),
161163
LANGUAGES_DIRECTORY_PATH,
162164
PUBLIC_DIRECTORY_PATH: config.PUBLIC_DIRECTORY_PATH ?? defaults.PUBLIC_DIRECTORY_PATH,

src/defaults.ts

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const CONTENT_TYPE_MAP: Record<Extension | string, string> = {
2222
'.yml': 'text/yaml',
2323
}
2424

25+
const ENABLE_CACHE = 'true'
26+
2527
export {
2628
ACL_CONTEXT_BUILDER_PATH,
2729
CONTENT_TYPE_MAP,
@@ -30,4 +32,5 @@ export {
3032
PUBLIC_DIRECTORY_PATH,
3133
PUBLIC_HEADERS_MAP,
3234
LANGUAGES_DIRECTORY_PATH,
35+
ENABLE_CACHE,
3336
}

src/lib/configurations.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,15 @@ const shouldManipulate = (extension: ExtensionOutput): extension is Extension =>
8989
['.json', '.yaml', '.yml'].includes(extension)
9090

9191
async function configurationsHandler(request: FastifyRequest, filename: string, config: RuntimeConfig): Promise<ConfigurationResponse> {
92-
const bufferPromise = fsCache.get(filename) ?? fileLoader(filename)
93-
fsCache.set(filename, bufferPromise)
92+
let bufferPromise: Promise<Buffer>
93+
94+
if (config.ENABLE_CACHE === 'true') {
95+
bufferPromise = fsCache.get(filename) ?? fileLoader(filename)
96+
fsCache.set(filename, bufferPromise)
97+
} else {
98+
bufferPromise = fileLoader(filename)
99+
}
100+
94101
const buffer = await bufferPromise
95102

96103
const fileExtension = path.extname(filename) as ExtensionOutput

src/lib/onSendHandler.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ const staticFileHandler = (context: FastifyContext) => async (
8585
language && reply.header('content-language', language)
8686
buffer = fileBuffer
8787
} else if (isPublic(url)) {
88-
const fileBuffer = await publicHandler(filename, injectNonce)
88+
const fileBuffer = await publicHandler(filename, injectNonce, config)
8989
// eslint-disable-next-line @typescript-eslint/no-floating-promises
9090
reply.header('content-length', fileBuffer.length)
9191
buffer = fileBuffer

src/lib/public.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
import { readFile } from 'fs/promises'
22

3+
import type { RuntimeConfig } from '../config'
4+
35
const fsCache = new Map<string, Promise<Buffer>>()
46

5-
async function publicHandler(filename: string, injectNonce: (input: string) => string): Promise<Buffer> {
6-
const bufferPromise: Promise<Buffer> = fsCache.get(filename) ?? readFile(filename)
7-
fsCache.set(filename, bufferPromise)
7+
async function publicHandler(filename: string, injectNonce: (input: string) => string, config: RuntimeConfig): Promise<Buffer> {
8+
let bufferPromise: Promise<Buffer>
9+
10+
if (config.ENABLE_CACHE === 'true') {
11+
bufferPromise = fsCache.get(filename) ?? readFile(filename)
12+
fsCache.set(filename, bufferPromise)
13+
} else {
14+
bufferPromise = readFile(filename)
15+
}
816

917
// in case of html, parse variables
1018
if (filename.endsWith('.html')) {

src/lib/test/extract-acl-context.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ describe('Extract Acl Context', () => {
2525
ACL_CONTEXT_BUILDER: undefined,
2626
ACL_CONTEXT_BUILDER_PATH: '',
2727
CONTENT_TYPE_MAP: {},
28+
ENABLE_CACHE: '',
2829
LANGUAGES_CONFIG: [],
2930
LANGUAGES_DIRECTORY_PATH: '',
3031
PUBLIC_DIRECTORY_PATH: '',

src/lib/test/extract-language-context.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ describe('Extract Language Context', () => {
2525
ACL_CONTEXT_BUILDER: undefined,
2626
ACL_CONTEXT_BUILDER_PATH: '',
2727
CONTENT_TYPE_MAP: {},
28+
ENABLE_CACHE: '',
2829
LANGUAGES_CONFIG: [
2930
{
3031
labelsMap: { hello: 'hello' },

src/lib/test/serve-files.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ describe('Serve files', () => {
6363
cleanup = () => Promise.all([confCleanup(), cpCleanup()])
6464

6565
fastify = await setupFastify({
66+
ENABLE_CACHE: '',
6667
RESOURCES_DIRECTORY_PATH: configurations,
6768
SERVICE_CONFIG_PATH: configPath,
6869
})

src/lib/test/serve-public.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ describe('Serve files', () => {
5757
cleanup = () => Promise.all([pCleanup(), cpCleanup(), rCleanup()])
5858

5959
fastify = await setupFastify({
60+
ENABLE_CACHE: '',
6061
PUBLIC_DIRECTORY_PATH: publicDir,
6162
RESOURCES_DIRECTORY_PATH: resourcesDir,
6263
SERVICE_CONFIG_PATH: configPath,

src/schemas/environmentVariablesSchema.ts

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ export const environmentVariablesSchema = {
2323
description: 'Acl context builder file absolute path',
2424
type: 'string',
2525
},
26+
ENABLE_CACHE: {
27+
description: 'Enable use of cache (default: true)',
28+
type: 'string',
29+
},
2630
LANGUAGES_DIRECTORY_PATH: {
2731
description: 'Absolute path of the directory containing files to be used for translation',
2832
type: 'string',

src/test/config.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const defaults = {
3333
ACL_CONTEXT_BUILDER: undefined,
3434
ACL_CONTEXT_BUILDER_PATH: '/usr/src/app/config/acl-context-builder.js',
3535
CONTENT_TYPE_MAP: defaultConfigs.CONTENT_TYPE_MAP,
36+
ENABLE_CACHE: defaultConfigs.ENABLE_CACHE,
3637
LANGUAGES_CONFIG: [],
3738
LANGUAGES_DIRECTORY_PATH: '/usr/static/languages',
3839
PUBLIC_DIRECTORY_PATH: '/usr/static/public',

0 commit comments

Comments
 (0)