Skip to content

Commit

Permalink
Merge pull request #7 from blastorg/disable-overall-cache
Browse files Browse the repository at this point in the history
disable overall cache
  • Loading branch information
fredrikj31 authored Nov 10, 2023
2 parents f9ab787 + db4eb5d commit a87dd48
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface PluginOptions {
dynamoDbAddress?: string;
tableName: string;
defaultTTLSeconds: number;
disableCache?: boolean;
}

export const dynamodbCache: FastifyPluginAsync<PluginOptions> = async (
Expand All @@ -22,9 +23,10 @@ export const dynamodbCache: FastifyPluginAsync<PluginOptions> = 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,
Expand Down
1 change: 1 addition & 0 deletions test/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ fastify
dynamoDbAddress: "http://localhost:8000",
tableName: "fastify-dynamodb-cache",
defaultTTLSeconds: 30,
disableCache: false, // Cache is enabled
})
.register(fastifySwagger, {
openapi: {
Expand Down

0 comments on commit a87dd48

Please sign in to comment.