|
1 | 1 | import { Cluster, ClusterOptions } from "ioredis"; |
2 | 2 | import { logger } from "./logger"; |
| 3 | +import { db, folders } from "../db"; |
| 4 | +import { eq } from "drizzle-orm"; |
3 | 5 |
|
4 | 6 | let client: Cluster | null = null; |
5 | 7 |
|
@@ -184,3 +186,102 @@ export async function deleteCachePattern(pattern: string): Promise<void> { |
184 | 186 | ); |
185 | 187 | } |
186 | 188 | } |
| 189 | + |
| 190 | +/** |
| 191 | + * Recursively get all ancestor folder IDs for a given folder |
| 192 | + * @param folderId - The folder ID to start from |
| 193 | + * @returns Array of ancestor folder IDs (from immediate parent to root) |
| 194 | + */ |
| 195 | +async function getAncestorFolderIds(folderId: string): Promise<string[]> { |
| 196 | + const ancestorIds: string[] = []; |
| 197 | + let currentFolderId: string | null = folderId; |
| 198 | + |
| 199 | + // Traverse up the hierarchy until we reach a root folder (parentId is null) |
| 200 | + while (currentFolderId) { |
| 201 | + const folder: { parentId: string | null } | undefined = await db.query.folders.findFirst({ |
| 202 | + where: eq(folders.id, currentFolderId), |
| 203 | + columns: { |
| 204 | + parentId: true, |
| 205 | + }, |
| 206 | + }); |
| 207 | + |
| 208 | + if (!folder || !folder.parentId) { |
| 209 | + break; |
| 210 | + } |
| 211 | + |
| 212 | + ancestorIds.push(folder.parentId); |
| 213 | + currentFolderId = folder.parentId; |
| 214 | + } |
| 215 | + |
| 216 | + return ancestorIds; |
| 217 | +} |
| 218 | + |
| 219 | +/** |
| 220 | + * Invalidate note counts cache for a user and all ancestor folders |
| 221 | + * This should be called whenever notes are created, updated, deleted, or their properties change |
| 222 | + * @param userId - The user ID |
| 223 | + * @param folderId - The folder ID where the note resides (null for root level notes) |
| 224 | + */ |
| 225 | +export async function invalidateNoteCounts(userId: string, folderId: string | null): Promise<void> { |
| 226 | + const cache = getCacheClient(); |
| 227 | + if (!cache) return; |
| 228 | + |
| 229 | + try { |
| 230 | + const cacheKeys: string[] = []; |
| 231 | + |
| 232 | + // Always invalidate user's global counts (matches CacheKeys.notesCounts pattern) |
| 233 | + cacheKeys.push(`notes:${userId}:counts`); |
| 234 | + |
| 235 | + // If note is in a folder, invalidate that folder and all ancestors |
| 236 | + if (folderId) { |
| 237 | + // Invalidate the immediate folder (matches counts.ts line 89 pattern) |
| 238 | + cacheKeys.push(`notes:${userId}:folder:${folderId}:counts`); |
| 239 | + |
| 240 | + // Get and invalidate all ancestor folders |
| 241 | + const ancestorIds = await getAncestorFolderIds(folderId); |
| 242 | + for (const ancestorId of ancestorIds) { |
| 243 | + cacheKeys.push(`notes:${userId}:folder:${ancestorId}:counts`); |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + // Delete all cache keys using pipeline for cluster compatibility |
| 248 | + if (cacheKeys.length > 0) { |
| 249 | + await deleteCache(...cacheKeys); |
| 250 | + logger.debug("Invalidated note counts cache", { |
| 251 | + userId, |
| 252 | + folderId: folderId || "root", |
| 253 | + keysInvalidated: cacheKeys.length, |
| 254 | + }); |
| 255 | + } |
| 256 | + } catch (error) { |
| 257 | + logger.error( |
| 258 | + "Failed to invalidate note counts cache", |
| 259 | + { |
| 260 | + userId, |
| 261 | + folderId: folderId || "root", |
| 262 | + }, |
| 263 | + error instanceof Error ? error : new Error(String(error)) |
| 264 | + ); |
| 265 | + } |
| 266 | +} |
| 267 | + |
| 268 | +/** |
| 269 | + * Invalidate note counts cache when a note moves between folders |
| 270 | + * Invalidates both old and new folder hierarchies |
| 271 | + * @param userId - The user ID |
| 272 | + * @param oldFolderId - The previous folder ID (null for root) |
| 273 | + * @param newFolderId - The new folder ID (null for root) |
| 274 | + */ |
| 275 | +export async function invalidateNoteCountsForMove( |
| 276 | + userId: string, |
| 277 | + oldFolderId: string | null, |
| 278 | + newFolderId: string | null |
| 279 | +): Promise<void> { |
| 280 | + // Invalidate old folder hierarchy |
| 281 | + await invalidateNoteCounts(userId, oldFolderId); |
| 282 | + |
| 283 | + // Invalidate new folder hierarchy (if different from old) |
| 284 | + if (oldFolderId !== newFolderId) { |
| 285 | + await invalidateNoteCounts(userId, newFolderId); |
| 286 | + } |
| 287 | +} |
0 commit comments