Skip to content

Commit c01551e

Browse files
GatsbyJS Botpieh
GatsbyJS Bot
andauthored
fix: expose ".del" function on lmdb cache (#32459) (#32464)
* expose '.del' method on cache instances, mark exposing internal cache instance to be removed in v4 * make gatsby-plugin-sharp work with legacy cache del way and new way with properly exposed del method * add .del method to ts typings * add .del method to dummy cache (for onPreInit lifecycle) (cherry picked from commit e1a1396) Co-authored-by: Michal Piechowiak <[email protected]>
1 parent 7b464be commit c01551e

File tree

6 files changed

+59
-3
lines changed

6 files changed

+59
-3
lines changed

packages/gatsby-plugin-sharp/src/gatsby-node.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ try {
2121
coreSupportsOnPluginInit = false
2222
}
2323

24+
function removeCachedValue(cache, key) {
25+
if (cache?.del) {
26+
// if cache expose ".del" method directly on public interface
27+
return cache.del(key)
28+
} else if (cache?.cache?.del) {
29+
// legacy - using internal cache instance and calling ".del" on it directly
30+
return cache.cache.del(key)
31+
}
32+
return Promise.reject(
33+
new Error(`Cache instance doesn't expose ".del" function`)
34+
)
35+
}
36+
2437
exports.onCreateDevServer = async ({ app, cache, reporter }) => {
2538
if (!_lazyJobsEnabled()) {
2639
return
@@ -53,14 +66,14 @@ exports.onCreateDevServer = async ({ app, cache, reporter }) => {
5366
} = splitOperationsByRequestedFile(cacheResult, pathOnDisk)
5467

5568
await _unstable_createJob(matchingJob, { reporter })
56-
await cache.cache.del(decodedURI)
69+
await removeCachedValue(cache, decodedURI)
5770

5871
if (jobWithRemainingOperations.args.operations.length > 0) {
5972
// There are still some operations pending for this job - replace the cached job
60-
await cache.cache.set(jobContentDigest, jobWithRemainingOperations)
73+
await cache.set(jobContentDigest, jobWithRemainingOperations)
6174
} else {
6275
// No operations left to process - purge the cache
63-
await cache.cache.del(jobContentDigest)
76+
await removeCachedValue(cache, jobContentDigest)
6477
}
6578

6679
return res.sendFile(pathOnDisk)

packages/gatsby/index.d.ts

+9
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,15 @@ export interface GatsbyCache {
12791279
* await cache.set(`unique-key`, value)
12801280
*/
12811281
set(key: string, value: any): Promise<any>
1282+
1283+
/**
1284+
* Deletes cached value
1285+
* @param {string} key Cache key
1286+
* @returns {Promise<void>} Promise resolving once key is deleted from cache
1287+
* @example
1288+
* await cache.del(`unique-key`)
1289+
*/
1290+
del(key: string): Promise<void>
12821291
}
12831292

12841293
export interface Tracing {

packages/gatsby/src/utils/api-node-helpers-docs.js

+9
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ const GatsbyCache = {
9999
* await cache.set(`unique-key`, value)
100100
*/
101101
set: true,
102+
103+
/**
104+
* Deletes cached value
105+
* @param {string} key Cache key
106+
* @returns {Promise<void>} Promise resolving once key is deleted from cache
107+
* @example
108+
* await cache.del(`unique-key`)
109+
*/
110+
del: true,
102111
};
103112

104113
/***/

packages/gatsby/src/utils/api-runner-node.js

+3
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ const getUninitializedCache = plugin => {
240240
async set() {
241241
throw new Error(message)
242242
},
243+
async del() {
244+
throw new Error(message)
245+
},
243246
}
244247
}
245248

packages/gatsby/src/utils/cache-lmdb.ts

+9
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,15 @@ export default class GatsbyCacheLmdb {
1919
public readonly name: string
2020
// Needed for plugins that want to write data to the cache directory
2121
public readonly directory: string
22+
// TODO: remove `.cache` in v4. This is compat mode - cache-manager cache implementation
23+
// expose internal cache that gives access to `.del` function that wasn't available in public
24+
// cache interface (gatsby-plugin-sharp use it to clear no longer needed data)
25+
public readonly cache: GatsbyCacheLmdb
2226

2327
constructor({ name = `db` }: { name: string }) {
2428
this.name = name
2529
this.directory = path.join(process.cwd(), `.cache/caches/${name}`)
30+
this.cache = this
2631
}
2732

2833
init(): GatsbyCacheLmdb {
@@ -59,4 +64,8 @@ export default class GatsbyCacheLmdb {
5964
await this.getDb().put(key, value)
6065
return value
6166
}
67+
68+
async del(key: string): Promise<void> {
69+
return (this.getDb().remove(key) as unknown) as Promise<void>
70+
}
6271
}

packages/gatsby/src/utils/cache.ts

+13
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ export default class GatsbyCache {
2020
public name: string
2121
public store: Store
2222
public directory: string
23+
// TODO: remove `.cache` in v4. This is compat mode - cache-manager cache implementation
24+
// expose internal cache that gives access to `.del` function that wasn't available in public
25+
// cache interface (gatsby-plugin-sharp use it to clear no longer needed data)
2326
public cache?: MultiCache
2427

2528
// @ts-ignore - set & get types are missing from fsStore?
@@ -84,4 +87,14 @@ export default class GatsbyCache {
8487
})
8588
})
8689
}
90+
91+
async del(key: string): Promise<void> {
92+
if (!this.cache) {
93+
throw new Error(
94+
`GatsbyCache wasn't initialised yet, please run the init method first`
95+
)
96+
}
97+
98+
return this.cache.del(key)
99+
}
87100
}

0 commit comments

Comments
 (0)