Skip to content

Commit

Permalink
chore: move revalidation interval to a constant (formbricks#921)
Browse files Browse the repository at this point in the history
  • Loading branch information
rotimi-best committed Oct 3, 2023
1 parent 2ccf92c commit ed6fc1b
Show file tree
Hide file tree
Showing 14 changed files with 36 additions and 24 deletions.
5 changes: 3 additions & 2 deletions packages/lib/actionClass/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { validateInputs } from "../utils/validate";
import { hasUserEnvironmentAccess } from "../environment/auth";
import { getActionClass } from "./service";
import { unstable_cache } from "next/cache";
import { SERVICES_REVALIDATION_INTERVAL } from "../constants";

export const canUserAccessActionClass = async (userId: string, actionClassId: string): Promise<boolean> =>
await unstable_cache(
Expand All @@ -22,5 +23,5 @@ export const canUserAccessActionClass = async (userId: string, actionClassId: st
},

[`users-${userId}-actionClasses-${actionClassId}`],
{ revalidate: 30 * 60, tags: [`actionClasses-${actionClassId}`] }
)(); // 30 minutes
{ revalidate: SERVICES_REVALIDATION_INTERVAL, tags: [`actionClasses-${actionClassId}`] }
)();
7 changes: 3 additions & 4 deletions packages/lib/actionClass/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
import "server-only";

import { prisma } from "@formbricks/database";
import { SERVICES_REVALIDATION_INTERVAL } from "../constants";
import { TActionClass, TActionClassInput, ZActionClassInput } from "@formbricks/types/v1/actionClasses";
import { ZId } from "@formbricks/types/v1/environment";
import { DatabaseError, ResourceNotFoundError } from "@formbricks/types/v1/errors";
import { revalidateTag, unstable_cache } from "next/cache";
import { validateInputs } from "../utils/validate";

const halfHourInSeconds = 60 * 30;

export const getActionClassCacheTag = (name: string, environmentId: string): string =>
`environments-${environmentId}-actionClass-${name}`;

Expand Down Expand Up @@ -50,7 +49,7 @@ export const getActionClasses = (environmentId: string): Promise<TActionClass[]>
[`environments-${environmentId}-actionClasses`],
{
tags: [getActionClassesCacheTag(environmentId)],
revalidate: halfHourInSeconds,
revalidate: SERVICES_REVALIDATION_INTERVAL,
}
)();

Expand Down Expand Up @@ -168,6 +167,6 @@ export const getActionClassCached = async (name: string, environmentId: string)
[`environments-${environmentId}-actionClasses-${name}`],
{
tags: [getActionClassesCacheTag(environmentId)],
revalidate: halfHourInSeconds,
revalidate: SERVICES_REVALIDATION_INTERVAL,
}
)();
5 changes: 3 additions & 2 deletions packages/lib/apiKey/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { validateInputs } from "../utils/validate";
import { hasUserEnvironmentAccess } from "../environment/auth";
import { getApiKey } from "./service";
import { unstable_cache } from "next/cache";
import { SERVICES_REVALIDATION_INTERVAL } from "../constants";

export const canUserAccessApiKey = async (userId: string, apiKeyId: string): Promise<boolean> =>
await unstable_cache(
Expand All @@ -21,5 +22,5 @@ export const canUserAccessApiKey = async (userId: string, apiKeyId: string): Pro
},

[`users-${userId}-apiKeys-${apiKeyId}`],
{ revalidate: 30 * 60, tags: [`apiKeys-${apiKeyId}`] }
)(); // 30 minutes
{ revalidate: SERVICES_REVALIDATION_INTERVAL, tags: [`apiKeys-${apiKeyId}`] }
)();
1 change: 1 addition & 0 deletions packages/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { env } from "@/env.mjs";
export const RESPONSES_LIMIT_FREE = 100;
export const IS_FORMBRICKS_CLOUD = env.IS_FORMBRICKS_CLOUD === "1";
export const REVALIDATION_INTERVAL = 0; //TODO: find a good way to cache and revalidate data when it changes
export const SERVICES_REVALIDATION_INTERVAL = 60 * 30; // 30 minutes
export const MAU_LIMIT = IS_FORMBRICKS_CLOUD ? 5000 : 1000000;

// URLs
Expand Down
5 changes: 3 additions & 2 deletions packages/lib/environment/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { prisma } from "@formbricks/database";
import { ZId } from "@formbricks/types/v1/environment";
import { unstable_cache } from "next/cache";
import { validateInputs } from "../utils/validate";
import { SERVICES_REVALIDATION_INTERVAL } from "../constants";

export const hasUserEnvironmentAccess = async (userId: string, environmentId: string) => {
return await unstable_cache(
Expand Down Expand Up @@ -31,6 +32,6 @@ export const hasUserEnvironmentAccess = async (userId: string, environmentId: st
return environmentUsers.includes(userId);
},
[`users-${userId}-environments-${environmentId}`],
{ revalidate: 30 * 60, tags: [`environments-${environmentId}`] }
)(); // 30 minutes
{ revalidate: SERVICES_REVALIDATION_INTERVAL, tags: [`environments-${environmentId}`] }
)();
};
5 changes: 3 additions & 2 deletions packages/lib/response/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { hasUserEnvironmentAccess } from "../environment/auth";
import { getResponse, getResponseCacheTag } from "./service";
import { unstable_cache } from "next/cache";
import { getSurvey } from "../services/survey";
import { SERVICES_REVALIDATION_INTERVAL } from "../constants";

export const canUserAccessResponse = async (userId: string, responseId: string): Promise<boolean> =>
await unstable_cache(
Expand All @@ -26,5 +27,5 @@ export const canUserAccessResponse = async (userId: string, responseId: string):
return true;
},
[`users-${userId}-responses-${responseId}`],
{ revalidate: 30 * 60, tags: [getResponseCacheTag(responseId)] }
)(); // 30 minutes
{ revalidate: SERVICES_REVALIDATION_INTERVAL, tags: [getResponseCacheTag(responseId)] }
)();
3 changes: 2 additions & 1 deletion packages/lib/services/attributeClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { validateInputs } from "../utils/validate";
import { DatabaseError } from "@formbricks/types/v1/errors";
import { cache } from "react";
import { revalidateTag, unstable_cache } from "next/cache";
import { SERVICES_REVALIDATION_INTERVAL } from "../constants";

const attributeClassesCacheTag = (environmentId: string): string =>
`environments-${environmentId}-attributeClasses`;
Expand Down Expand Up @@ -100,7 +101,7 @@ export const getAttributeClassByNameCached = async (environmentId: string, name:
getAttributeClassesCacheKey(environmentId),
{
tags: getAttributeClassesCacheKey(environmentId),
revalidate: 30 * 60, // 30 minutes
revalidate: SERVICES_REVALIDATION_INTERVAL,
}
)();

Expand Down
5 changes: 3 additions & 2 deletions packages/lib/services/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { populateEnvironment } from "../utils/createDemoProductHelpers";
import { ZEnvironment, ZEnvironmentUpdateInput, ZId } from "@formbricks/types/v1/environment";
import { validateInputs } from "../utils/validate";
import { unstable_cache, revalidateTag } from "next/cache";
import { SERVICES_REVALIDATION_INTERVAL } from "../constants";

export const getEnvironmentCacheTag = (environmentId: string) => `environments-${environmentId}`;
export const getEnvironmentsCacheTag = (productId: string) => `products-${productId}-environments`;
Expand Down Expand Up @@ -46,7 +47,7 @@ export const getEnvironment = (environmentId: string) =>
[`environments-${environmentId}`],
{
tags: [getEnvironmentCacheTag(environmentId)],
revalidate: 30 * 60, // 30 minutes
revalidate: SERVICES_REVALIDATION_INTERVAL,
}
)();

Expand Down Expand Up @@ -93,7 +94,7 @@ export const getEnvironments = async (productId: string): Promise<TEnvironment[]
[`products-${productId}-environments`],
{
tags: [getEnvironmentsCacheTag(productId)],
revalidate: 30 * 60, // 30 minutes
revalidate: SERVICES_REVALIDATION_INTERVAL,
}
)();

Expand Down
3 changes: 2 additions & 1 deletion packages/lib/services/person.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { cache } from "react";
import { PEOPLE_PER_PAGE } from "../constants";
import { validateInputs } from "../utils/validate";
import { getAttributeClassByName } from "./attributeClass";
import { SERVICES_REVALIDATION_INTERVAL } from "../constants";

export const selectPerson = {
id: true,
Expand Down Expand Up @@ -100,7 +101,7 @@ export const getPersonCached = async (personId: string) =>
getPersonCacheKey(personId),
{
tags: getPersonCacheKey(personId),
revalidate: 30 * 60, // 30 minutes
revalidate: SERVICES_REVALIDATION_INTERVAL,
}
)();

Expand Down
5 changes: 3 additions & 2 deletions packages/lib/services/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { validateInputs } from "../utils/validate";
import { EnvironmentType } from "@prisma/client";
import { EventType } from "@prisma/client";
import { getEnvironmentCacheTag, getEnvironmentsCacheTag } from "./environment";
import { SERVICES_REVALIDATION_INTERVAL } from "../constants";

export const getProductsCacheTag = (teamId: string): string => `teams-${teamId}-products`;
const getProductCacheTag = (environmentId: string): string => `environments-${environmentId}-product`;
Expand Down Expand Up @@ -86,7 +87,7 @@ export const getProducts = async (teamId: string): Promise<TProduct[]> =>
[`teams-${teamId}-products`],
{
tags: [getProductsCacheTag(teamId)],
revalidate: 30 * 60, // 30 minutes
revalidate: SERVICES_REVALIDATION_INTERVAL,
}
)();

Expand Down Expand Up @@ -125,7 +126,7 @@ export const getProductByEnvironmentIdCached = (environmentId: string) =>
getProductCacheKey(environmentId),
{
tags: getProductCacheKey(environmentId),
revalidate: 30 * 60, // 30 minutes
revalidate: SERVICES_REVALIDATION_INTERVAL,
}
)();

Expand Down
5 changes: 3 additions & 2 deletions packages/lib/services/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { unstable_cache, revalidateTag } from "next/cache";
import { validateInputs } from "../utils/validate";
import { deleteTeam } from "./team";
import { z } from "zod";
import { SERVICES_REVALIDATION_INTERVAL } from "../constants";

const responseSelection = {
id: true,
Expand Down Expand Up @@ -52,7 +53,7 @@ export const getProfile = async (userId: string): Promise<TProfile | null> =>
[`profiles-${userId}`],
{
tags: [getProfileByEmailCacheTag(userId)],
revalidate: 30 * 60, // 30 minutes
revalidate: SERVICES_REVALIDATION_INTERVAL,
}
)();

Expand Down Expand Up @@ -84,7 +85,7 @@ export const getProfileByEmail = async (email: string): Promise<TProfile | null>
[`profiles-${email}`],
{
tags: [getProfileCacheTag(email)],
revalidate: 30 * 60, // 30 minutes
revalidate: SERVICES_REVALIDATION_INTERVAL,
}
)();

Expand Down
5 changes: 3 additions & 2 deletions packages/lib/services/team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
updateEnvironmentArgs,
} from "../utils/createDemoProductHelpers";
import { validateInputs } from "../utils/validate";
import { SERVICES_REVALIDATION_INTERVAL } from "../constants";

export const select = {
id: true,
Expand Down Expand Up @@ -66,7 +67,7 @@ export const getTeamsByUserId = async (userId: string): Promise<TTeam[]> =>
[`users-${userId}-teams`],
{
tags: [getTeamsByUserIdCacheTag(userId)],
revalidate: 30 * 60, // 30 minutes
revalidate: SERVICES_REVALIDATION_INTERVAL,
}
)();

Expand Down Expand Up @@ -102,7 +103,7 @@ export const getTeamByEnvironmentId = async (environmentId: string): Promise<TTe
[`environments-${environmentId}-team`],
{
tags: [getTeamByEnvironmentIdCacheTag(environmentId)],
revalidate: 30 * 60, // 30 minutes
revalidate: SERVICES_REVALIDATION_INTERVAL,
}
)();

Expand Down
3 changes: 2 additions & 1 deletion packages/lib/tag/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { hasUserEnvironmentAccess } from "../environment/auth";
import { getTag } from "./service";
import { unstable_cache } from "next/cache";
import { ZId } from "@formbricks/types/v1/environment";
import { SERVICES_REVALIDATION_INTERVAL } from "../constants";

export const canUserAccessTag = async (userId: string, tagId: string): Promise<boolean> =>
await unstable_cache(
Expand All @@ -21,6 +22,6 @@ export const canUserAccessTag = async (userId: string, tagId: string): Promise<b
},
[`${userId}-${tagId}`],
{
revalidate: 30 * 60, // 30 minutes
revalidate: SERVICES_REVALIDATION_INTERVAL,
}
)();
3 changes: 2 additions & 1 deletion packages/lib/tagOnResponse/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ZId } from "@formbricks/types/v1/environment";
import { canUserAccessResponse } from "../response/auth";
import { canUserAccessTag } from "../tag/auth";
import { getTagOnResponseCacheTag } from "../services/tagOnResponse";
import { SERVICES_REVALIDATION_INTERVAL } from "../constants";

export const canUserAccessTagOnResponse = async (
userId: string,
Expand All @@ -22,5 +23,5 @@ export const canUserAccessTagOnResponse = async (
return isAuthorizedForTag && isAuthorizedForResponse;
},
[`users-${userId}-tagOnResponse-${tagId}-${responseId}`],
{ revalidate: 30 * 60, tags: [getTagOnResponseCacheTag(tagId, responseId)] }
{ revalidate: SERVICES_REVALIDATION_INTERVAL, tags: [getTagOnResponseCacheTag(tagId, responseId)] }
)();

0 comments on commit ed6fc1b

Please sign in to comment.