@chriscdn/file-cache 0.0.6 → 0.0.8
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/README.md +23 -12
- package/lib/index.d.ts +1 -1
- package/lib/index.js +15 -8
- package/lib/index.js.map +1 -1
- package/package.json +3 -4
- package/src/index.ts +20 -16
package/README.md
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
# @chriscdn/file-cache
|
|
2
2
|
|
|
3
|
-
`FileCache` is a node utility for caching generated and remote files. Common use
|
|
3
|
+
`FileCache` is a node utility for caching generated and remote files. Common use
|
|
4
|
+
cases include:
|
|
4
5
|
|
|
5
|
-
- **Caching files stored in Amazon S3**: Keep a local cache of frequently
|
|
6
|
-
|
|
6
|
+
- **Caching files stored in Amazon S3**: Keep a local cache of frequently
|
|
7
|
+
accessed files to avoid repeatedly fetching them.
|
|
8
|
+
- **Thumbnail caching**: Store generated thumbnails locally for quick access
|
|
9
|
+
instead of regenerating them every time.
|
|
7
10
|
|
|
8
|
-
`FileCache` does not store state in memory, meaning your cache remains
|
|
11
|
+
`FileCache` does not store state in memory, meaning your cache remains
|
|
12
|
+
unaffected by application restarts. Cached files are automatically expired using
|
|
13
|
+
a time-to-live (TTL) policy and the file's modified date. The file's modified
|
|
14
|
+
date is updated each time the file is accessed.
|
|
9
15
|
|
|
10
16
|
## Installing
|
|
11
17
|
|
|
@@ -25,8 +31,11 @@ yarn add @chriscdn/file-cache
|
|
|
25
31
|
|
|
26
32
|
The general approach is this:
|
|
27
33
|
|
|
28
|
-
- **Create a callback** — Write an async function that takes a file path and
|
|
29
|
-
|
|
34
|
+
- **Create a callback** — Write an async function that takes a file path and
|
|
35
|
+
your custom input (like a URL), then generates or downloads the file and saves
|
|
36
|
+
it to the path.
|
|
37
|
+
- **Set up the cache** — Initialize a `FileCache` with your callback and some
|
|
38
|
+
options (like where to store files and how long to keep them).
|
|
30
39
|
- **Get a file** — Call the cache with your input. It will:
|
|
31
40
|
- Hash the input to create a unique file name.
|
|
32
41
|
- If the file is already cached and still valid, return its path.
|
|
@@ -57,15 +66,15 @@ const options: FileCacheOptions<MyCallbackArgs> = {
|
|
|
57
66
|
autoCreateCachePath: false,
|
|
58
67
|
|
|
59
68
|
// An asynchronous callback function, which is executed if no matching cached
|
|
60
|
-
// file is found. It is the responsibility of this function to use `
|
|
69
|
+
// file is found. It is the responsibility of this function to use `args`
|
|
61
70
|
// to write a file to `filePath`.
|
|
62
|
-
cb: async (filePath: string,
|
|
63
|
-
const { url } =
|
|
71
|
+
cb: async (filePath: string, args: MyCallbackArgs) => {
|
|
72
|
+
const { url } = args;
|
|
64
73
|
await downloadAndConvertToJPG(url, filePath);
|
|
65
74
|
},
|
|
66
75
|
|
|
67
76
|
// Specifies the file extension for the cached file. Can be synchronous or asynchronous.
|
|
68
|
-
ext: (
|
|
77
|
+
ext: (args: MyCallbackArgs) => ".jpg",
|
|
69
78
|
|
|
70
79
|
// Determines the time-to-live (TTL) of a cached file, in milliseconds,
|
|
71
80
|
// based on when it was last accessed.
|
|
@@ -85,8 +94,10 @@ const imageFilePath = await imageCache({
|
|
|
85
94
|
|
|
86
95
|
## Cleanup
|
|
87
96
|
|
|
88
|
-
`FileCache` instances are intended to be instantiated as singletons, persisting
|
|
89
|
-
|
|
97
|
+
`FileCache` instances are intended to be instantiated as singletons, persisting
|
|
98
|
+
throughout the lifecycle of your app. If you need to deallocate an instance, be
|
|
99
|
+
sure to call the `destroy()` method to prevent a memory leak. This does not
|
|
100
|
+
cleanup the cache directory.
|
|
90
101
|
|
|
91
102
|
## Tests
|
|
92
103
|
|
package/lib/index.d.ts
CHANGED
|
@@ -19,7 +19,7 @@ declare class FileCache<T extends Record<string, any>> {
|
|
|
19
19
|
private _ext;
|
|
20
20
|
private _intervalId;
|
|
21
21
|
private _semaphore;
|
|
22
|
-
private
|
|
22
|
+
private _cleanupGroupSemaphore;
|
|
23
23
|
constructor({ cachePath, autoCreateCachePath, cb, ext, ttl, cleanupInterval, resolveCacheFileKey, }: FileCacheOptions<T>);
|
|
24
24
|
/**
|
|
25
25
|
* Runs a cleanup pass to remove files older than 1 day from the cache.
|
package/lib/index.js
CHANGED
|
@@ -2,13 +2,13 @@ import path from "path";
|
|
|
2
2
|
import fs from "fs";
|
|
3
3
|
import { pathExists, pathExistsSync } from "path-exists";
|
|
4
4
|
import sha1 from "sha1";
|
|
5
|
-
import Semaphore from "@chriscdn/promise-semaphore";
|
|
5
|
+
import { GroupSemaphore, Semaphore } from "@chriscdn/promise-semaphore";
|
|
6
6
|
import touch from "touch";
|
|
7
7
|
import { findNuke } from "@chriscdn/find-nuke";
|
|
8
8
|
import { Duration } from "@chriscdn/duration";
|
|
9
|
-
import { rimraf } from "rimraf";
|
|
10
9
|
import { Memoize } from "@chriscdn/memoize";
|
|
11
10
|
const fsp = fs.promises;
|
|
11
|
+
const deleteFile = async (filepath) => await fs.promises.unlink(filepath);
|
|
12
12
|
class FileCache {
|
|
13
13
|
_cachePath;
|
|
14
14
|
_cb;
|
|
@@ -18,7 +18,7 @@ class FileCache {
|
|
|
18
18
|
_ext;
|
|
19
19
|
_intervalId = null;
|
|
20
20
|
_semaphore = new Semaphore();
|
|
21
|
-
|
|
21
|
+
_cleanupGroupSemaphore = new GroupSemaphore();
|
|
22
22
|
// private _fileNameResover(args:T) =>
|
|
23
23
|
constructor({ cachePath, autoCreateCachePath, cb, ext, ttl, cleanupInterval, resolveCacheFileKey, }) {
|
|
24
24
|
this._cachePath = cachePath;
|
|
@@ -50,7 +50,7 @@ class FileCache {
|
|
|
50
50
|
*/
|
|
51
51
|
async cleanup() {
|
|
52
52
|
try {
|
|
53
|
-
await this.
|
|
53
|
+
await this._cleanupGroupSemaphore.acquire("cleanup");
|
|
54
54
|
await findNuke(this._cachePath, {
|
|
55
55
|
olderThan: this._ttl,
|
|
56
56
|
verbose: true,
|
|
@@ -58,7 +58,7 @@ class FileCache {
|
|
|
58
58
|
});
|
|
59
59
|
}
|
|
60
60
|
finally {
|
|
61
|
-
this.
|
|
61
|
+
this._cleanupGroupSemaphore.release("cleanup");
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
64
|
/**
|
|
@@ -74,9 +74,11 @@ class FileCache {
|
|
|
74
74
|
async getFile(args) {
|
|
75
75
|
const filePath = await this.resolveFilePath(args);
|
|
76
76
|
try {
|
|
77
|
+
// await this._cleanupGroupSemaphore.acquire("thumbnail");
|
|
78
|
+
// await this._semaphore.acquire(filePath);
|
|
77
79
|
await Promise.all([
|
|
80
|
+
this._cleanupGroupSemaphore.acquire("getFile"),
|
|
78
81
|
this._semaphore.acquire(filePath),
|
|
79
|
-
this._cleanupSemaphore.wait(), // wait for any cleanup task to end
|
|
80
82
|
]);
|
|
81
83
|
if (await pathExists(filePath)) {
|
|
82
84
|
// fire and forget
|
|
@@ -89,6 +91,7 @@ class FileCache {
|
|
|
89
91
|
}
|
|
90
92
|
finally {
|
|
91
93
|
this._semaphore.release(filePath);
|
|
94
|
+
this._cleanupGroupSemaphore.release("getFile");
|
|
92
95
|
}
|
|
93
96
|
return filePath;
|
|
94
97
|
}
|
|
@@ -105,9 +108,12 @@ class FileCache {
|
|
|
105
108
|
async expire(args) {
|
|
106
109
|
const filePath = await this.resolveFilePath(args);
|
|
107
110
|
try {
|
|
108
|
-
await
|
|
111
|
+
await Promise.all([
|
|
112
|
+
this._cleanupGroupSemaphore.acquire("expire"),
|
|
113
|
+
this._semaphore.acquire(filePath),
|
|
114
|
+
]);
|
|
109
115
|
if (await pathExists(filePath)) {
|
|
110
|
-
await
|
|
116
|
+
await deleteFile(filePath);
|
|
111
117
|
return true;
|
|
112
118
|
}
|
|
113
119
|
else {
|
|
@@ -116,6 +122,7 @@ class FileCache {
|
|
|
116
122
|
}
|
|
117
123
|
finally {
|
|
118
124
|
this._semaphore.release(filePath);
|
|
125
|
+
this._cleanupGroupSemaphore.release("expire");
|
|
119
126
|
}
|
|
120
127
|
}
|
|
121
128
|
/**
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AACzD,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,SAAS,MAAM,6BAA6B,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AACzD,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AACxE,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAC/C,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAE9C,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAE5C,MAAM,GAAG,GAAG,EAAE,CAAC,QAAQ,CAAC;AAMxB,MAAM,UAAU,GAAG,KAAK,EAAE,QAAkB,EAAE,EAAE,CAC9C,MAAM,EAAE,CAAC,QAAQ,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;AAYrC,MAAM,SAAS;IACL,UAAU,CAAmC;IAE7C,GAAG,CAA4B;IAE/B,IAAI,CAA6B;IACjC,gBAAgB,CAAyC;IAEzD,oBAAoB,CAEH;IACjB,IAAI,CAA6B;IAEjC,WAAW,GAA0C,IAAI,CAAC;IAE1D,UAAU,GAAc,IAAI,SAAS,EAAE,CAAC;IACxC,sBAAsB,GAAmB,IAAI,cAAc,EAAE,CAAC;IAEt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AC/B,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC;SAChB,CAAC,CAAC;QAEH,OAAO,IAAI,CAAC,MAAM,CAAC;YACjB,IAAI;YACJ,GAAG;SACJ,CAAC,CAAC;IACL,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,eAAe,CAAC,IAAO;QAC3B,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC;QAElD,MAAM,QAAQ,GAAG,IAAI,CAAC,OAAO,CAC3B,IAAI,CAAC,UAAU,EACf,QAAQ,CAAC,CAAC,CAAC,EACX,QAAQ,CAAC,CAAC,CAAC,EACX,QAAQ,CAAC,CAAC,CAAC;QACX,eAAe;QACf,QAAQ,CACT,CAAC;QAEF,OAAO,QAAQ,CAAC;IAClB,CAAC;CACF;AAED,OAAO,EAAsB,SAAS,EAAiB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chriscdn/file-cache",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
4
4
|
"description": "File cache ",
|
|
5
5
|
"repository": "https://github.com/chriscdn/file-cache",
|
|
6
6
|
"author": "Christopher Meyer <chris@schwiiz.org>",
|
|
@@ -20,9 +20,8 @@
|
|
|
20
20
|
"@chriscdn/duration": "^1.0.3",
|
|
21
21
|
"@chriscdn/find-nuke": "^0.0.4",
|
|
22
22
|
"@chriscdn/memoize": "^1.0.9",
|
|
23
|
-
"@chriscdn/promise-semaphore": "^
|
|
23
|
+
"@chriscdn/promise-semaphore": "^3.0.1",
|
|
24
24
|
"path-exists": "^5.0.0",
|
|
25
|
-
"rimraf": "^6.0.1",
|
|
26
25
|
"sha1": "^1.1.1",
|
|
27
26
|
"temp": "^0.9.4",
|
|
28
27
|
"touch": "^3.1.1"
|
|
@@ -32,6 +31,6 @@
|
|
|
32
31
|
"@types/sha1": "^1.1.5",
|
|
33
32
|
"@types/touch": "^3.1.5",
|
|
34
33
|
"typescript": "^5.8.3",
|
|
35
|
-
"vitest": "^3.1.
|
|
34
|
+
"vitest": "^3.1.3"
|
|
36
35
|
}
|
|
37
36
|
}
|
package/src/index.ts
CHANGED
|
@@ -2,18 +2,22 @@ import path from "path";
|
|
|
2
2
|
import fs from "fs";
|
|
3
3
|
import { pathExists, pathExistsSync } from "path-exists";
|
|
4
4
|
import sha1 from "sha1";
|
|
5
|
-
import Semaphore from "@chriscdn/promise-semaphore";
|
|
5
|
+
import { GroupSemaphore, Semaphore } from "@chriscdn/promise-semaphore";
|
|
6
6
|
import touch from "touch";
|
|
7
7
|
import { findNuke } from "@chriscdn/find-nuke";
|
|
8
8
|
import { Duration } from "@chriscdn/duration";
|
|
9
|
-
|
|
9
|
+
|
|
10
10
|
import { Memoize } from "@chriscdn/memoize";
|
|
11
|
+
|
|
11
12
|
const fsp = fs.promises;
|
|
12
13
|
|
|
13
14
|
type DirectoryPath = string;
|
|
14
15
|
type FilePath = string;
|
|
15
16
|
type Milliseconds = number;
|
|
16
17
|
|
|
18
|
+
const deleteFile = async (filepath: FilePath) =>
|
|
19
|
+
await fs.promises.unlink(filepath);
|
|
20
|
+
|
|
17
21
|
export type FileCacheOptions<T extends Record<string, any>> = {
|
|
18
22
|
cachePath: DirectoryPath;
|
|
19
23
|
autoCreateCachePath?: boolean;
|
|
@@ -40,7 +44,7 @@ class FileCache<T extends Record<string, any>> {
|
|
|
40
44
|
private _intervalId: ReturnType<typeof setInterval> | null = null;
|
|
41
45
|
|
|
42
46
|
private _semaphore: Semaphore = new Semaphore();
|
|
43
|
-
private
|
|
47
|
+
private _cleanupGroupSemaphore: GroupSemaphore = new GroupSemaphore();
|
|
44
48
|
|
|
45
49
|
// private _fileNameResover(args:T) =>
|
|
46
50
|
|
|
@@ -91,14 +95,14 @@ class FileCache<T extends Record<string, any>> {
|
|
|
91
95
|
*/
|
|
92
96
|
async cleanup() {
|
|
93
97
|
try {
|
|
94
|
-
await this.
|
|
98
|
+
await this._cleanupGroupSemaphore.acquire("cleanup");
|
|
95
99
|
await findNuke(this._cachePath, {
|
|
96
100
|
olderThan: this._ttl,
|
|
97
101
|
verbose: true,
|
|
98
102
|
deleteEmptyDirectories: true,
|
|
99
103
|
});
|
|
100
104
|
} finally {
|
|
101
|
-
this.
|
|
105
|
+
this._cleanupGroupSemaphore.release("cleanup");
|
|
102
106
|
}
|
|
103
107
|
}
|
|
104
108
|
|
|
@@ -116,9 +120,12 @@ class FileCache<T extends Record<string, any>> {
|
|
|
116
120
|
const filePath = await this.resolveFilePath(args);
|
|
117
121
|
|
|
118
122
|
try {
|
|
123
|
+
// await this._cleanupGroupSemaphore.acquire("thumbnail");
|
|
124
|
+
// await this._semaphore.acquire(filePath);
|
|
125
|
+
|
|
119
126
|
await Promise.all([
|
|
127
|
+
this._cleanupGroupSemaphore.acquire("getFile"),
|
|
120
128
|
this._semaphore.acquire(filePath),
|
|
121
|
-
this._cleanupSemaphore.wait(), // wait for any cleanup task to end
|
|
122
129
|
]);
|
|
123
130
|
|
|
124
131
|
if (await pathExists(filePath)) {
|
|
@@ -130,6 +137,7 @@ class FileCache<T extends Record<string, any>> {
|
|
|
130
137
|
}
|
|
131
138
|
} finally {
|
|
132
139
|
this._semaphore.release(filePath);
|
|
140
|
+
this._cleanupGroupSemaphore.release("getFile");
|
|
133
141
|
}
|
|
134
142
|
|
|
135
143
|
return filePath;
|
|
@@ -150,16 +158,20 @@ class FileCache<T extends Record<string, any>> {
|
|
|
150
158
|
const filePath = await this.resolveFilePath(args);
|
|
151
159
|
|
|
152
160
|
try {
|
|
153
|
-
await
|
|
161
|
+
await Promise.all([
|
|
162
|
+
this._cleanupGroupSemaphore.acquire("expire"),
|
|
163
|
+
this._semaphore.acquire(filePath),
|
|
164
|
+
]);
|
|
154
165
|
|
|
155
166
|
if (await pathExists(filePath)) {
|
|
156
|
-
await
|
|
167
|
+
await deleteFile(filePath);
|
|
157
168
|
return true;
|
|
158
169
|
} else {
|
|
159
170
|
return false;
|
|
160
171
|
}
|
|
161
172
|
} finally {
|
|
162
173
|
this._semaphore.release(filePath);
|
|
174
|
+
this._cleanupGroupSemaphore.release("expire");
|
|
163
175
|
}
|
|
164
176
|
}
|
|
165
177
|
|
|
@@ -211,14 +223,6 @@ class FileCache<T extends Record<string, any>> {
|
|
|
211
223
|
|
|
212
224
|
return filePath;
|
|
213
225
|
}
|
|
214
|
-
|
|
215
|
-
// async resolveCreateFilePath(args: T): Promise<FilePath> {
|
|
216
|
-
// const filePath = this.resolveFilePath(args);
|
|
217
|
-
|
|
218
|
-
// await fsp.mkdir(path.dirname(filePath), { recursive: true });
|
|
219
|
-
|
|
220
|
-
// return filePath;
|
|
221
|
-
// }
|
|
222
226
|
}
|
|
223
227
|
|
|
224
228
|
export { type DirectoryPath, FileCache, type FilePath };
|