mirror of
https://github.com/fosrl/pangolin.git
synced 2026-09-08 04:02:54 +02:00
add more caching
This commit is contained in:
@@ -14,32 +14,57 @@ export const LAUNCHER_FULL_MAX_SITE_GROUPS = 200;
|
||||
export const LAUNCHER_FULL_MAX_LABEL_GROUPS = 100;
|
||||
export const LAUNCHER_FILTERED_SITE_GROUPING_MAX = 20;
|
||||
|
||||
const LAUNCHER_SCALE_TTL_SEC = 60;
|
||||
const LAUNCHER_SCALE_COUNTS_TTL_SEC = 60;
|
||||
|
||||
function launcherScaleCacheKey(
|
||||
type LauncherScaleCountsCacheEntry = {
|
||||
resourceCount: number;
|
||||
siteGroupCount: number;
|
||||
labelGroupCount: number;
|
||||
mode: LauncherScaleInfo["mode"];
|
||||
};
|
||||
|
||||
function launcherScaleCountsCacheKey(
|
||||
orgId: string,
|
||||
userId: string,
|
||||
roleIds: number[],
|
||||
query: LauncherScaleQuery
|
||||
roleIds: number[]
|
||||
) {
|
||||
const rolesKey = [...roleIds].sort((a, b) => a - b).join(",");
|
||||
const filterKey = [
|
||||
query.query,
|
||||
query.groupBy,
|
||||
query.siteIds ?? "",
|
||||
query.labelIds ?? "",
|
||||
query.sort_by,
|
||||
query.order
|
||||
].join("|");
|
||||
return `launcherScale:${orgId}:${userId}:${rolesKey}:${filterKey}`;
|
||||
return `launcher:scale:counts:${orgId}:${userId}:${rolesKey}`;
|
||||
}
|
||||
|
||||
async function getLauncherScaleForUserUncached(
|
||||
function buildScaleCapabilities(
|
||||
counts: LauncherScaleCountsCacheEntry,
|
||||
query: LauncherScaleQuery
|
||||
): LauncherScaleInfo["capabilities"] {
|
||||
const siteFilterIds = parseIdListParam(query.siteIds);
|
||||
const labelFilterIds = parseIdListParam(query.labelIds);
|
||||
|
||||
return {
|
||||
allowSiteGrouping:
|
||||
counts.siteGroupCount <= LAUNCHER_FULL_MAX_SITE_GROUPS ||
|
||||
(siteFilterIds.length > 0 &&
|
||||
siteFilterIds.length <= LAUNCHER_FILTERED_SITE_GROUPING_MAX),
|
||||
allowLabelGrouping:
|
||||
counts.labelGroupCount <= LAUNCHER_FULL_MAX_LABEL_GROUPS ||
|
||||
(labelFilterIds.length > 0 &&
|
||||
labelFilterIds.length <= LAUNCHER_FILTERED_SITE_GROUPING_MAX),
|
||||
requireSearchOrFilter:
|
||||
counts.mode === "compact" &&
|
||||
counts.resourceCount > LAUNCHER_FULL_MAX_RESOURCES
|
||||
};
|
||||
}
|
||||
|
||||
async function getLauncherScaleCountsForUser(
|
||||
orgId: string,
|
||||
userId: string,
|
||||
userRoleIds: number[],
|
||||
query: LauncherScaleQuery
|
||||
): Promise<LauncherScaleInfo> {
|
||||
userRoleIds: number[]
|
||||
): Promise<LauncherScaleCountsCacheEntry> {
|
||||
const cacheKey = launcherScaleCountsCacheKey(orgId, userId, userRoleIds);
|
||||
const cached = await cache.get<LauncherScaleCountsCacheEntry>(cacheKey);
|
||||
if (cached) {
|
||||
return cached;
|
||||
}
|
||||
|
||||
const accessible = await resolveAccessibleIds(orgId, userId, userRoleIds);
|
||||
const resourceCount =
|
||||
accessible.resourceIds.length + accessible.siteResourceIds.length;
|
||||
@@ -75,33 +100,15 @@ async function getLauncherScaleForUserUncached(
|
||||
? "full"
|
||||
: "compact";
|
||||
|
||||
const siteFilterIds = parseIdListParam(query.siteIds);
|
||||
const labelFilterIds = parseIdListParam(query.labelIds);
|
||||
|
||||
const allowSiteGrouping =
|
||||
siteGroupCount <= LAUNCHER_FULL_MAX_SITE_GROUPS ||
|
||||
(siteFilterIds.length > 0 &&
|
||||
siteFilterIds.length <= LAUNCHER_FILTERED_SITE_GROUPING_MAX);
|
||||
|
||||
const allowLabelGrouping =
|
||||
labelGroupCount <= LAUNCHER_FULL_MAX_LABEL_GROUPS ||
|
||||
(labelFilterIds.length > 0 &&
|
||||
labelFilterIds.length <= LAUNCHER_FILTERED_SITE_GROUPING_MAX);
|
||||
|
||||
const requireSearchOrFilter =
|
||||
mode === "compact" && resourceCount > LAUNCHER_FULL_MAX_RESOURCES;
|
||||
|
||||
return {
|
||||
mode,
|
||||
const result: LauncherScaleCountsCacheEntry = {
|
||||
resourceCount,
|
||||
siteGroupCount,
|
||||
labelGroupCount,
|
||||
capabilities: {
|
||||
allowSiteGrouping,
|
||||
allowLabelGrouping,
|
||||
requireSearchOrFilter
|
||||
}
|
||||
mode
|
||||
};
|
||||
|
||||
await cache.set(cacheKey, result, LAUNCHER_SCALE_COUNTS_TTL_SEC);
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function getLauncherScaleForUser(
|
||||
@@ -110,18 +117,17 @@ export async function getLauncherScaleForUser(
|
||||
userRoleIds: number[],
|
||||
query: LauncherScaleQuery
|
||||
): Promise<LauncherScaleInfo> {
|
||||
const cacheKey = launcherScaleCacheKey(orgId, userId, userRoleIds, query);
|
||||
const cached = await cache.get<LauncherScaleInfo>(cacheKey);
|
||||
if (cached) {
|
||||
return cached;
|
||||
}
|
||||
|
||||
const result = await getLauncherScaleForUserUncached(
|
||||
const counts = await getLauncherScaleCountsForUser(
|
||||
orgId,
|
||||
userId,
|
||||
userRoleIds,
|
||||
query
|
||||
userRoleIds
|
||||
);
|
||||
await cache.set(cacheKey, result, LAUNCHER_SCALE_TTL_SEC);
|
||||
return result;
|
||||
|
||||
return {
|
||||
mode: counts.mode,
|
||||
resourceCount: counts.resourceCount,
|
||||
siteGroupCount: counts.siteGroupCount,
|
||||
labelGroupCount: counts.labelGroupCount,
|
||||
capabilities: buildScaleCapabilities(counts, query)
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user