Refactored redis usage. Added lib/cache.

This commit is contained in:
Mike Cao 2022-11-07 22:35:51 -08:00
parent 3485b6268b
commit f118bc95c1
22 changed files with 236 additions and 221 deletions

View file

@ -1,5 +1,5 @@
import prisma from 'lib/prisma';
import redis, { DELETED } from 'lib/redis';
import cache from 'lib/cache';
export async function deleteUser(userId) {
const { client } = prisma;
@ -8,12 +8,6 @@ export async function deleteUser(userId) {
where: { userId },
});
let websiteIds = [];
if (websites.length > 0) {
websiteIds = websites.map(a => a.id);
}
return client
.$transaction([
client.pageview.deleteMany({
@ -37,13 +31,15 @@ export async function deleteUser(userId) {
},
}),
])
.then(async res => {
if (redis.enabled) {
for (let i = 0; i < websiteIds.length; i++) {
await redis.set(`website:${websiteIds[i]}`, DELETED);
.then(async data => {
if (cache.enabled) {
const ids = websites.map(a => a.id);
for (let i = 0; i < ids.length; i++) {
await cache.deleteWebsite(`website:${ids[i]}`);
}
}
return res;
return data;
});
}

View file

@ -1,5 +1,5 @@
import prisma from 'lib/prisma';
import redis from 'lib/redis';
import cache from 'lib/cache';
export async function createWebsite(userId, data) {
return prisma.client.website
@ -13,11 +13,11 @@ export async function createWebsite(userId, data) {
...data,
},
})
.then(async res => {
if (redis.enabled && res) {
await redis.set(`website:${res.id}`, res);
.then(async data => {
if (cache.enabled) {
await cache.storeWebsite(data);
}
return res;
return data;
});
}

View file

@ -1,5 +1,5 @@
import prisma from 'lib/prisma';
import redis, { DELETED } from 'lib/redis';
import cache from 'lib/cache';
export async function deleteWebsite(id) {
const { client, transaction } = prisma;
@ -20,11 +20,11 @@ export async function deleteWebsite(id) {
client.website.delete({
where: { id },
}),
]).then(async res => {
if (redis.enabled) {
await redis.set(`website:${id}`, DELETED);
]).then(async data => {
if (cache.enabled) {
await cache.deleteWebsite(id);
}
return res;
return data;
});
}

View file

@ -1,16 +1,7 @@
import prisma from 'lib/prisma';
import redis from 'lib/redis';
export async function getWebsite(where) {
return prisma.client.website
.findUnique({
where,
})
.then(async data => {
if (redis.enabled && data) {
await redis.set(`website:${data.id}`, data);
}
return data;
});
return prisma.client.website.findUnique({
where,
});
}