-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(redis): Change Redis Implementation
- Loading branch information
Showing
12 changed files
with
236 additions
and
230 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,44 @@ | ||
import { createElysia } from "@libs/elysia"; | ||
import { prismaClient } from "@libs/prismaDatabase"; | ||
import redis from "@libs/redis"; | ||
import { redis } from "@libs/redisClient"; | ||
|
||
export default createElysia() | ||
.use(redis) | ||
.get( | ||
"/", | ||
async ({ redis }) => { | ||
let blogData; | ||
const redisBlogData = await redis.get("blog.all"); | ||
if (!redisBlogData) { | ||
const getAllBlog = await prismaClient.blog | ||
.findMany({ | ||
include: { | ||
comments: true, | ||
reactions: true, | ||
}, | ||
orderBy: { | ||
createdAt: "desc", | ||
}, | ||
}) | ||
.then((data) => | ||
data.map((blog) => ({ | ||
...blog, | ||
commentsCount: blog.comments.length, | ||
reactionsCount: blog.reactions.length, | ||
})) | ||
); | ||
blogData = getAllBlog; | ||
await redis.set("blog.all", JSON.stringify(getAllBlog), 1440); | ||
} else { | ||
blogData = JSON.parse(redisBlogData); | ||
} | ||
export default createElysia().get( | ||
"/", | ||
async () => { | ||
let blogData; | ||
const redisBlogData = await redis.get("blog.all"); | ||
if (!redisBlogData) { | ||
const getAllBlog = await prismaClient.blog | ||
.findMany({ | ||
include: { | ||
comments: true, | ||
reactions: true, | ||
}, | ||
orderBy: { | ||
createdAt: "desc", | ||
}, | ||
}) | ||
.then((data) => | ||
data.map((blog) => ({ | ||
...blog, | ||
commentsCount: blog.comments.length, | ||
reactionsCount: blog.reactions.length, | ||
})) | ||
); | ||
blogData = getAllBlog; | ||
await redis.set("blog.all", JSON.stringify(getAllBlog)); | ||
} else { | ||
blogData = JSON.parse(redisBlogData); | ||
} | ||
|
||
return { | ||
status: 200, | ||
data: blogData, | ||
}; | ||
return { | ||
status: 200, | ||
data: blogData, | ||
}; | ||
}, | ||
{ | ||
detail: { | ||
tags: ["Blog"], | ||
}, | ||
{ | ||
detail: { | ||
tags: ["Blog"], | ||
}, | ||
} | ||
); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,72 @@ | ||
import { createElysia } from "@libs/elysia"; | ||
import { prismaClient } from "@libs/prismaDatabase"; | ||
import redis from "@libs/redis"; | ||
import { redis } from "@libs/redisClient"; | ||
|
||
export default createElysia() | ||
.use(redis) | ||
.get( | ||
"/:slug", | ||
async ({ params: { slug }, redis }) => { | ||
let blogData; | ||
const redisBlogData = await redis.get(`blog.${slug}`); | ||
if (!redisBlogData) { | ||
const blog = await prismaClient.blog.findUnique({ | ||
where: { | ||
slug: slug, | ||
}, | ||
include: { | ||
reactions: { | ||
include: { | ||
user: { | ||
select: { | ||
id: true, | ||
name: true, | ||
iconUrl: true, | ||
}, | ||
export default createElysia().get( | ||
"/:slug", | ||
async ({ params: { slug } }) => { | ||
let blogData; | ||
const redisBlogData = await redis.get(`blog.${slug}`); | ||
if (!redisBlogData) { | ||
const blog = await prismaClient.blog.findUnique({ | ||
where: { | ||
slug: slug, | ||
}, | ||
include: { | ||
reactions: { | ||
include: { | ||
user: { | ||
select: { | ||
id: true, | ||
name: true, | ||
iconUrl: true, | ||
}, | ||
}, | ||
}, | ||
comments: { | ||
include: { | ||
user: { | ||
select: { | ||
id: true, | ||
name: true, | ||
email: true, | ||
iconUrl: true, | ||
bannerUrl: true, | ||
location: true, | ||
headline: true, | ||
isAdmin: true, | ||
}, | ||
}, | ||
comments: { | ||
include: { | ||
user: { | ||
select: { | ||
id: true, | ||
name: true, | ||
email: true, | ||
iconUrl: true, | ||
bannerUrl: true, | ||
location: true, | ||
headline: true, | ||
isAdmin: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
blogData = blog; | ||
await redis.set(`blog.${slug}`, JSON.stringify(blog), 1440); | ||
} else { | ||
blogData = JSON.parse(redisBlogData); | ||
} | ||
|
||
if (!blogData) { | ||
return { | ||
status: 404, | ||
message: "Blog not found.", | ||
}; | ||
} | ||
}, | ||
}); | ||
blogData = blog; | ||
await redis.set(`blog.${slug}`, JSON.stringify(blog)); | ||
} else { | ||
blogData = JSON.parse(redisBlogData); | ||
} | ||
|
||
if (!blogData) { | ||
return { | ||
status: 200, | ||
data: { | ||
...blogData, | ||
commentsCount: blogData.comments.length, | ||
reactionsCount: blogData.reactions.length, | ||
}, | ||
status: 404, | ||
message: "Blog not found.", | ||
}; | ||
}, | ||
{ | ||
detail: { | ||
tags: ["Blog"], | ||
}, | ||
} | ||
); | ||
|
||
return { | ||
status: 200, | ||
data: { | ||
...blogData, | ||
commentsCount: blogData.comments.length, | ||
reactionsCount: blogData.reactions.length, | ||
}, | ||
}; | ||
}, | ||
{ | ||
detail: { | ||
tags: ["Blog"], | ||
}, | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,36 @@ | ||
import { createElysia } from "@libs/elysia"; | ||
import { prismaClient } from "@libs/prismaDatabase"; | ||
import redis from "@libs/redis"; | ||
import { redis } from "@libs/redisClient"; | ||
|
||
export default createElysia() | ||
.use(redis) | ||
.get( | ||
"/", | ||
async ({ redis }) => { | ||
let portfolioData; | ||
const redisPortfolioData = await redis.get(`portfolio.all`); | ||
if (!redisPortfolioData) { | ||
const portfolio = await prismaClient.portfolio.findMany({ | ||
include: { | ||
stacks: true, | ||
export default createElysia().get( | ||
"/", | ||
async () => { | ||
let portfolioData; | ||
const redisPortfolioData = await redis.get(`portfolio.all`); | ||
if (!redisPortfolioData) { | ||
const portfolio = await prismaClient.portfolio.findMany({ | ||
include: { | ||
stacks: true, | ||
}, | ||
orderBy: [ | ||
{ | ||
isFeatured: "desc", | ||
}, | ||
orderBy: [ | ||
{ | ||
isFeatured: "desc", | ||
}, | ||
], | ||
}); | ||
portfolioData = portfolio; | ||
await redis.set("portfolio.all", JSON.stringify(portfolio), 1440); | ||
} else { | ||
portfolioData = JSON.parse(redisPortfolioData); | ||
} | ||
return { | ||
status: 200, | ||
data: portfolioData, | ||
}; | ||
}, | ||
{ | ||
detail: { | ||
tags: ["Portfolios"], | ||
}, | ||
], | ||
}); | ||
portfolioData = portfolio; | ||
await redis.set("portfolio.all", JSON.stringify(portfolio)); | ||
} else { | ||
portfolioData = JSON.parse(redisPortfolioData); | ||
} | ||
); | ||
return { | ||
status: 200, | ||
data: portfolioData, | ||
}; | ||
}, | ||
{ | ||
detail: { | ||
tags: ["Portfolios"], | ||
}, | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,33 @@ | ||
import { createElysia } from "@libs/elysia"; | ||
import { prismaClient } from "@libs/prismaDatabase"; | ||
import redis from "@libs/redis"; | ||
import { redis } from "@libs/redisClient"; | ||
|
||
export default createElysia() | ||
.use(redis) | ||
.get( | ||
"/:id", | ||
async ({ params: { id }, redis }) => { | ||
let portfolioData; | ||
const redisPortfolioData = await redis.get(`portfolio.${id}`); | ||
if (!redisPortfolioData) { | ||
const portfolio = await prismaClient.portfolio.findUnique({ | ||
where: { id: parseInt(id) }, | ||
include: { | ||
stacks: true, | ||
}, | ||
}); | ||
portfolioData = portfolio; | ||
await redis.set(`portfolio.${id}`, JSON.stringify(portfolio), 1440); | ||
} else { | ||
portfolioData = JSON.parse(redisPortfolioData); | ||
} | ||
export default createElysia().get( | ||
"/:id", | ||
async ({ params: { id } }) => { | ||
let portfolioData; | ||
const redisPortfolioData = await redis.get(`portfolio.${id}`); | ||
if (!redisPortfolioData) { | ||
const portfolio = await prismaClient.portfolio.findUnique({ | ||
where: { id: parseInt(id) }, | ||
include: { | ||
stacks: true, | ||
}, | ||
}); | ||
portfolioData = portfolio; | ||
await redis.set(`portfolio.${id}`, JSON.stringify(portfolio)); | ||
} else { | ||
portfolioData = JSON.parse(redisPortfolioData); | ||
} | ||
|
||
return { | ||
status: 200, | ||
data: portfolioData, | ||
}; | ||
return { | ||
status: 200, | ||
data: portfolioData, | ||
}; | ||
}, | ||
{ | ||
detail: { | ||
tags: ["Portfolios"], | ||
}, | ||
{ | ||
detail: { | ||
tags: ["Portfolios"], | ||
}, | ||
} | ||
); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.