@chriscdn/file-cache 0.0.5 → 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 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 cases include:
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 accessed files to avoid repeatedly fetching them.
6
- - **Thumbnail caching**: Store generated thumbnails locally for quick access instead of regenerating them every time.
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 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.
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 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).
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.
@@ -53,16 +62,19 @@ const options: FileCacheOptions<MyCallbackArgs> = {
53
62
  // anything else in this directory.
54
63
  cachePath: "/some/path/on/your/filesystem/",
55
64
 
65
+ // optional, automatically create cachePath if it doesn't exist
66
+ autoCreateCachePath: false,
67
+
56
68
  // An asynchronous callback function, which is executed if no matching cached
57
- // file is found. It is the responsibility of this function to use `context`
69
+ // file is found. It is the responsibility of this function to use `args`
58
70
  // to write a file to `filePath`.
59
- cb: async (filePath: string, context: MyCallbackArgs) => {
60
- const { url } = context;
71
+ cb: async (filePath: string, args: MyCallbackArgs) => {
72
+ const { url } = args;
61
73
  await downloadAndConvertToJPG(url, filePath);
62
74
  },
63
75
 
64
76
  // Specifies the file extension for the cached file. Can be synchronous or asynchronous.
65
- ext: (context: MyCallbackArgs) => ".jpg",
77
+ ext: (args: MyCallbackArgs) => ".jpg",
66
78
 
67
79
  // Determines the time-to-live (TTL) of a cached file, in milliseconds,
68
80
  // based on when it was last accessed.
@@ -82,7 +94,10 @@ const imageFilePath = await imageCache({
82
94
 
83
95
  ## Cleanup
84
96
 
85
- `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.
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.
86
101
 
87
102
  ## Tests
88
103
 
package/lib/index.d.ts CHANGED
@@ -4,21 +4,23 @@ type Milliseconds = number;
4
4
  export type FileCacheOptions<T extends Record<string, any>> = {
5
5
  cachePath: DirectoryPath;
6
6
  autoCreateCachePath?: boolean;
7
- cb: (filePath: FilePath, context: T, cache: FileCache<T>) => Promise<void>;
8
- ext: (context: T) => string | Promise<string>;
7
+ cb: (filePath: FilePath, args: T, cache: FileCache<T>) => Promise<void>;
8
+ ext: (args: T) => string | Promise<string>;
9
+ resolveCacheFileKey?: (args: T) => string | Promise<string>;
9
10
  ttl: Milliseconds;
10
11
  cleanupInterval?: Milliseconds;
11
12
  };
12
13
  declare class FileCache<T extends Record<string, any>> {
13
14
  private _cachePath;
14
15
  private _cb;
15
- private _ext;
16
16
  private _ttl;
17
17
  private _cleanupInterval;
18
+ private _resolveCacheFileKey;
19
+ private _ext;
18
20
  private _intervalId;
19
21
  private _semaphore;
20
- private _cleanupSemaphore;
21
- constructor({ cachePath, autoCreateCachePath, cb, ext, ttl, cleanupInterval }: FileCacheOptions<T>);
22
+ private _cleanupGroupSemaphore;
23
+ constructor({ cachePath, autoCreateCachePath, cb, ext, ttl, cleanupInterval, resolveCacheFileKey, }: FileCacheOptions<T>);
22
24
  /**
23
25
  * Runs a cleanup pass to remove files older than 1 day from the cache.
24
26
  * This is called periodically, but can be triggered manually.
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";
@@ -12,19 +12,23 @@ const fsp = fs.promises;
12
12
  class FileCache {
13
13
  _cachePath;
14
14
  _cb;
15
- _ext;
16
15
  _ttl;
17
16
  _cleanupInterval;
17
+ _resolveCacheFileKey;
18
+ _ext;
18
19
  _intervalId = null;
19
20
  _semaphore = new Semaphore();
20
- _cleanupSemaphore = new Semaphore();
21
- constructor({ cachePath, autoCreateCachePath, cb, ext, ttl, cleanupInterval }) {
21
+ _cleanupGroupSemaphore = new GroupSemaphore();
22
+ // private _fileNameResover(args:T) =>
23
+ constructor({ cachePath, autoCreateCachePath, cb, ext, ttl, cleanupInterval, resolveCacheFileKey, }) {
22
24
  this._cachePath = cachePath;
23
25
  this._cb = cb;
24
26
  this._ext = ext;
25
27
  this._ttl = ttl;
26
28
  this._cleanupInterval = cleanupInterval ??
27
29
  Duration.toMilliseconds({ days: 1 });
30
+ this._resolveCacheFileKey = resolveCacheFileKey ??
31
+ ((args) => sha1(JSON.stringify(args)));
28
32
  if ((pathExistsSync(cachePath))) {
29
33
  // we're good
30
34
  }
@@ -46,7 +50,7 @@ class FileCache {
46
50
  */
47
51
  async cleanup() {
48
52
  try {
49
- await this._cleanupSemaphore.acquire();
53
+ await this._cleanupGroupSemaphore.acquire("cleanup");
50
54
  await findNuke(this._cachePath, {
51
55
  olderThan: this._ttl,
52
56
  verbose: true,
@@ -54,7 +58,7 @@ class FileCache {
54
58
  });
55
59
  }
56
60
  finally {
57
- this._cleanupSemaphore.release();
61
+ this._cleanupGroupSemaphore.release("cleanup");
58
62
  }
59
63
  }
60
64
  /**
@@ -70,9 +74,11 @@ class FileCache {
70
74
  async getFile(args) {
71
75
  const filePath = await this.resolveFilePath(args);
72
76
  try {
77
+ // await this._cleanupGroupSemaphore.acquire("thumbnail");
78
+ // await this._semaphore.acquire(filePath);
73
79
  await Promise.all([
80
+ this._cleanupGroupSemaphore.acquire("getFile"),
74
81
  this._semaphore.acquire(filePath),
75
- this._cleanupSemaphore.wait(), // wait for any cleanup task to end
76
82
  ]);
77
83
  if (await pathExists(filePath)) {
78
84
  // fire and forget
@@ -85,6 +91,7 @@ class FileCache {
85
91
  }
86
92
  finally {
87
93
  this._semaphore.release(filePath);
94
+ this._cleanupGroupSemaphore.release("getFile");
88
95
  }
89
96
  return filePath;
90
97
  }
@@ -101,7 +108,10 @@ class FileCache {
101
108
  async expire(args) {
102
109
  const filePath = await this.resolveFilePath(args);
103
110
  try {
104
- await this._semaphore.acquire(filePath);
111
+ await Promise.all([
112
+ this._cleanupGroupSemaphore.acquire("expire"),
113
+ this._semaphore.acquire(filePath),
114
+ ]);
105
115
  if (await pathExists(filePath)) {
106
116
  await rimraf(filePath);
107
117
  return true;
@@ -112,6 +122,7 @@ class FileCache {
112
122
  }
113
123
  finally {
114
124
  this._semaphore.release(filePath);
125
+ this._cleanupGroupSemaphore.release("expire");
115
126
  }
116
127
  }
117
128
  /**
@@ -130,10 +141,13 @@ class FileCache {
130
141
  * generating a hash from the arguments to create a unique file name.
131
142
  */
132
143
  async resolveFileName(args) {
133
- const baseName = sha1(JSON.stringify(args));
144
+ const [name, ext] = await Promise.all([
145
+ this._resolveCacheFileKey(args),
146
+ this._ext(args),
147
+ ]);
134
148
  return path.format({
135
- name: baseName,
136
- ext: await this._ext(args),
149
+ name,
150
+ ext,
137
151
  });
138
152
  }
139
153
  /**
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;AACpD,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAC/C,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,MAAM,GAAG,GAAG,EAAE,CAAC,QAAQ,CAAC;AAexB,MAAM,SAAS;IACL,UAAU,CAAmC;IAE7C,GAAG,CAA4B;IAC/B,IAAI,CAA6B;IACjC,IAAI,CAA6B;IACjC,gBAAgB,CAAyC;IAEzD,WAAW,GAA0C,IAAI,CAAC;IAE1D,UAAU,GAAc,IAAI,SAAS,EAAE,CAAC;IACxC,iBAAiB,GAAc,IAAI,SAAS,EAAE,CAAC;IAEvD,YACE,EAAE,SAAS,EAAE,mBAAmB,EAAE,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,eAAe,EAC1C;QAErB,I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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.5",
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": "^2.0.12",
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;
@@ -17,8 +18,9 @@ type Milliseconds = number;
17
18
  export type FileCacheOptions<T extends Record<string, any>> = {
18
19
  cachePath: DirectoryPath;
19
20
  autoCreateCachePath?: boolean;
20
- cb: (filePath: FilePath, context: T, cache: FileCache<T>) => Promise<void>;
21
- ext: (context: T) => string | Promise<string>;
21
+ cb: (filePath: FilePath, args: T, cache: FileCache<T>) => Promise<void>;
22
+ ext: (args: T) => string | Promise<string>;
23
+ resolveCacheFileKey?: (args: T) => string | Promise<string>;
22
24
  ttl: Milliseconds;
23
25
  cleanupInterval?: Milliseconds;
24
26
  };
@@ -27,18 +29,32 @@ class FileCache<T extends Record<string, any>> {
27
29
  private _cachePath: FileCacheOptions<T>["cachePath"];
28
30
 
29
31
  private _cb: FileCacheOptions<T>["cb"];
30
- private _ext: FileCacheOptions<T>["ext"];
32
+
31
33
  private _ttl: FileCacheOptions<T>["ttl"];
32
34
  private _cleanupInterval: FileCacheOptions<T>["cleanupInterval"];
33
35
 
36
+ private _resolveCacheFileKey: Required<
37
+ FileCacheOptions<T>
38
+ >["resolveCacheFileKey"];
39
+ private _ext: FileCacheOptions<T>["ext"];
40
+
34
41
  private _intervalId: ReturnType<typeof setInterval> | null = null;
35
42
 
36
43
  private _semaphore: Semaphore = new Semaphore();
37
- private _cleanupSemaphore: Semaphore = new Semaphore();
44
+ private _cleanupGroupSemaphore: GroupSemaphore = new GroupSemaphore();
45
+
46
+ // private _fileNameResover(args:T) =>
38
47
 
39
48
  constructor(
40
- { cachePath, autoCreateCachePath, cb, ext, ttl, cleanupInterval }:
41
- FileCacheOptions<T>,
49
+ {
50
+ cachePath,
51
+ autoCreateCachePath,
52
+ cb,
53
+ ext,
54
+ ttl,
55
+ cleanupInterval,
56
+ resolveCacheFileKey,
57
+ }: FileCacheOptions<T>,
42
58
  ) {
43
59
  this._cachePath = cachePath;
44
60
  this._cb = cb;
@@ -47,6 +63,9 @@ class FileCache<T extends Record<string, any>> {
47
63
  this._cleanupInterval = cleanupInterval ??
48
64
  Duration.toMilliseconds({ days: 1 });
49
65
 
66
+ this._resolveCacheFileKey = resolveCacheFileKey ??
67
+ ((args: T) => sha1(JSON.stringify(args)));
68
+
50
69
  if ((pathExistsSync(cachePath))) {
51
70
  // we're good
52
71
  } else if (autoCreateCachePath) {
@@ -73,14 +92,14 @@ class FileCache<T extends Record<string, any>> {
73
92
  */
74
93
  async cleanup() {
75
94
  try {
76
- await this._cleanupSemaphore.acquire();
95
+ await this._cleanupGroupSemaphore.acquire("cleanup");
77
96
  await findNuke(this._cachePath, {
78
97
  olderThan: this._ttl,
79
98
  verbose: true,
80
99
  deleteEmptyDirectories: true,
81
100
  });
82
101
  } finally {
83
- this._cleanupSemaphore.release();
102
+ this._cleanupGroupSemaphore.release("cleanup");
84
103
  }
85
104
  }
86
105
 
@@ -98,9 +117,12 @@ class FileCache<T extends Record<string, any>> {
98
117
  const filePath = await this.resolveFilePath(args);
99
118
 
100
119
  try {
120
+ // await this._cleanupGroupSemaphore.acquire("thumbnail");
121
+ // await this._semaphore.acquire(filePath);
122
+
101
123
  await Promise.all([
124
+ this._cleanupGroupSemaphore.acquire("getFile"),
102
125
  this._semaphore.acquire(filePath),
103
- this._cleanupSemaphore.wait(), // wait for any cleanup task to end
104
126
  ]);
105
127
 
106
128
  if (await pathExists(filePath)) {
@@ -112,6 +134,7 @@ class FileCache<T extends Record<string, any>> {
112
134
  }
113
135
  } finally {
114
136
  this._semaphore.release(filePath);
137
+ this._cleanupGroupSemaphore.release("getFile");
115
138
  }
116
139
 
117
140
  return filePath;
@@ -132,7 +155,10 @@ class FileCache<T extends Record<string, any>> {
132
155
  const filePath = await this.resolveFilePath(args);
133
156
 
134
157
  try {
135
- await this._semaphore.acquire(filePath);
158
+ await Promise.all([
159
+ this._cleanupGroupSemaphore.acquire("expire"),
160
+ this._semaphore.acquire(filePath),
161
+ ]);
136
162
 
137
163
  if (await pathExists(filePath)) {
138
164
  await rimraf(filePath);
@@ -142,6 +168,7 @@ class FileCache<T extends Record<string, any>> {
142
168
  }
143
169
  } finally {
144
170
  this._semaphore.release(filePath);
171
+ this._cleanupGroupSemaphore.release("expire");
145
172
  }
146
173
  }
147
174
 
@@ -162,11 +189,14 @@ class FileCache<T extends Record<string, any>> {
162
189
  * generating a hash from the arguments to create a unique file name.
163
190
  */
164
191
  private async resolveFileName(args: T): Promise<string> {
165
- const baseName = sha1(JSON.stringify(args));
192
+ const [name, ext] = await Promise.all([
193
+ this._resolveCacheFileKey(args),
194
+ this._ext(args),
195
+ ]);
166
196
 
167
197
  return path.format({
168
- name: baseName,
169
- ext: await this._ext(args),
198
+ name,
199
+ ext,
170
200
  });
171
201
  }
172
202
 
@@ -190,14 +220,6 @@ class FileCache<T extends Record<string, any>> {
190
220
 
191
221
  return filePath;
192
222
  }
193
-
194
- // async resolveCreateFilePath(args: T): Promise<FilePath> {
195
- // const filePath = this.resolveFilePath(args);
196
-
197
- // await fsp.mkdir(path.dirname(filePath), { recursive: true });
198
-
199
- // return filePath;
200
- // }
201
223
  }
202
224
 
203
225
  export { type DirectoryPath, FileCache, type FilePath };