astro-accelerator-utils 0.3.100 → 0.3.102
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/v1/cache.mjs +18 -1
- package/lib/v1/navigation.mjs +1 -1
- package/lib/v1/posts.mjs +3 -1
- package/package.json +1 -1
package/lib/v1/cache.mjs
CHANGED
|
@@ -2,6 +2,9 @@ import fs from 'fs';
|
|
|
2
2
|
import path from 'path';
|
|
3
3
|
import process from 'process';
|
|
4
4
|
|
|
5
|
+
/** @type {Map<string, { value: any, mtimeMs: number }>} */
|
|
6
|
+
const memoryCache = new Map();
|
|
7
|
+
|
|
5
8
|
export class Cache {
|
|
6
9
|
/**
|
|
7
10
|
* Constructor
|
|
@@ -36,6 +39,15 @@ export class Cache {
|
|
|
36
39
|
* @returns {Promise<any>}
|
|
37
40
|
*/
|
|
38
41
|
getItem (key) {
|
|
42
|
+
const memoryEntry = memoryCache.get(key);
|
|
43
|
+
if (memoryEntry) {
|
|
44
|
+
const timeDifference = Math.abs((Date.now() - memoryEntry.mtimeMs) / 1000);
|
|
45
|
+
if (timeDifference < this.maxAge) {
|
|
46
|
+
return memoryEntry.value;
|
|
47
|
+
}
|
|
48
|
+
memoryCache.delete(key);
|
|
49
|
+
}
|
|
50
|
+
|
|
39
51
|
const itemPath = this.getItemPath(key);
|
|
40
52
|
try {
|
|
41
53
|
|
|
@@ -45,7 +57,9 @@ export class Cache {
|
|
|
45
57
|
let timeDifference = Math.abs((date_time.getTime() - mtime.getTime()) / 1000);
|
|
46
58
|
if (timeDifference < this.maxAge) {
|
|
47
59
|
const content = fs.readFileSync(itemPath).toString();
|
|
48
|
-
|
|
60
|
+
const value = JSON.parse(content);
|
|
61
|
+
memoryCache.set(key, { value, mtimeMs: mtime.getTime() });
|
|
62
|
+
return value;
|
|
49
63
|
}
|
|
50
64
|
} catch{}
|
|
51
65
|
|
|
@@ -62,6 +76,7 @@ export class Cache {
|
|
|
62
76
|
setItem (key, value) {
|
|
63
77
|
const itemPath = this.getItemPath(key);
|
|
64
78
|
fs.writeFileSync(itemPath, JSON.stringify(value));
|
|
79
|
+
memoryCache.set(key, { value, mtimeMs: Date.now() });
|
|
65
80
|
}
|
|
66
81
|
|
|
67
82
|
/**
|
|
@@ -69,6 +84,8 @@ export class Cache {
|
|
|
69
84
|
* @returns {Promise<void>}
|
|
70
85
|
*/
|
|
71
86
|
clear() {
|
|
87
|
+
memoryCache.clear();
|
|
88
|
+
|
|
72
89
|
const folder = this.getCachePath();
|
|
73
90
|
const files = fs.readdirSync(folder);
|
|
74
91
|
|
package/lib/v1/navigation.mjs
CHANGED
package/lib/v1/posts.mjs
CHANGED
|
@@ -24,10 +24,12 @@ export class Posts {
|
|
|
24
24
|
all () {
|
|
25
25
|
const key = 'v1_posts.all';
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
const posts = this.cache.get(key, () => {
|
|
28
28
|
const pageImportResult = this.fetchAll();
|
|
29
29
|
return Object.values(pageImportResult);
|
|
30
30
|
});
|
|
31
|
+
|
|
32
|
+
return [...posts];
|
|
31
33
|
}
|
|
32
34
|
|
|
33
35
|
/**
|