astro-accelerator-utils 0.1.4 → 0.1.6
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/index.d.mts +1 -1
- package/index.mjs +1 -1
- package/lib/navigation.d.mts +0 -4
- package/lib/navigation.mjs +11 -7
- package/lib/postQueries.d.mts +2 -2
- package/lib/postQueries.mjs +2 -2
- package/lib/taxonomy.d.mts +0 -7
- package/lib/taxonomy.mjs +5 -3
- package/lib/v1/cache.d.mts +43 -0
- package/lib/v1/cache.mjs +100 -0
- package/lib/v1/dates.d.mts +2 -3
- package/lib/v1/dates.mjs +1 -2
- package/lib/v1/posts.d.mts +20 -4
- package/lib/v1/posts.mjs +22 -7
- package/package.json +1 -1
- package/lib/cache.d.mts +0 -30
- package/lib/cache.mjs +0 -76
package/index.d.mts
CHANGED
|
@@ -2,7 +2,7 @@ import { DateFormatter } from "./lib/v1/dates.mjs";
|
|
|
2
2
|
import { Markdown } from "./lib/v1/markdown.mjs";
|
|
3
3
|
import { Posts } from "./lib/v1/posts.mjs";
|
|
4
4
|
import { UrlFormatter } from "./lib/v1/urls.mjs";
|
|
5
|
-
import
|
|
5
|
+
import { Cache } from "./lib/v1/cache.mjs";
|
|
6
6
|
import * as PostQueries from "./lib/postQueries.mjs";
|
|
7
7
|
import * as PostFiltering from "./lib/postFiltering.mjs";
|
|
8
8
|
import * as PostOrdering from "./lib/postOrdering.mjs";
|
package/index.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
+
import { Cache } from './lib/v1/cache.mjs';
|
|
1
2
|
import { DateFormatter } from './lib/v1/dates.mjs';
|
|
2
3
|
import { Markdown } from './lib/v1/markdown.mjs';
|
|
3
4
|
import { Posts } from './lib/v1/posts.mjs';
|
|
4
5
|
import { UrlFormatter } from './lib/v1/urls.mjs';
|
|
5
6
|
|
|
6
|
-
import * as Cache from './lib/cache.mjs';
|
|
7
7
|
import * as PostQueries from './lib/postQueries.mjs';
|
|
8
8
|
import * as PostFiltering from './lib/postFiltering.mjs';
|
|
9
9
|
import * as PostOrdering from './lib/postOrdering.mjs';
|
package/lib/navigation.d.mts
CHANGED
package/lib/navigation.mjs
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
|
+
import { Posts } from './v1/posts.mjs';
|
|
2
|
+
import { Cache } from './v1/cache.mjs';
|
|
1
3
|
import * as PostQueries from './postQueries.mjs';
|
|
2
4
|
import * as PostFiltering from './postFiltering.mjs';
|
|
3
|
-
import * as Cache from './cache.mjs';
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* @typedef { import("../types/Site").Site } Site
|
|
7
8
|
* @typedef { import("../types/NavPage").NavPage } NavPage
|
|
8
9
|
*/
|
|
9
10
|
|
|
11
|
+
const cache = new Cache(200);
|
|
12
|
+
|
|
10
13
|
/**
|
|
11
14
|
*
|
|
12
15
|
* @param {URL} currentUrl
|
|
@@ -18,7 +21,7 @@ export async function getMenu (currentUrl, site, menu) {
|
|
|
18
21
|
const key = 'Navigation__getMenu';
|
|
19
22
|
|
|
20
23
|
/** @type {NavPage[]} */
|
|
21
|
-
let pages =
|
|
24
|
+
let pages = cache.getItem(key);
|
|
22
25
|
|
|
23
26
|
if (pages == null) {
|
|
24
27
|
pages = [];
|
|
@@ -35,7 +38,7 @@ export async function getMenu (currentUrl, site, menu) {
|
|
|
35
38
|
}
|
|
36
39
|
|
|
37
40
|
// Cache the result
|
|
38
|
-
|
|
41
|
+
cache.setItem(key, pages);
|
|
39
42
|
}
|
|
40
43
|
|
|
41
44
|
PostQueries.setCurrentPage(pages, currentUrl);
|
|
@@ -66,13 +69,14 @@ export function isNavPage (item) {
|
|
|
66
69
|
export async function getNavigation (currentUrl, site) {
|
|
67
70
|
|
|
68
71
|
const key = 'Navigation__getNavigation';
|
|
72
|
+
const posts = new Posts();
|
|
69
73
|
|
|
70
74
|
/** @type {NavPage[]} */
|
|
71
|
-
let pageHierarchy =
|
|
75
|
+
let pageHierarchy = cache.getItem(key);
|
|
72
76
|
|
|
73
77
|
if (pageHierarchy == null) {
|
|
74
|
-
const topLevelPages =
|
|
75
|
-
const allPages =
|
|
78
|
+
const topLevelPages = posts.root(site.subfolder).filter(PostFiltering.showInMenu);
|
|
79
|
+
const allPages = posts.all().filter(PostFiltering.showInMenu);
|
|
76
80
|
|
|
77
81
|
pageHierarchy = topLevelPages
|
|
78
82
|
.map(p => PostQueries.mapNavPage(p, site))
|
|
@@ -104,7 +108,7 @@ export async function getNavigation (currentUrl, site) {
|
|
|
104
108
|
}
|
|
105
109
|
|
|
106
110
|
// Cache the result
|
|
107
|
-
|
|
111
|
+
cache.setItem(key, pageHierarchy);
|
|
108
112
|
}
|
|
109
113
|
|
|
110
114
|
return pageHierarchy;
|
package/lib/postQueries.d.mts
CHANGED
|
@@ -5,14 +5,14 @@
|
|
|
5
5
|
export function injectFetchAll(fetcher: () => MarkdownInstance[]): void;
|
|
6
6
|
/**
|
|
7
7
|
* Replaced by Posts.all()
|
|
8
|
-
* Use Posts.all().filter(...) to filter results
|
|
8
|
+
* @deprecated Use Posts.all().filter(...) to filter results
|
|
9
9
|
* @param {PagePredicate} [filter]
|
|
10
10
|
* @returns {Promise<MarkdownInstance[]>}
|
|
11
11
|
*/
|
|
12
12
|
export function getPages(filter?: PagePredicate): Promise<MarkdownInstance[]>;
|
|
13
13
|
/**
|
|
14
14
|
* Replaced by Posts.root()
|
|
15
|
-
* Use Posts.root().filter(...) to filter results
|
|
15
|
+
* @deprecated Use Posts.root().filter(...) to filter results
|
|
16
16
|
* @param {Site} site
|
|
17
17
|
* @param {PagePredicate} [filter]
|
|
18
18
|
* @returns {Promise<MarkdownInstance[]>}
|
package/lib/postQueries.mjs
CHANGED
|
@@ -28,7 +28,7 @@ export function injectFetchAll (fetcher) {
|
|
|
28
28
|
|
|
29
29
|
/**
|
|
30
30
|
* Replaced by Posts.all()
|
|
31
|
-
* Use Posts.all().filter(...) to filter results
|
|
31
|
+
* @deprecated Use Posts.all().filter(...) to filter results
|
|
32
32
|
* @param {PagePredicate} [filter]
|
|
33
33
|
* @returns {Promise<MarkdownInstance[]>}
|
|
34
34
|
*/
|
|
@@ -51,7 +51,7 @@ export async function getPages (filter) {
|
|
|
51
51
|
|
|
52
52
|
/**
|
|
53
53
|
* Replaced by Posts.root()
|
|
54
|
-
* Use Posts.root().filter(...) to filter results
|
|
54
|
+
* @deprecated Use Posts.root().filter(...) to filter results
|
|
55
55
|
* @param {Site} site
|
|
56
56
|
* @param {PagePredicate} [filter]
|
|
57
57
|
* @returns {Promise<MarkdownInstance[]>}
|
package/lib/taxonomy.d.mts
CHANGED
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @typedef { import("../types/Site").Site } Site
|
|
3
|
-
* @typedef { import("../types/Taxonomy").Taxonomy } Taxonomy
|
|
4
|
-
* @typedef { import("../types/Taxonomy").TaxonomyEntry } TaxonomyEntry
|
|
5
|
-
* @typedef { import("../types/Taxonomy").TaxonomyLinks } TaxonomyLinks
|
|
6
|
-
* @typedef { import("../types/Astro").MarkdownInstance } MarkdownInstance
|
|
7
|
-
*/
|
|
8
1
|
/**
|
|
9
2
|
*
|
|
10
3
|
* @param {TaxonomyEntry} a
|
package/lib/taxonomy.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import { Cache } from './v1/cache.mjs';
|
|
1
2
|
import { UrlFormatter } from './v1/urls.mjs';
|
|
2
|
-
import * as Cache from './cache.mjs';
|
|
3
3
|
import * as PostQueries from './postQueries.mjs';
|
|
4
4
|
import * as PostFiltering from './postFiltering.mjs';
|
|
5
5
|
|
|
@@ -11,6 +11,8 @@ import * as PostFiltering from './postFiltering.mjs';
|
|
|
11
11
|
* @typedef { import("../types/Astro").MarkdownInstance } MarkdownInstance
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
+
const cache = new Cache(200);
|
|
15
|
+
|
|
14
16
|
/**
|
|
15
17
|
*
|
|
16
18
|
* @param {TaxonomyEntry} a
|
|
@@ -64,7 +66,7 @@ export async function getTaxonomy () {
|
|
|
64
66
|
const cacheKey = 'Global__taxonomy';
|
|
65
67
|
|
|
66
68
|
/** @type {Taxonomy} */
|
|
67
|
-
let taxonomy =
|
|
69
|
+
let taxonomy = cache.getItem(cacheKey);
|
|
68
70
|
|
|
69
71
|
if (taxonomy == null) {
|
|
70
72
|
/** @type {MarkdownInstance[]} */
|
|
@@ -119,7 +121,7 @@ export async function getTaxonomy () {
|
|
|
119
121
|
return 0;
|
|
120
122
|
});
|
|
121
123
|
|
|
122
|
-
|
|
124
|
+
cache.setItem(cacheKey, taxonomy);
|
|
123
125
|
}
|
|
124
126
|
|
|
125
127
|
return taxonomy;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export class Cache {
|
|
2
|
+
/**
|
|
3
|
+
* Constructor
|
|
4
|
+
* @param {number} maxAge
|
|
5
|
+
*/
|
|
6
|
+
constructor(maxAge: number);
|
|
7
|
+
maxAge: number;
|
|
8
|
+
/**
|
|
9
|
+
* Gets an item from the cache, falls back to supplied function
|
|
10
|
+
* @param {string} key
|
|
11
|
+
* @param {() => any} func
|
|
12
|
+
*/
|
|
13
|
+
get(key: string, func: () => any): any;
|
|
14
|
+
/**
|
|
15
|
+
* Gets an item from the cache
|
|
16
|
+
* @param {string} key
|
|
17
|
+
* @returns {Promise<any>}
|
|
18
|
+
*/
|
|
19
|
+
getItem(key: string): Promise<any>;
|
|
20
|
+
/**
|
|
21
|
+
* Adds an item to the cache
|
|
22
|
+
* @param {string} key
|
|
23
|
+
* @param {any} value
|
|
24
|
+
* @returns {Promise<void>}
|
|
25
|
+
*/
|
|
26
|
+
setItem(key: string, value: any): Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* Clears the cache
|
|
29
|
+
* @returns {Promise<void>}
|
|
30
|
+
*/
|
|
31
|
+
clear(): Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* Get's the path of the cache files
|
|
34
|
+
* @returns {string}
|
|
35
|
+
*/
|
|
36
|
+
getCachePath(): string;
|
|
37
|
+
/**
|
|
38
|
+
* Gets the file path for a cache item
|
|
39
|
+
* @param {string} key
|
|
40
|
+
* @returns {string}
|
|
41
|
+
*/
|
|
42
|
+
getItemPath(key: string): string;
|
|
43
|
+
}
|
package/lib/v1/cache.mjs
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import process from 'process';
|
|
4
|
+
|
|
5
|
+
export class Cache {
|
|
6
|
+
/**
|
|
7
|
+
* Constructor
|
|
8
|
+
* @param {number} maxAge
|
|
9
|
+
*/
|
|
10
|
+
constructor(maxAge) {
|
|
11
|
+
this.maxAge = maxAge;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Gets an item from the cache, falls back to supplied function
|
|
16
|
+
* @param {string} key
|
|
17
|
+
* @param {() => any} func
|
|
18
|
+
*/
|
|
19
|
+
get(key, func) {
|
|
20
|
+
const cached = this.getItem(key);
|
|
21
|
+
|
|
22
|
+
if (cached) {
|
|
23
|
+
return cached;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const fetched = func();
|
|
27
|
+
|
|
28
|
+
this.setItem(key, fetched);
|
|
29
|
+
|
|
30
|
+
return fetched;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Gets an item from the cache
|
|
35
|
+
* @param {string} key
|
|
36
|
+
* @returns {Promise<any>}
|
|
37
|
+
*/
|
|
38
|
+
getItem (key) {
|
|
39
|
+
const itemPath = this.getItemPath(key);
|
|
40
|
+
try {
|
|
41
|
+
|
|
42
|
+
const { mtime } = fs.statSync(itemPath);
|
|
43
|
+
|
|
44
|
+
var date_time = new Date();
|
|
45
|
+
let timeDifference = Math.abs((date_time.getTime() - mtime.getTime()) / 1000);
|
|
46
|
+
if (timeDifference < this.maxAge) {
|
|
47
|
+
console.log('Cache hit', key);
|
|
48
|
+
const content = fs.readFileSync(itemPath).toString();
|
|
49
|
+
return JSON.parse(content);
|
|
50
|
+
}
|
|
51
|
+
} catch{}
|
|
52
|
+
|
|
53
|
+
console.log('Cache miss', key);
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Adds an item to the cache
|
|
59
|
+
* @param {string} key
|
|
60
|
+
* @param {any} value
|
|
61
|
+
* @returns {Promise<void>}
|
|
62
|
+
*/
|
|
63
|
+
setItem (key, value) {
|
|
64
|
+
const itemPath = this.getItemPath(key);
|
|
65
|
+
fs.writeFileSync(itemPath, JSON.stringify(value));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Clears the cache
|
|
70
|
+
* @returns {Promise<void>}
|
|
71
|
+
*/
|
|
72
|
+
clear() {
|
|
73
|
+
const folder = this.getCachePath();
|
|
74
|
+
const files = fs.readdirSync(folder);
|
|
75
|
+
|
|
76
|
+
for(const file of files) {
|
|
77
|
+
fs.unlinkSync(path.join(folder, file));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Get's the path of the cache files
|
|
83
|
+
* @returns {string}
|
|
84
|
+
*/
|
|
85
|
+
getCachePath () {
|
|
86
|
+
const cachePath = path.join(process.cwd(), '.cache/');
|
|
87
|
+
fs.mkdirSync(cachePath, { recursive: true })
|
|
88
|
+
return cachePath;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Gets the file path for a cache item
|
|
93
|
+
* @param {string} key
|
|
94
|
+
* @returns {string}
|
|
95
|
+
*/
|
|
96
|
+
getItemPath (key) {
|
|
97
|
+
const cachePath = this.getCachePath();
|
|
98
|
+
return path.join(cachePath, key + '.json');
|
|
99
|
+
}
|
|
100
|
+
}
|
package/lib/v1/dates.d.mts
CHANGED
|
@@ -10,11 +10,10 @@ export class DateFormatter {
|
|
|
10
10
|
dateOptions: Intl.DateTimeFormatOptions;
|
|
11
11
|
/**
|
|
12
12
|
* Returns the formatted pubDate
|
|
13
|
-
* @param {
|
|
13
|
+
* @param {string | Date} date
|
|
14
14
|
* @param {string} lang
|
|
15
|
-
* @param {Site} site
|
|
16
15
|
* @returns {string}
|
|
17
16
|
*/
|
|
18
|
-
formatDate(date:
|
|
17
|
+
formatDate(date: string | Date, lang: string): string;
|
|
19
18
|
}
|
|
20
19
|
export type Site = any;
|
package/lib/v1/dates.mjs
CHANGED
package/lib/v1/posts.d.mts
CHANGED
|
@@ -1,11 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef { import("../../types/Astro").MarkdownInstance } MarkdownInstance
|
|
3
|
+
* @typedef { import("./cache.mjs").Cache } Cache
|
|
4
|
+
*/
|
|
1
5
|
export class Posts {
|
|
2
6
|
/**
|
|
3
7
|
* Constructor
|
|
4
|
-
* @param {
|
|
8
|
+
* @param {Cache} cache
|
|
9
|
+
* @param {() => MarkdownInstance[]} [fetchAll]
|
|
5
10
|
*/
|
|
6
|
-
constructor(
|
|
11
|
+
constructor(cache: Cache, fetchAll?: () => MarkdownInstance[]);
|
|
12
|
+
cache: import("./cache.mjs").Cache;
|
|
7
13
|
fetchAll: () => any;
|
|
8
14
|
allPosts: any[];
|
|
9
|
-
|
|
10
|
-
|
|
15
|
+
/**
|
|
16
|
+
* Gets all markdown posts in the site
|
|
17
|
+
* @returns {MarkdownInstance[]}
|
|
18
|
+
*/
|
|
19
|
+
all(): MarkdownInstance[];
|
|
20
|
+
/**
|
|
21
|
+
* Gets top-level markdown posts in the site
|
|
22
|
+
* @returns {MarkdownInstance[]}
|
|
23
|
+
*/
|
|
24
|
+
root(subfolder: any): MarkdownInstance[];
|
|
11
25
|
}
|
|
26
|
+
export type MarkdownInstance = import("../../types/Astro").MarkdownInstance;
|
|
27
|
+
export type Cache = import("./cache.mjs").Cache;
|
package/lib/v1/posts.mjs
CHANGED
|
@@ -1,23 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef { import("../../types/Astro").MarkdownInstance } MarkdownInstance
|
|
3
|
+
* @typedef { import("./cache.mjs").Cache } Cache
|
|
4
|
+
*/
|
|
5
|
+
|
|
1
6
|
export class Posts {
|
|
2
7
|
/**
|
|
3
8
|
* Constructor
|
|
4
|
-
* @param {
|
|
9
|
+
* @param {Cache} cache
|
|
10
|
+
* @param {() => MarkdownInstance[]} [fetchAll]
|
|
5
11
|
*/
|
|
6
|
-
constructor(fetchAll) {
|
|
12
|
+
constructor(cache, fetchAll) {
|
|
7
13
|
/* istanbul ignore next */
|
|
14
|
+
this.cache = cache;
|
|
8
15
|
this.fetchAll = fetchAll ?? function() { return import.meta.glob("/src/pages/**/*.md", { eager: true }); }
|
|
9
16
|
this.allPosts = [];
|
|
10
17
|
}
|
|
11
18
|
|
|
19
|
+
/**
|
|
20
|
+
* Gets all markdown posts in the site
|
|
21
|
+
* @returns {MarkdownInstance[]}
|
|
22
|
+
*/
|
|
12
23
|
all () {
|
|
13
|
-
|
|
14
|
-
const pageImportResult = this.fetchAll();
|
|
15
|
-
this.allPosts = Object.values(pageImportResult);
|
|
16
|
-
}
|
|
24
|
+
const key = 'cache-posts.all()';
|
|
17
25
|
|
|
18
|
-
return this.
|
|
26
|
+
return this.cache.get(key, () => {
|
|
27
|
+
const pageImportResult = this.fetchAll();
|
|
28
|
+
return Object.values(pageImportResult);
|
|
29
|
+
});
|
|
19
30
|
}
|
|
20
31
|
|
|
32
|
+
/**
|
|
33
|
+
* Gets top-level markdown posts in the site
|
|
34
|
+
* @returns {MarkdownInstance[]}
|
|
35
|
+
*/
|
|
21
36
|
root (subfolder) {
|
|
22
37
|
const isRoot = subfolder.length == 0;
|
|
23
38
|
const expectedDepth = isRoot ? 1 : 2;
|
package/package.json
CHANGED
package/lib/cache.d.mts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Get's the path of the cache files
|
|
3
|
-
* @returns {Promise<string>}
|
|
4
|
-
*/
|
|
5
|
-
export function getCachePath(): Promise<string>;
|
|
6
|
-
/**
|
|
7
|
-
* Gets the file path for a cache item
|
|
8
|
-
* @param {string} key
|
|
9
|
-
* @returns {Promise<string>}
|
|
10
|
-
*/
|
|
11
|
-
export function getItemPath(key: string): Promise<string>;
|
|
12
|
-
/**
|
|
13
|
-
* Gets an item from the cache
|
|
14
|
-
* @param {string} key
|
|
15
|
-
* @param {number} [maxAgeInSeconds]
|
|
16
|
-
* @returns {Promise<any>}
|
|
17
|
-
*/
|
|
18
|
-
export function getItem(key: string, maxAgeInSeconds?: number): Promise<any>;
|
|
19
|
-
/**
|
|
20
|
-
* Adds an item to the cache
|
|
21
|
-
* @param {string} key
|
|
22
|
-
* @param {any} value
|
|
23
|
-
* @returns {Promise<void>}
|
|
24
|
-
*/
|
|
25
|
-
export function setItem(key: string, value: any): Promise<void>;
|
|
26
|
-
/**
|
|
27
|
-
* Clears the cache
|
|
28
|
-
* @returns {Promise<void>}
|
|
29
|
-
*/
|
|
30
|
-
export function clear(): Promise<void>;
|
package/lib/cache.mjs
DELETED
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import fs from 'fs';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
import process from 'process';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Get's the path of the cache files
|
|
7
|
-
* @returns {Promise<string>}
|
|
8
|
-
*/
|
|
9
|
-
export async function getCachePath () {
|
|
10
|
-
const cachePath = path.join(process.cwd(), '.cache/');
|
|
11
|
-
await fs.promises.mkdir(cachePath, { recursive: true })
|
|
12
|
-
return cachePath;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Gets the file path for a cache item
|
|
17
|
-
* @param {string} key
|
|
18
|
-
* @returns {Promise<string>}
|
|
19
|
-
*/
|
|
20
|
-
export async function getItemPath (key) {
|
|
21
|
-
const cachePath = await getCachePath();
|
|
22
|
-
return path.join(cachePath, key + '.json');
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Gets an item from the cache
|
|
27
|
-
* @param {string} key
|
|
28
|
-
* @param {number} [maxAgeInSeconds]
|
|
29
|
-
* @returns {Promise<any>}
|
|
30
|
-
*/
|
|
31
|
-
export async function getItem (key, maxAgeInSeconds) {
|
|
32
|
-
if (maxAgeInSeconds == null) {
|
|
33
|
-
maxAgeInSeconds = 200;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const itemPath = await getItemPath(key);
|
|
37
|
-
try {
|
|
38
|
-
|
|
39
|
-
const { mtime } = await fs.promises.stat(itemPath);
|
|
40
|
-
|
|
41
|
-
var date_time = new Date();
|
|
42
|
-
let timeDifference = Math.abs((date_time.getTime() - mtime.getTime()) / 1000);
|
|
43
|
-
if (timeDifference < maxAgeInSeconds) {
|
|
44
|
-
console.log('Cache hit', key);
|
|
45
|
-
const content = fs.readFileSync(itemPath).toString();
|
|
46
|
-
return JSON.parse(content);
|
|
47
|
-
}
|
|
48
|
-
} catch{}
|
|
49
|
-
|
|
50
|
-
console.log('Cache miss', key);
|
|
51
|
-
return null;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Adds an item to the cache
|
|
56
|
-
* @param {string} key
|
|
57
|
-
* @param {any} value
|
|
58
|
-
* @returns {Promise<void>}
|
|
59
|
-
*/
|
|
60
|
-
export async function setItem (key, value) {
|
|
61
|
-
const itemPath = await getItemPath(key);
|
|
62
|
-
await fs.promises.writeFile(itemPath, JSON.stringify(value));
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Clears the cache
|
|
67
|
-
* @returns {Promise<void>}
|
|
68
|
-
*/
|
|
69
|
-
export async function clear() {
|
|
70
|
-
const folder = await getCachePath();
|
|
71
|
-
const files = fs.readdirSync(folder);
|
|
72
|
-
|
|
73
|
-
for(const file of files) {
|
|
74
|
-
fs.unlinkSync(path.join(folder, file));
|
|
75
|
-
}
|
|
76
|
-
}
|