Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disable overall cache #7

Merged
merged 3 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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