Skip to content

Commit

Permalink
Use cache for month txn
Browse files Browse the repository at this point in the history
  • Loading branch information
amitsingh-007 committed Dec 27, 2024
1 parent 6c145b0 commit 1e74ea9
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 64 deletions.
64 changes: 64 additions & 0 deletions src/server/cache/transaction-cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { COLLECTION } from '@/types/database';
import {
unstable_cacheLife as cacheLife,
unstable_cacheTag as cacheTag,
revalidateTag,
} from 'next/cache';
import { monthFilter } from '../utils/transaction-filters';
import { aggregateSum, fetch } from '../firebase-admin/firestore';

/**
* Cache keys
*/
const getTxnByMonthYearKey = (userId: string, month: number, year: number) =>
`transactions-month-year-${userId}-${month}-${year}`;

const getMontlyTxnSumKey = (userId: string, month: number, year: number) =>
`monthly-sum-${userId}-${month}-${year}`;

/**
* Cache flush methods
*/
export const clearMonthYearTxnCache = (
userId: string,
month: number,
year: number
) => {
revalidateTag(getTxnByMonthYearKey(userId, month, year));
revalidateTag(getMontlyTxnSumKey(userId, month, year));
};

/**
* Cache methods
*/
export const getTxnByMonthYearCache = async (
userId: string,
month: number,
year: number
) => {
'use cache';
cacheTag(getTxnByMonthYearKey(userId, month, year));
cacheLife('max');

return fetch({
userId,
collection: COLLECTION.TRANSACTIONS,
conditions: monthFilter(month, year),
});
};

export const getMonthTotalSumCache = async (
userId: string,
month: number,
year: number
) => {
'use cache';
cacheTag(getMontlyTxnSumKey(userId, month, year));
cacheLife('max');

return await aggregateSum({
userId,
collection: COLLECTION.TRANSACTIONS,
conditions: monthFilter(month, year),
});
};
65 changes: 8 additions & 57 deletions src/server/services/transaction-service.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,26 @@
import { COLLECTION, TCondition } from '@/types/database';
import { COLLECTION } from '@/types/database';
import { TCardTransactionForm } from '@/types/form';
import { TRPCError } from '@trpc/server';
import { UserRecord } from 'firebase-admin/auth';
import {
addToCollection,
aggregateSum,
fetch,
fetchExists,
} from '../firebase-admin/firestore';
import { MONTHS } from '@/app/constants';
import { cardFilter, monthFilter } from '../utils/transaction-filters';
import {
unstable_cacheLife as cacheLife,
unstable_cacheTag as cacheTag,
revalidateTag,
} from 'next/cache';
import { getMontlySumCacheKey } from '../utils/cache';

const monthFilter = (
month: number,
year: number
): TCondition<COLLECTION.TRANSACTIONS>[] => {
const startDate = new Date(year, month, 1).getTime();
const endDate = new Date(year, month + 1, 1).getTime();
return [
{
fieldPath: 'date',
opStr: '>=',
value: startDate,
},
{
fieldPath: 'date',
opStr: '<',
value: endDate,
},
];
};

const cardFilter = (cardId: string): TCondition<COLLECTION.TRANSACTIONS> => ({
fieldPath: 'cardId',
opStr: '==',
value: cardId,
});
clearMonthYearTxnCache,
getMonthTotalSumCache,
getTxnByMonthYearCache,
} from '../cache/transaction-cache';

export const getTransactionsByMonthYear = async (
user: UserRecord,
month: number,
year: number
) => {
return fetch({
userId: user.uid,
collection: COLLECTION.TRANSACTIONS,
conditions: monthFilter(month, year),
});
return getTxnByMonthYearCache(user.uid, month, year);
};

export const getPaginatedTransactions = async (
Expand All @@ -68,22 +37,6 @@ export const getPaginatedTransactions = async (
});
};

const getMonthTotalSumCache = async (
userId: string,
month: number,
year: number
) => {
'use cache';
cacheTag(getMontlySumCacheKey(userId, month, year));
cacheLife('max');

return await aggregateSum({
userId,
collection: COLLECTION.TRANSACTIONS,
conditions: monthFilter(month, year),
});
};

export const getAnnualSummary = async (user: UserRecord, year: number) => {
const promises = MONTHS.map(async (month) => {
const sum = await getMonthTotalSumCache(user.uid, month.value, year);
Expand Down Expand Up @@ -116,7 +69,5 @@ export const saveTransaction = async (

await addToCollection(user, COLLECTION.TRANSACTIONS, transaction);

revalidateTag(
getMontlySumCacheKey(user.uid, date.getMonth(), date.getFullYear())
);
clearMonthYearTxnCache(user.uid, date.getMonth(), date.getFullYear());
};
7 changes: 0 additions & 7 deletions src/server/utils/cache.ts

This file was deleted.

29 changes: 29 additions & 0 deletions src/server/utils/transaction-filters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { COLLECTION, TCondition } from '@/types/database';

export const monthFilter = (
month: number,
year: number
): TCondition<COLLECTION.TRANSACTIONS>[] => {
const startDate = new Date(year, month, 1).getTime();
const endDate = new Date(year, month + 1, 1).getTime();
return [
{
fieldPath: 'date',
opStr: '>=',
value: startDate,
},
{
fieldPath: 'date',
opStr: '<',
value: endDate,
},
];
};

export const cardFilter = (
cardId: string
): TCondition<COLLECTION.TRANSACTIONS> => ({
fieldPath: 'cardId',
opStr: '==',
value: cardId,
});

0 comments on commit 1e74ea9

Please sign in to comment.