Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gatsby): provide a mechanism for plugins to use a named cache in…
…stance (#10146)
- Loading branch information
Showing
3 changed files
with
36 additions
and
8 deletions.
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,21 @@ | ||
const getCache = require(`../get-cache`) | ||
|
||
const CACHE_KEY = `__test__` | ||
|
||
test(`it returns a new cache instance`, () => { | ||
const cache = getCache(CACHE_KEY) | ||
|
||
expect(cache.get).toEqual(expect.any(Function)) | ||
expect(cache.set).toEqual(expect.any(Function)) | ||
}) | ||
|
||
test(`it retrieves already created cache instance`, async () => { | ||
const key = `some-value` | ||
const value = [`a`, `b`, `c`] | ||
const cache = getCache(CACHE_KEY) | ||
await cache.set(key, value) | ||
|
||
const other = getCache(CACHE_KEY) | ||
|
||
expect(await other.get(key)).toEqual(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
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,12 @@ | ||
const Cache = require(`./cache`) | ||
|
||
let caches = new Map() | ||
|
||
module.exports = function getCache(name) { | ||
let cache = caches.get(name) | ||
if (!cache) { | ||
cache = new Cache({ name }).init() | ||
caches.set(name, cache) | ||
} | ||
return cache | ||
} |