diff --git a/README.md b/README.md index ff2d632..a531b28 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ const fastify = Fastify().register(dynamodbCache, { dynamoDbAddress: "http://localhost:8000", // Optional! If you are hosting your own instance of Dynamo (locally or cloud), then specify the ip address of the database here. tableName: "fastify-dynamodb-cache", // DynamoDB table name defaultTTLSeconds: 30, // Default TTL (seconds), which would be used if no TTL is specified on the endpoint. + disableCache: true, // Optional! If you want to disable caching from being set on endpoints, you can set this to true. Set it to false or leave it empty to enable cache. }); fastify.get( diff --git a/src/index.ts b/src/index.ts index e3b834b..2ec2529 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,6 +9,7 @@ export interface PluginOptions { dynamoDbAddress?: string; tableName: string; defaultTTLSeconds: number; + disableCache?: boolean; } export const dynamodbCache: FastifyPluginAsync = async ( @@ -22,9 +23,10 @@ export const dynamodbCache: FastifyPluginAsync = async ( fastify.addHook("onRoute", (routeOptions) => { if ( - routeOptions.config && - routeOptions.config.cache && - routeOptions.config.cache.cacheEnabled === true + !opts.disableCache && // Only add cache to route if plugin setting "disableCache" is false or undefined + routeOptions.config && // Check if route config object is set + routeOptions.config.cache && // Check if route config cache object is set + routeOptions.config.cache.cacheEnabled === true // Check if cache object contains "cacheEnabled" property and that is true ) { const onRequestHook = createOnRequestHook({ dynamoClient, diff --git a/test/src/index.ts b/test/src/index.ts index 8a211e5..d796b22 100644 --- a/test/src/index.ts +++ b/test/src/index.ts @@ -15,6 +15,7 @@ fastify dynamoDbAddress: "http://localhost:8000", tableName: "fastify-dynamodb-cache", defaultTTLSeconds: 30, + disableCache: false, // Cache is enabled }) .register(fastifySwagger, { openapi: {