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

feat: move revalidation interval to a constant #921

Merged
merged 1 commit into from
Oct 3, 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
5 changes: 3 additions & 2 deletions packages/lib/actionClass/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,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 @@ -20,5 +21,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 @@ -3,6 +3,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 @@ -19,5 +20,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 @@ -4,6 +4,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 @@ -24,5 +25,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 @@ -12,6 +12,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 @@ -99,7 +100,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 @@ -8,6 +8,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 @@ -45,7 +46,7 @@ export const getEnvironment = (environmentId: string) =>
[`environments-${environmentId}`],
{
tags: [getEnvironmentCacheTag(environmentId)],
revalidate: 30 * 60, // 30 minutes
revalidate: SERVICES_REVALIDATION_INTERVAL,
}
)();

Expand Down Expand Up @@ -92,7 +93,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 @@ -12,6 +12,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 @@ -85,7 +86,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 @@ -124,7 +125,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 @@ -8,6 +8,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 @@ -50,7 +51,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 @@ -82,7 +83,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 @@ -24,6 +24,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 @@ -64,7 +65,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 @@ -100,7 +101,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 @@ -3,6 +3,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 @@ -19,6 +20,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 @@ -4,6 +4,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 @@ -20,5 +21,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)] }
)();
Loading