jbilcke-hf HF Staff commited on
Commit
825aa76
·
1 Parent(s): 16e4326

add performance improvement

Browse files
Files changed (2) hide show
  1. src/core/getPost.mts +18 -0
  2. src/core/readPostFiles.mts +12 -0
src/core/getPost.mts CHANGED
@@ -4,13 +4,31 @@ import { postDirFilePath } from "../config.mts"
4
  import { readPostFile } from "./readPostFile.mts"
5
  import { Post } from "../types.mts"
6
 
 
 
7
  export const getPost = async (appId: string, postId: string): Promise<Post> => {
 
 
 
 
 
 
 
 
 
 
 
 
8
  const postFileName = `${appId}_${postId}.json`
9
 
10
  const postFilePath = path.join(postDirFilePath, postFileName)
11
 
12
  try {
13
  const post = await readPostFile(postFilePath)
 
 
 
 
14
  return post
15
  } catch (err) {
16
  throw new Error(`couldn't find post ${postId} for app ${appId}`)
 
4
  import { readPostFile } from "./readPostFile.mts"
5
  import { Post } from "../types.mts"
6
 
7
+ const cache: { [key: string]: { timestamp: number, post: Post }} = {};
8
+
9
  export const getPost = async (appId: string, postId: string): Promise<Post> => {
10
+
11
+ /*
12
+ const cacheKey = `${appId}_${postId}`;
13
+ const now = Date.now();
14
+
15
+ // this is a tight cache (5 seconds)
16
+ if (cache[cacheKey] && (now - cache[cacheKey].timestamp) < (5 * 1000)) {
17
+ // return the cached data if it is less than 5 minutes old
18
+ return cache[cacheKey].post;
19
+ }
20
+ */
21
+
22
  const postFileName = `${appId}_${postId}.json`
23
 
24
  const postFilePath = path.join(postDirFilePath, postFileName)
25
 
26
  try {
27
  const post = await readPostFile(postFilePath)
28
+
29
+ // if successful, cache the post and its fetch time
30
+ // cache[cacheKey] = { timestamp: now, post };
31
+
32
  return post
33
  } catch (err) {
34
  throw new Error(`couldn't find post ${postId} for app ${appId}`)
src/core/readPostFiles.mts CHANGED
@@ -4,8 +4,17 @@ import { promises as fs } from "node:fs"
4
  import { Post } from "../types.mts"
5
  import { readPostFile } from "./readPostFile.mts"
6
 
 
 
7
  export const readPostFiles = async (postDirFilePath: string, appId?: string): Promise<Post[]> => {
8
 
 
 
 
 
 
 
 
9
  let postFiles: string[] = []
10
  try {
11
  const filesInDir = await fs.readdir(postDirFilePath)
@@ -38,5 +47,8 @@ export const readPostFiles = async (postDirFilePath: string, appId?: string): Pr
38
  }
39
  }
40
 
 
 
 
41
  return posts
42
  }
 
4
  import { Post } from "../types.mts"
5
  import { readPostFile } from "./readPostFile.mts"
6
 
7
+ const cache = {} as { [directory: string]: { timestamp: number, files: Post[] } };
8
+
9
  export const readPostFiles = async (postDirFilePath: string, appId?: string): Promise<Post[]> => {
10
 
11
+ const now = Date.now()
12
+
13
+ if (cache[postDirFilePath] && (now - cache[postDirFilePath].timestamp) < (5 * 60 * 1000)) {
14
+ // return cached data if it's less than 5 minutes old
15
+ return cache[postDirFilePath].files
16
+ }
17
+
18
  let postFiles: string[] = []
19
  try {
20
  const filesInDir = await fs.readdir(postDirFilePath)
 
47
  }
48
  }
49
 
50
+ // store results in cache with current timestamp
51
+ cache[postDirFilePath] = { timestamp: now, files: posts }
52
+
53
  return posts
54
  }