@chriscdn/file-cache 0.0.10 → 0.0.11
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 +7 -19
- package/lib/index.js +4 -8
- package/lib/index.js.map +1 -1
- package/package.json +2 -3
- package/src/index.ts +3 -7
package/README.md
CHANGED
|
@@ -1,17 +1,11 @@
|
|
|
1
1
|
# @chriscdn/file-cache
|
|
2
2
|
|
|
3
|
-
`FileCache` is a node utility for caching generated and remote files. Common use
|
|
4
|
-
cases include:
|
|
3
|
+
`FileCache` is a node utility for caching generated and remote files. Common use cases include:
|
|
5
4
|
|
|
6
|
-
- **Caching files stored in Amazon S3**: Keep a local cache of frequently
|
|
7
|
-
|
|
8
|
-
- **Thumbnail caching**: Store generated thumbnails locally for quick access
|
|
9
|
-
instead of regenerating them every time.
|
|
5
|
+
- **Caching files stored in Amazon S3**: Keep a local cache of frequently accessed files to avoid repeatedly fetching them.
|
|
6
|
+
- **Thumbnail caching**: Store generated thumbnails locally for quick access instead of regenerating them every time.
|
|
10
7
|
|
|
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.
|
|
8
|
+
`FileCache` does not store state in memory, meaning your cache remains unaffected by application restarts. Cached files are automatically expired using a time-to-live (TTL) policy and the file's modified date. The file's modified date is updated each time the file is accessed.
|
|
15
9
|
|
|
16
10
|
## Installing
|
|
17
11
|
|
|
@@ -31,11 +25,8 @@ yarn add @chriscdn/file-cache
|
|
|
31
25
|
|
|
32
26
|
The general approach is this:
|
|
33
27
|
|
|
34
|
-
- **Create a callback** — Write an async function that takes a file path and
|
|
35
|
-
|
|
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).
|
|
28
|
+
- **Create a callback** — Write an async function that takes a file path and your custom input (like a URL), then generates or downloads the file and saves it to the path.
|
|
29
|
+
- **Set up the cache** — Initialize a `FileCache` with your callback and some options (like where to store files and how long to keep them).
|
|
39
30
|
- **Get a file** — Call the cache with your input. It will:
|
|
40
31
|
- Hash the input to create a unique file name.
|
|
41
32
|
- If the file is already cached and still valid, return its path.
|
|
@@ -94,10 +85,7 @@ const imageFilePath = await imageCache({
|
|
|
94
85
|
|
|
95
86
|
## Cleanup
|
|
96
87
|
|
|
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.
|
|
88
|
+
`FileCache` instances are intended to be instantiated as singletons, persisting throughout the lifecycle of your app. If you need to deallocate an instance, be sure to call the `destroy()` method to prevent a memory leak. This does not cleanup the cache directory.
|
|
101
89
|
|
|
102
90
|
## Tests
|
|
103
91
|
|
package/lib/index.js
CHANGED
|
@@ -6,7 +6,7 @@ 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 { Memoize } from "@chriscdn/memoize";
|
|
9
|
+
// import { Memoize } from "@chriscdn/memoize";
|
|
10
10
|
const fsp = fs.promises;
|
|
11
11
|
const deleteFile = async (filepath) => await fs.promises.unlink(filepath);
|
|
12
12
|
class FileCache {
|
|
@@ -41,8 +41,8 @@ class FileCache {
|
|
|
41
41
|
// fire and forget
|
|
42
42
|
this.cleanup();
|
|
43
43
|
this._intervalId = setInterval(this.cleanup.bind(this), this._cleanupInterval);
|
|
44
|
-
this.resolveFileName = Memoize(this.resolveFileName.bind(this));
|
|
45
|
-
this.resolveFilePath = Memoize(this.resolveFilePath.bind(this));
|
|
44
|
+
// this.resolveFileName = Memoize(this.resolveFileName.bind(this));
|
|
45
|
+
// this.resolveFilePath = Memoize(this.resolveFilePath.bind(this));
|
|
46
46
|
}
|
|
47
47
|
/**
|
|
48
48
|
* Runs a cleanup pass to remove files older than 1 day from the cache.
|
|
@@ -74,8 +74,6 @@ 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);
|
|
79
77
|
await Promise.all([
|
|
80
78
|
this._cleanupGroupSemaphore.acquire("getFile"),
|
|
81
79
|
this._semaphore.acquire(filePath),
|
|
@@ -158,9 +156,7 @@ class FileCache {
|
|
|
158
156
|
*/
|
|
159
157
|
async resolveFilePath(args) {
|
|
160
158
|
const fileName = await this.resolveFileName(args);
|
|
161
|
-
const filePath = path.resolve(this._cachePath, fileName[0], fileName[1], fileName[2],
|
|
162
|
-
// fileName[3],
|
|
163
|
-
fileName);
|
|
159
|
+
const filePath = path.resolve(this._cachePath, fileName[0], fileName[1], fileName[2], fileName);
|
|
164
160
|
return filePath;
|
|
165
161
|
}
|
|
166
162
|
}
|
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,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;AAE9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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chriscdn/file-cache",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.11",
|
|
4
4
|
"description": "File cache ",
|
|
5
5
|
"repository": "https://github.com/chriscdn/file-cache",
|
|
6
6
|
"author": "Christopher Meyer <chris@schwiiz.org>",
|
|
@@ -19,8 +19,7 @@
|
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"@chriscdn/duration": "^1.0.3",
|
|
21
21
|
"@chriscdn/find-nuke": "^0.0.5",
|
|
22
|
-
"@chriscdn/
|
|
23
|
-
"@chriscdn/promise-semaphore": "^3.0.1",
|
|
22
|
+
"@chriscdn/promise-semaphore": "^3.1.1",
|
|
24
23
|
"path-exists": "^5.0.0",
|
|
25
24
|
"sha1": "^1.1.1",
|
|
26
25
|
"temp": "^0.9.4",
|
package/src/index.ts
CHANGED
|
@@ -7,7 +7,7 @@ import touch from "touch";
|
|
|
7
7
|
import { findNuke } from "@chriscdn/find-nuke";
|
|
8
8
|
import { Duration } from "@chriscdn/duration";
|
|
9
9
|
|
|
10
|
-
import { Memoize } from "@chriscdn/memoize";
|
|
10
|
+
// import { Memoize } from "@chriscdn/memoize";
|
|
11
11
|
|
|
12
12
|
const fsp = fs.promises;
|
|
13
13
|
|
|
@@ -86,8 +86,8 @@ class FileCache<T extends Record<string, any>> {
|
|
|
86
86
|
this._cleanupInterval,
|
|
87
87
|
);
|
|
88
88
|
|
|
89
|
-
this.resolveFileName = Memoize(this.resolveFileName.bind(this));
|
|
90
|
-
this.resolveFilePath = Memoize(this.resolveFilePath.bind(this));
|
|
89
|
+
// this.resolveFileName = Memoize(this.resolveFileName.bind(this));
|
|
90
|
+
// this.resolveFilePath = Memoize(this.resolveFilePath.bind(this));
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
/**
|
|
@@ -121,9 +121,6 @@ class FileCache<T extends Record<string, any>> {
|
|
|
121
121
|
const filePath = await this.resolveFilePath(args);
|
|
122
122
|
|
|
123
123
|
try {
|
|
124
|
-
// await this._cleanupGroupSemaphore.acquire("thumbnail");
|
|
125
|
-
// await this._semaphore.acquire(filePath);
|
|
126
|
-
|
|
127
124
|
await Promise.all([
|
|
128
125
|
this._cleanupGroupSemaphore.acquire("getFile"),
|
|
129
126
|
this._semaphore.acquire(filePath),
|
|
@@ -218,7 +215,6 @@ class FileCache<T extends Record<string, any>> {
|
|
|
218
215
|
fileName[0],
|
|
219
216
|
fileName[1],
|
|
220
217
|
fileName[2],
|
|
221
|
-
// fileName[3],
|
|
222
218
|
fileName,
|
|
223
219
|
);
|
|
224
220
|
|