Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gatsby): Add cache-lmdb implementation (#32373)
- Loading branch information
Showing
3 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import * as path from "path" | ||
import fs from "fs-extra" | ||
import { describeWhenLMDB } from "../worker/__tests__/test-helpers" | ||
|
||
const complexObject = { | ||
key: `value`, | ||
another_key: 2, | ||
nested: { hello: `world`, foo: `bar`, nested: { super: `duper` } }, | ||
} | ||
|
||
describeWhenLMDB(`cache-lmdb`, () => { | ||
let cache | ||
|
||
beforeAll(async () => { | ||
const { default: GatsbyCacheLmdb } = await import(`../cache-lmdb`) | ||
cache = new GatsbyCacheLmdb({ name: `__test__` }) | ||
const fileDir = path.join( | ||
process.cwd(), | ||
`.cache/caches-lmdb-${process.env.JEST_WORKER_ID}` | ||
) | ||
await fs.emptyDir(fileDir) | ||
}) | ||
|
||
it(`it can be instantiated`, () => { | ||
if (!cache) fail(`cache not instantiated`) | ||
}) | ||
it(`returns cache instance with get/set methods`, () => { | ||
expect(cache.get).toEqual(expect.any(Function)) | ||
expect(cache.set).toEqual(expect.any(Function)) | ||
}) | ||
describe(`set`, () => { | ||
it(`resolves to the value it cached (string)`, () => | ||
expect(cache.set(`string`, `I'm a simple string`)).resolves.toBe( | ||
`I'm a simple string` | ||
)) | ||
it(`resolves to the value it cached (object)`, () => | ||
expect(cache.set(`object`, complexObject)).resolves.toStrictEqual( | ||
complexObject | ||
)) | ||
}) | ||
describe(`get`, () => { | ||
it(`resolves to the found value (string)`, () => | ||
expect(cache.get(`string`)).resolves.toBe(`I'm a simple string`)) | ||
it(`resolves to the found value (object)`, () => | ||
expect(cache.get(`object`)).resolves.toStrictEqual(complexObject)) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { open, RootDatabase, Database } from "lmdb-store" | ||
import path from "path" | ||
|
||
// Since the regular GatsbyCache saves to "caches" this should be "caches-lmdb" | ||
const cacheDbFile = | ||
process.env.NODE_ENV === `test` | ||
? `caches-lmdb-${ | ||
// FORCE_TEST_DATABASE_ID will be set if this gets executed in worker context | ||
// when running jest tests. JEST_WORKER_ID will be set when this gets executed directly | ||
// in test context (jest will use jest-worker internally). | ||
process.env.FORCE_TEST_DATABASE_ID ?? process.env.JEST_WORKER_ID | ||
}` | ||
: `caches-lmdb` | ||
|
||
export default class GatsbyCacheLmdb { | ||
private static store | ||
private db: Database | undefined | ||
public readonly name: string | ||
|
||
constructor({ name = `db` }: { name: string }) { | ||
this.name = name | ||
} | ||
|
||
private static getStore(): RootDatabase { | ||
if (!GatsbyCacheLmdb.store) { | ||
GatsbyCacheLmdb.store = open({ | ||
name: `root`, | ||
path: path.join(process.cwd(), `.cache/${cacheDbFile}`), | ||
compression: true, | ||
maxDbs: 200, | ||
}) | ||
} | ||
return GatsbyCacheLmdb.store | ||
} | ||
|
||
private getDb(): Database { | ||
if (!this.db) { | ||
this.db = GatsbyCacheLmdb.getStore().openDB({ | ||
name: this.name, | ||
}) | ||
} | ||
return this.db | ||
} | ||
|
||
async get<T = unknown>(key): Promise<T | undefined> { | ||
return this.getDb().get(key) | ||
} | ||
|
||
async set<T>(key: string, value: T): Promise<T | undefined> { | ||
await this.getDb().put(key, value) | ||
return value | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters