feat: add Redis caching to cospend API endpoints
All checks were successful
CI / update (push) Successful in 1m21s

Implement comprehensive caching for all cospend routes to improve performance:

Cache Implementation:
- Balance API: 30-minute TTL for user balances and global balances
- Debts API: 15-minute TTL for debt breakdown calculations
- Payments List: 10-minute TTL with pagination support
- Individual Payment: 30-minute TTL for payment details

Cache Invalidation:
- Created invalidateCospendCaches() helper function
- Invalidates user balances, debts, and payment lists on mutations
- Applied to payment create, update, and delete operations
- Applied to recurring payment execution (manual and cron)
This commit is contained in:
2026-01-13 19:45:09 +01:00
parent 041d415525
commit baa3f3e533
7 changed files with 163 additions and 29 deletions

View File

@@ -5,6 +5,7 @@ import { PaymentSplit } from '../../../../../models/PaymentSplit';
import { dbConnect } from '../../../../../utils/db';
import { error, json } from '@sveltejs/kit';
import { calculateNextExecutionDate } from '../../../../../lib/utils/recurring';
import { invalidateCospendCaches } from '$lib/server/cache';
// This endpoint is designed to be called by a cron job or external scheduler
// It processes all recurring payments that are due for execution
@@ -65,6 +66,10 @@ export const POST: RequestHandler = async ({ request }) => {
await Promise.all(splitPromises);
// Invalidate caches for all affected users
const affectedUsernames = recurringPayment.splits.map((split) => split.username);
await invalidateCospendCaches(affectedUsernames, payment._id.toString());
// Calculate next execution date
const nextExecutionDate = calculateNextExecutionDate(recurringPayment, now);