Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: expose ".del" function on lmdb cache (#32459) #32464

Merged
merged 1 commit into from Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 16 additions & 3 deletions packages/gatsby-plugin-sharp/src/gatsby-node.js
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
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
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
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
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
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)
}
}