Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
♻️ Remove unnecessary caching
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Jun 21, 2019
1 parent 9f9b9b1 commit 8ec93c6
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 166 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "staart",
"version": "1.0.25",
"version": "1.0.26",
"main": "index.js",
"repository": "[email protected]:AnandChowdhary/staart.git",
"author": "Anand Chowdhary <[email protected]>",
Expand Down
52 changes: 8 additions & 44 deletions src/crud/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export const createEmail = async (
email.isVerified = isVerified;
email.createdAt = new Date();
email.updatedAt = email.createdAt;
deleteItemFromCache(CacheCategories.USER_EMAILS, email.userId);
const result = <InsertResult>(
await query(
`INSERT INTO emails ${tableValues(email)}`,
Expand Down Expand Up @@ -79,13 +78,6 @@ export const updateEmail = async (id: number, email: KeyValue) => {
email.updatedAt = dateToDateTime(new Date());
email = removeReadOnlyValues(email);
const emailDetails = await getEmail(id);
deleteItemFromCache(CacheCategories.USER_EMAILS, emailDetails.userId);
deleteItemFromCache(CacheCategories.EMAIL, emailDetails.email);
deleteItemFromCache(CacheCategories.EMAIL, id);
deleteItemFromCache(
CacheCategories.USER_VERIFIED_EMAILS,
emailDetails.userId
);
return await query(`UPDATE emails SET ${setValues(email)} WHERE id = ?`, [
...Object.values(email),
id
Expand All @@ -97,22 +89,16 @@ export const updateEmail = async (id: number, email: KeyValue) => {
*/
export const deleteEmail = async (id: number) => {
const emailDetails = await getEmail(id);
deleteItemFromCache(CacheCategories.USER_EMAILS, emailDetails.userId);
deleteItemFromCache(CacheCategories.EMAIL, emailDetails.email);
deleteItemFromCache(CacheCategories.EMAIL, id);
return await query("DELETE FROM emails WHERE id = ?", [id]);
};

/**
* Delete a user's email
*/
export const deleteAllUserEmails = async (userId: number) => {
deleteItemFromCache(CacheCategories.USER_EMAILS, userId);
const allEmails = await getUserEmails(userId);
allEmails.forEach(email => {
if (email.id && email.email) {
deleteItemFromCache(CacheCategories.EMAIL, email.id);
deleteItemFromCache(CacheCategories.EMAIL, email.email);
}
});
return await query("DELETE FROM emails WHERE userId = ?", [userId]);
Expand All @@ -123,12 +109,7 @@ export const deleteAllUserEmails = async (userId: number) => {
*/
export const getEmail = async (id: number) => {
return (<Email[]>(
await cachedQuery(
CacheCategories.EMAIL,
id,
"SELECT * FROM emails WHERE id = ? LIMIT 1",
[id]
)
await query("SELECT * FROM emails WHERE id = ? LIMIT 1", [id])
))[0];
};

Expand Down Expand Up @@ -161,12 +142,7 @@ export const getUserPrimaryEmail = async (user: User | number) => {
*/
export const getUserEmails = async (userId: number) => {
return await addIsPrimaryToEmails(<Email[]>(
await cachedQuery(
CacheCategories.USER_EMAILS,
userId,
"SELECT * FROM emails WHERE userId = ?",
[userId]
)
await query("SELECT * FROM emails WHERE userId = ?", [userId])
));
};

Expand All @@ -178,9 +154,7 @@ export const getUserBestEmail = async (userId: number) => {
return await getUserPrimaryEmail(userId);
} catch (error) {}
return await (<Email[]>(
await cachedQuery(
CacheCategories.USER_EMAILS,
userId,
await query(
"SELECT * FROM emails WHERE userId = ? ORDER BY isVerified DESC LIMIT 1",
[userId]
)
Expand All @@ -193,12 +167,7 @@ export const getUserBestEmail = async (userId: number) => {
export const getEmailObject = async (email: string) => {
return await addIsPrimaryToEmail(
(<Email[]>(
await cachedQuery(
CacheCategories.EMAIL,
email,
"SELECT * FROM emails WHERE email = ? LIMIT 1",
[email]
)
await query("SELECT * FROM emails WHERE email = ? LIMIT 1", [email])
))[0]
);
};
Expand All @@ -208,9 +177,7 @@ export const getEmailObject = async (email: string) => {
*/
export const getVerifiedEmailObject = async (email: string) => {
return (<Email[]>(
await cachedQuery(
CacheCategories.EMAIL,
email,
await query(
"SELECT * FROM emails WHERE email = ? AND isVerified = 1 LIMIT 1",
[email]
)
Expand All @@ -229,12 +196,9 @@ export const getUserVerifiedEmails = async (user: User | number) => {
}
if (!userId) throw new Error(ErrorCode.USER_NOT_FOUND);
return await addIsPrimaryToEmails(<Email[]>(
await cachedQuery(
CacheCategories.USER_VERIFIED_EMAILS,
userId,
"SELECT * FROM emails WHERE userId = ? AND isVerified = 1",
[userId]
)
await query("SELECT * FROM emails WHERE userId = ? AND isVerified = 1", [
userId
])
));
};

Expand Down
15 changes: 2 additions & 13 deletions src/crud/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ export const createEvent = async (event: Event, locals?: Locals) => {
event.ipAddress = locals.ipAddress;
event.userAgent = locals.userAgent;
}
if (event.userId) {
deleteItemFromCache(CacheCategories.USER_EVENT, event.userId);
deleteItemFromCache(CacheCategories.USER_RECENT_EVENTS, event.userId);
}
await query(`INSERT INTO events ${tableValues(event)}`, Object.values(event));
};

Expand All @@ -27,12 +23,7 @@ export const createEvent = async (event: Event, locals?: Locals) => {
*/
export const getUserEvents = async (userId: number) => {
return <Event[]>(
await cachedQuery(
CacheCategories.USER_EVENT,
userId,
`SELECT * FROM events WHERE userId = ?`,
[userId]
)
await query(`SELECT * FROM events WHERE userId = ?`, [userId])
);
};

Expand All @@ -41,9 +32,7 @@ export const getUserEvents = async (userId: number) => {
*/
export const getUserRecentEvents = async (userId: number) => {
return await addLocationToEvents(<Event[]>(
await cachedQuery(
CacheCategories.USER_RECENT_EVENTS,
userId,
await query(
`SELECT * FROM events WHERE userId = ? ORDER BY id DESC LIMIT 10`,
[userId]
)
Expand Down
46 changes: 4 additions & 42 deletions src/crud/membership.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,7 @@ import { getPaginatedData } from "./data";
export const createMembership = async (membership: Membership) => {
membership.createdAt = new Date();
membership.updatedAt = membership.createdAt;
deleteItemFromCache(
CacheCategories.ORGANIZATION_MEMBERSHIPS,
membership.organizationId
);
deleteItemFromCache(CacheCategories.USER_MEMBERSHIPS, membership.userId);
deleteItemFromCache(
CacheCategories.USER_MEMBERSHIP_ORGANIZATION,
`${membership.userId}_${membership.organizationId}`
);
return await query(
`INSERT INTO memberships ${tableValues(membership)}`,
Object.values(membership)
Expand All @@ -49,14 +41,6 @@ export const updateMembership = async (id: number, membership: KeyValue) => {
membershipDetails.userId
);
deleteItemFromCache(CacheCategories.MEMBERSHIP, id);
deleteItemFromCache(
CacheCategories.ORGANIZATION_MEMBERSHIPS,
membershipDetails.organizationId
);
deleteItemFromCache(
CacheCategories.USER_MEMBERSHIP_ORGANIZATION,
`${membershipDetails.userId}_${membershipDetails.organizationId}`
);
return await query(
`UPDATE memberships SET ${setValues(membership)} WHERE id = ?`,
[...Object.values(membership), id]
Expand All @@ -74,14 +58,6 @@ export const deleteMembership = async (id: number) => {
membershipDetails.userId
);
deleteItemFromCache(CacheCategories.MEMBERSHIP, id);
deleteItemFromCache(
CacheCategories.ORGANIZATION_MEMBERSHIPS,
membershipDetails.organizationId
);
deleteItemFromCache(
CacheCategories.USER_MEMBERSHIP_ORGANIZATION,
`${membershipDetails.userId}_${membershipDetails.organizationId}`
);
return await query("DELETE FROM memberships WHERE id = ?", [id]);
};

Expand All @@ -91,15 +67,10 @@ export const deleteMembership = async (id: number) => {
export const deleteAllOrganizationMemberships = async (
organizationId: number
) => {
deleteItemFromCache(CacheCategories.ORGANIZATION_MEMBERSHIPS, organizationId);
const allMemberships = await getOrganizationMembers(organizationId);
for await (const membership of allMemberships) {
if (membership.id) {
deleteItemFromCache(CacheCategories.USER_MEMBERSHIPS, membership.userId);
deleteItemFromCache(
CacheCategories.USER_MEMBERSHIP_ORGANIZATION,
`${membership.userId}_${membership.organizationId}`
);
}
}
return await query("DELETE FROM memberships WHERE organizationId = ?", [
Expand All @@ -115,10 +86,6 @@ export const deleteAllUserMemberships = async (userId: number) => {
for await (const membership of allMemberships) {
if (membership.id) {
deleteItemFromCache(CacheCategories.USER_MEMBERSHIPS, membership.userId);
deleteItemFromCache(
CacheCategories.USER_MEMBERSHIP_ORGANIZATION,
`${membership.userId}_${membership.organizationId}`
);
}
}
return await query("DELETE FROM memberships WHERE userId = ?", [userId]);
Expand Down Expand Up @@ -155,12 +122,9 @@ export const getMembershipDetailed = async (id: number) => {
*/
export const getOrganizationMembers = async (organizationId: number) => {
return <Membership[]>(
await cachedQuery(
CacheCategories.ORGANIZATION_MEMBERSHIPS,
organizationId,
`SELECT * FROM memberships WHERE organizationId = ?`,
[organizationId]
)
await query(`SELECT * FROM memberships WHERE organizationId = ?`, [
organizationId
])
);
};

Expand Down Expand Up @@ -248,9 +212,7 @@ export const getUserOrganizationMembership = async (
else throw new Error(ErrorCode.ORGANIZATION_NOT_FOUND);
}
return (<Membership[]>(
await cachedQuery(
CacheCategories.USER_MEMBERSHIP_ORGANIZATION,
`${user}_${organization}`,
await query(
`SELECT * FROM memberships WHERE userId = ? AND organizationId = ?`,
[user, organization]
)
Expand Down
25 changes: 2 additions & 23 deletions src/crud/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export const createNotification = async (notification: Notification) => {
notification.createdAt = new Date();
notification.updatedAt = notification.createdAt;
notification.read = !!notification.read;
deleteItemFromCache(CacheCategories.USER_NOTIFICATIONS, notification.userId);
return await query(
`INSERT INTO notifications ${tableValues(notification)}`,
Object.values(notification)
Expand All @@ -29,12 +28,7 @@ export const createNotification = async (notification: Notification) => {
*/
export const getNotification = async (notificationId: number) => {
return (<Notification[]>(
await cachedQuery(
CacheCategories.NOTIFICATION,
notificationId,
"SELECT * FROM notifications WHERE id = ?",
[notificationId]
)
await query("SELECT * FROM notifications WHERE id = ?", [notificationId])
))[0];
};

Expand All @@ -49,11 +43,6 @@ export const updateNotification = async (
if (!notificationDetails.userId) throw new Error(ErrorCode.NOT_FOUND);
data.updatedAt = dateToDateTime(new Date());
data = removeReadOnlyValues(data);
deleteItemFromCache(CacheCategories.NOTIFICATION, notificationId);
deleteItemFromCache(
CacheCategories.USER_NOTIFICATIONS,
notificationDetails.userId
);
return await query(
`UPDATE notifications SET ${setValues(data)} WHERE id = ?`,
[...Object.values(data), notificationId]
Expand All @@ -65,11 +54,6 @@ export const updateNotification = async (
*/
export const deleteNotification = async (notificationId: number) => {
const notificationDetails = await getNotification(notificationId);
deleteItemFromCache(CacheCategories.NOTIFICATION, notificationId);
deleteItemFromCache(
CacheCategories.USER_NOTIFICATIONS,
notificationDetails.userId
);
return await query("DELETE FROM `notifications` WHERE id = ? LIMIT 1", [
notificationId
]);
Expand All @@ -80,11 +64,6 @@ export const deleteNotification = async (notificationId: number) => {
*/
export const getUserNotifications = async (userId: number) => {
return <Notification[]>(
await cachedQuery(
CacheCategories.USER_NOTIFICATIONS,
userId,
"SELECT * FROM notifications WHERE userId = ?",
[userId]
)
await query("SELECT * FROM notifications WHERE userId = ?", [userId])
);
};
21 changes: 8 additions & 13 deletions src/crud/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ export const getApiKeyFromKeySecret = async (
apiKey: string,
secretKey: string
) => {
deleteItemFromCache(CacheCategories.API_KEY, apiKey);
return (<ApiKey[]>(
await query(
"SELECT * FROM `api-keys` WHERE apiKey = ? AND secretKey = ? AND organizationId = ? LIMIT 1",
Expand All @@ -159,7 +158,6 @@ export const createApiKey = async (apiKey: ApiKey) => {
apiKey.secretKey = cryptoRandomString({ length: 20, type: "hex" });
apiKey.createdAt = new Date();
apiKey.updatedAt = apiKey.createdAt;
deleteItemFromCache(CacheCategories.API_KEYS, apiKey.organizationId);
return await query(
`INSERT INTO \`api-keys\` ${tableValues(apiKey)}`,
Object.values(apiKey)
Expand All @@ -174,25 +172,22 @@ export const updateApiKey = async (
apiKey: string,
data: KeyValue
) => {
const apiKeyDetails = await getApiKey(organizationId, apiKey);
data.updatedAt = dateToDateTime(new Date());
data = removeReadOnlyValues(data);
deleteItemFromCache(CacheCategories.API_KEY, apiKey);
deleteItemFromCache(CacheCategories.API_KEYS, apiKeyDetails.organizationId);
return await query(
`UPDATE \`api-keys\` SET ${setValues(data)} WHERE apiKey = ?`,
[...Object.values(data), apiKey]
`UPDATE \`api-keys\` SET ${setValues(
data
)} WHERE apiKey = ? AND organizationId = ?`,
[...Object.values(data), apiKey, organizationId]
);
};

/**
* Delete an API key
*/
export const deleteApiKey = async (organizationId: number, apiKey: string) => {
const apiKeyDetails = await getApiKey(organizationId, apiKey);
deleteItemFromCache(CacheCategories.API_KEY, apiKey);
deleteItemFromCache(CacheCategories.API_KEYS, apiKeyDetails.organizationId);
return await query("DELETE FROM `api-keys` WHERE apiKey = ? LIMIT 1", [
apiKey
]);
return await query(
"DELETE FROM `api-keys` WHERE apiKey = ? AND organizationId = ? LIMIT 1",
[apiKey, organizationId]
);
};
Loading

0 comments on commit 8ec93c6

Please sign in to comment.