Skip to content

Commit 14367d1

Browse files
committed
Optimized job actions by caching data for improved efficiency
1 parent 04faa7c commit 14367d1

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

lib/actions/job.action.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import path from "path";
33

44
import type { GetJobsParams } from "./shared.types";
55

6+
let _jsearch: any;
7+
let _countries: any;
8+
69
export async function getJobs(params: GetJobsParams) {
710
try {
811
const {
@@ -19,12 +22,16 @@ export async function getJobs(params: GetJobsParams) {
1922
// Calculate the number of jobs to skip based on the page number and page size
2023
const skipAmount = (page - 1) * pageSize;
2124

22-
const file = path.join(process.cwd(), "content", "jsearch.json");
23-
const fileSync = readFileSync(file, "utf8");
25+
if (!_jsearch) {
26+
const file = path.join(process.cwd(), "content", "jsearch.json");
27+
const fileSync = readFileSync(file, "utf8");
28+
29+
const jsonData = JSON.parse(fileSync);
2430

25-
const jsonData = JSON.parse(fileSync);
31+
_jsearch = jsonData;
32+
}
2633

27-
const allJobs = jsonData.data || [];
34+
const allJobs = _jsearch.data || [];
2835

2936
const searchQueryRegExp = new RegExp(
3037
(searchQuery || "").toLowerCase(),
@@ -87,12 +94,16 @@ export async function getJobs(params: GetJobsParams) {
8794

8895
export async function getCountryFilters() {
8996
try {
90-
const file = path.join(process.cwd(), "content", "countries.json");
91-
const fileSync = readFileSync(file, "utf8");
97+
if (!_countries) {
98+
const file = path.join(process.cwd(), "content", "countries.json");
99+
const fileSync = readFileSync(file, "utf8");
92100

93-
const jsonData = JSON.parse(fileSync);
101+
const jsonData = JSON.parse(fileSync);
102+
103+
_countries = jsonData;
104+
}
94105

95-
const result = jsonData.map((country: any) => ({
106+
const result = _countries.map((country: any) => ({
96107
name: country.name,
97108
value: country.cca2,
98109
}));

0 commit comments

Comments
 (0)