Skip to content

Commit

Permalink
fix: expose ".del" function on lmdb cache (#32459)
Browse files Browse the repository at this point in the history
* 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)
  • Loading branch information
pieh authored Jul 21, 2021
1 parent 9595ccb commit e1a1396
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 3 deletions.
19 changes: 16 additions & 3 deletions packages/gatsby-plugin-sharp/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ try {
coreSupportsOnPluginInit = false
}

function removeCachedValue(cache, key) {
if (cache?.del) {
// if cache expose ".del" method directly on public interface
return cache.del(key)
} else if (cache?.cache?.del) {
// legacy - using internal cache instance and calling ".del" on it directly
return cache.cache.del(key)
}
return Promise.reject(
new Error(`Cache instance doesn't expose ".del" function`)
)
}

exports.onCreateDevServer = async ({ app, cache, reporter }) => {
if (!_lazyJobsEnabled()) {
return
Expand Down Expand Up @@ -53,14 +66,14 @@ exports.onCreateDevServer = async ({ app, cache, reporter }) => {
} = splitOperationsByRequestedFile(cacheResult, pathOnDisk)

await _unstable_createJob(matchingJob, { reporter })
await cache.cache.del(decodedURI)
await removeCachedValue(cache, decodedURI)

if (jobWithRemainingOperations.args.operations.length > 0) {
// There are still some operations pending for this job - replace the cached job
await cache.cache.set(jobContentDigest, jobWithRemainingOperations)
await cache.set(jobContentDigest, jobWithRemainingOperations)
} else {
// No operations left to process - purge the cache
await cache.cache.del(jobContentDigest)
await removeCachedValue(cache, jobContentDigest)
}

return res.sendFile(pathOnDisk)
Expand Down
9 changes: 9 additions & 0 deletions packages/gatsby/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,15 @@ export interface GatsbyCache {
* await cache.set(`unique-key`, value)
*/
set(key: string, value: any): Promise<any>

/**
* Deletes cached value
* @param {string} key Cache key
* @returns {Promise<void>} Promise resolving once key is deleted from cache
* @example
* await cache.del(`unique-key`)
*/
del(key: string): Promise<void>
}

export interface Tracing {
Expand Down
9 changes: 9 additions & 0 deletions packages/gatsby/src/utils/api-node-helpers-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ const GatsbyCache = {
* await cache.set(`unique-key`, value)
*/
set: true,

/**
* Deletes cached value
* @param {string} key Cache key
* @returns {Promise<void>} Promise resolving once key is deleted from cache
* @example
* await cache.del(`unique-key`)
*/
del: true,
};

/***/
Expand Down
3 changes: 3 additions & 0 deletions packages/gatsby/src/utils/api-runner-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ const getUninitializedCache = plugin => {
async set() {
throw new Error(message)
},
async del() {
throw new Error(message)
},
}
}

Expand Down
9 changes: 9 additions & 0 deletions packages/gatsby/src/utils/cache-lmdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@ export default class GatsbyCacheLmdb {
public readonly name: string
// Needed for plugins that want to write data to the cache directory
public readonly directory: string
// TODO: remove `.cache` in v4. This is compat mode - cache-manager cache implementation
// expose internal cache that gives access to `.del` function that wasn't available in public
// cache interface (gatsby-plugin-sharp use it to clear no longer needed data)
public readonly cache: GatsbyCacheLmdb

constructor({ name = `db` }: { name: string }) {
this.name = name
this.directory = path.join(process.cwd(), `.cache/caches/${name}`)
this.cache = this
}

init(): GatsbyCacheLmdb {
Expand Down Expand Up @@ -59,4 +64,8 @@ export default class GatsbyCacheLmdb {
await this.getDb().put(key, value)
return value
}

async del(key: string): Promise<void> {
return (this.getDb().remove(key) as unknown) as Promise<void>
}
}
13 changes: 13 additions & 0 deletions packages/gatsby/src/utils/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export default class GatsbyCache {
public name: string
public store: Store
public directory: string
// TODO: remove `.cache` in v4. This is compat mode - cache-manager cache implementation
// expose internal cache that gives access to `.del` function that wasn't available in public
// cache interface (gatsby-plugin-sharp use it to clear no longer needed data)
public cache?: MultiCache

// @ts-ignore - set & get types are missing from fsStore?
Expand Down Expand Up @@ -84,4 +87,14 @@ export default class GatsbyCache {
})
})
}

async del(key: string): Promise<void> {
if (!this.cache) {
throw new Error(
`GatsbyCache wasn't initialised yet, please run the init method first`
)
}

return this.cache.del(key)
}
}

0 comments on commit e1a1396

Please sign in to comment.