@chriscdn/file-cache 0.0.6 → 0.0.7
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 +13 -6
- package/lib/index.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +15 -14
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,7 +2,7 @@ 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";
|
|
@@ -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,7 +108,10 @@ 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
116
|
await rimraf(filePath);
|
|
111
117
|
return true;
|
|
@@ -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,MAA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,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.7",
|
|
4
4
|
"description": "File cache ",
|
|
5
5
|
"repository": "https://github.com/chriscdn/file-cache",
|
|
6
6
|
"author": "Christopher Meyer <chris@schwiiz.org>",
|
|
@@ -20,7 +20,7 @@
|
|
|
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
25
|
"rimraf": "^6.0.1",
|
|
26
26
|
"sha1": "^1.1.1",
|
package/src/index.ts
CHANGED
|
@@ -2,12 +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
9
|
import { rimraf } from "rimraf";
|
|
10
10
|
import { Memoize } from "@chriscdn/memoize";
|
|
11
|
+
|
|
11
12
|
const fsp = fs.promises;
|
|
12
13
|
|
|
13
14
|
type DirectoryPath = string;
|
|
@@ -40,7 +41,7 @@ class FileCache<T extends Record<string, any>> {
|
|
|
40
41
|
private _intervalId: ReturnType<typeof setInterval> | null = null;
|
|
41
42
|
|
|
42
43
|
private _semaphore: Semaphore = new Semaphore();
|
|
43
|
-
private
|
|
44
|
+
private _cleanupGroupSemaphore: GroupSemaphore = new GroupSemaphore();
|
|
44
45
|
|
|
45
46
|
// private _fileNameResover(args:T) =>
|
|
46
47
|
|
|
@@ -91,14 +92,14 @@ class FileCache<T extends Record<string, any>> {
|
|
|
91
92
|
*/
|
|
92
93
|
async cleanup() {
|
|
93
94
|
try {
|
|
94
|
-
await this.
|
|
95
|
+
await this._cleanupGroupSemaphore.acquire("cleanup");
|
|
95
96
|
await findNuke(this._cachePath, {
|
|
96
97
|
olderThan: this._ttl,
|
|
97
98
|
verbose: true,
|
|
98
99
|
deleteEmptyDirectories: true,
|
|
99
100
|
});
|
|
100
101
|
} finally {
|
|
101
|
-
this.
|
|
102
|
+
this._cleanupGroupSemaphore.release("cleanup");
|
|
102
103
|
}
|
|
103
104
|
}
|
|
104
105
|
|
|
@@ -116,9 +117,12 @@ class FileCache<T extends Record<string, any>> {
|
|
|
116
117
|
const filePath = await this.resolveFilePath(args);
|
|
117
118
|
|
|
118
119
|
try {
|
|
120
|
+
// await this._cleanupGroupSemaphore.acquire("thumbnail");
|
|
121
|
+
// await this._semaphore.acquire(filePath);
|
|
122
|
+
|
|
119
123
|
await Promise.all([
|
|
124
|
+
this._cleanupGroupSemaphore.acquire("getFile"),
|
|
120
125
|
this._semaphore.acquire(filePath),
|
|
121
|
-
this._cleanupSemaphore.wait(), // wait for any cleanup task to end
|
|
122
126
|
]);
|
|
123
127
|
|
|
124
128
|
if (await pathExists(filePath)) {
|
|
@@ -130,6 +134,7 @@ class FileCache<T extends Record<string, any>> {
|
|
|
130
134
|
}
|
|
131
135
|
} finally {
|
|
132
136
|
this._semaphore.release(filePath);
|
|
137
|
+
this._cleanupGroupSemaphore.release("getFile");
|
|
133
138
|
}
|
|
134
139
|
|
|
135
140
|
return filePath;
|
|
@@ -150,7 +155,10 @@ class FileCache<T extends Record<string, any>> {
|
|
|
150
155
|
const filePath = await this.resolveFilePath(args);
|
|
151
156
|
|
|
152
157
|
try {
|
|
153
|
-
await
|
|
158
|
+
await Promise.all([
|
|
159
|
+
this._cleanupGroupSemaphore.acquire("expire"),
|
|
160
|
+
this._semaphore.acquire(filePath),
|
|
161
|
+
]);
|
|
154
162
|
|
|
155
163
|
if (await pathExists(filePath)) {
|
|
156
164
|
await rimraf(filePath);
|
|
@@ -160,6 +168,7 @@ class FileCache<T extends Record<string, any>> {
|
|
|
160
168
|
}
|
|
161
169
|
} finally {
|
|
162
170
|
this._semaphore.release(filePath);
|
|
171
|
+
this._cleanupGroupSemaphore.release("expire");
|
|
163
172
|
}
|
|
164
173
|
}
|
|
165
174
|
|
|
@@ -211,14 +220,6 @@ class FileCache<T extends Record<string, any>> {
|
|
|
211
220
|
|
|
212
221
|
return filePath;
|
|
213
222
|
}
|
|
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
223
|
}
|
|
223
224
|
|
|
224
225
|
export { type DirectoryPath, FileCache, type FilePath };
|