summaryrefslogtreecommitdiff
path: root/lib/revalidation-utils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'lib/revalidation-utils.ts')
-rw-r--r--lib/revalidation-utils.ts99
1 files changed, 99 insertions, 0 deletions
diff --git a/lib/revalidation-utils.ts b/lib/revalidation-utils.ts
new file mode 100644
index 00000000..14c5bf01
--- /dev/null
+++ b/lib/revalidation-utils.ts
@@ -0,0 +1,99 @@
+/**
+ * Request Context가 없는 환경(cronjob, background worker 등)에서
+ * 캐시를 무효화하기 위한 유틸리티
+ *
+ * 사용 예시:
+ * ```ts
+ * // Cronjob에서 사용
+ * await revalidateViaCronJob({ tags: ['vendors', 'users'] });
+ *
+ * // 또는 개별 태그
+ * await revalidateTagViaCronJob('vendors');
+ * ```
+ */
+
+/**
+ * API를 통한 캐시 무효화 (cronjob 등에서 사용)
+ */
+export async function revalidateViaCronJob(options: {
+ tags?: string[];
+}): Promise<{ success: boolean; error?: string }> {
+ try {
+ const baseUrl = process.env.NEXT_PUBLIC_BASE_URL || 'http://localhost:3000';
+
+ const response = await fetch(`${baseUrl}/api/revalidate/approval`, {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify({
+ tags: options.tags,
+ secret: process.env.REVALIDATION_SECRET,
+ }),
+ });
+
+ if (!response.ok) {
+ const error = await response.text();
+ console.error('[Revalidation] API call failed:', error);
+ return {
+ success: false,
+ error: `Revalidation API failed: ${response.statusText}`,
+ };
+ }
+
+ const result = await response.json();
+ console.log('[Revalidation] Cache invalidated:', options.tags);
+
+ return { success: true };
+ } catch (error) {
+ console.error('[Revalidation] Failed to revalidate cache:', error);
+ // 캐시 무효화 실패는 치명적이지 않으므로 경고만 출력
+ return {
+ success: false,
+ error: error instanceof Error ? error.message : 'Unknown error',
+ };
+ }
+}
+
+/**
+ * 단일 태그 무효화 (cronjob에서 사용)
+ */
+export async function revalidateTagViaCronJob(tag: string) {
+ return revalidateViaCronJob({ tags: [tag] });
+}
+
+/**
+ * 벤더 관련 캐시 무효화
+ */
+export async function revalidateVendorCaches() {
+ return revalidateViaCronJob({
+ tags: ['vendors', 'vendor-status-counts'],
+ });
+}
+
+/**
+ * 유저 관련 캐시 무효화
+ */
+export async function revalidateUserCaches() {
+ return revalidateViaCronJob({
+ tags: ['users', 'roles', 'user-roles'],
+ });
+}
+
+/**
+ * 승인 프로세스에서 사용하는 모든 캐시 무효화
+ */
+export async function revalidateApprovalRelatedCaches() {
+ return revalidateViaCronJob({
+ tags: [
+ 'vendors',
+ 'vendor-status-counts',
+ 'users',
+ 'roles',
+ 'user-roles',
+ 'approval-logs',
+ 'pending-actions',
+ ],
+ });
+}
+