-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
64 additions
and
22 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 |
---|---|---|
@@ -1,2 +1,11 @@ | ||
const babelPreset = require(`babel-preset-gatsby-package`)() | ||
module.exports = require(`babel-jest`).createTransformer(babelPreset) | ||
module.exports = require(`babel-jest`).createTransformer({ | ||
...babelPreset, | ||
overrides: [ | ||
...(babelPreset.overrides || []), | ||
{ | ||
test: `**/*.ts`, | ||
plugins: [[`@babel/plugin-transform-typescript`, { isTSX: true }]], | ||
}, | ||
], | ||
}) |
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
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 |
---|---|---|
@@ -1,58 +1,75 @@ | ||
const fs = require(`fs-extra`) | ||
const manager = require(`cache-manager`) | ||
const fsStore = require(`cache-manager-fs-hash`) | ||
const path = require(`path`) | ||
import manager, { Store, StoreConfig, CachingConfig } from "cache-manager" | ||
import fs from "fs-extra" | ||
import fsStore from "cache-manager-fs-hash" | ||
import path from "path" | ||
|
||
const MAX_CACHE_SIZE = 250 | ||
const TTL = Number.MAX_SAFE_INTEGER | ||
|
||
class Cache { | ||
constructor({ name = `db`, store = fsStore } = {}) { | ||
interface ICacheProperties { | ||
name?: string | ||
store?: Store | ||
} | ||
|
||
export default class Cache { | ||
public name: string | ||
public store: Store | ||
public cache?: manager.Cache | ||
|
||
constructor({ name = `db`, store = fsStore }: ICacheProperties = {}) { | ||
this.name = name | ||
this.store = store | ||
} | ||
|
||
get directory() { | ||
get directory(): string { | ||
return path.join(process.cwd(), `.cache/caches/${this.name}`) | ||
} | ||
|
||
init() { | ||
init(): Cache { | ||
fs.ensureDirSync(this.directory) | ||
|
||
const caches = [ | ||
const configs: StoreConfig[] = [ | ||
{ | ||
store: `memory`, | ||
max: MAX_CACHE_SIZE, | ||
ttl: TTL, | ||
}, | ||
{ | ||
store: this.store, | ||
ttl: TTL, | ||
options: { | ||
path: this.directory, | ||
ttl: TTL, | ||
}, | ||
}, | ||
].map(cache => manager.caching(cache)) | ||
] | ||
|
||
const caches = configs.map(cache => manager.caching(cache)) | ||
|
||
this.cache = manager.multiCaching(caches) | ||
|
||
return this | ||
} | ||
|
||
get(key) { | ||
get<T = unknown>(key): Promise<T | undefined> { | ||
return new Promise(resolve => { | ||
this.cache.get(key, (err, res) => { | ||
// eslint-disable-next-line no-unused-expressions | ||
this.cache?.get<T>(key, (err, res) => { | ||
resolve(err ? undefined : res) | ||
}) | ||
}) | ||
} | ||
|
||
set(key, value, args = {}) { | ||
set<T>( | ||
key: string, | ||
value: T, | ||
args: CachingConfig = { ttl: TTL } | ||
): Promise<T | undefined> { | ||
return new Promise(resolve => { | ||
this.cache.set(key, value, args, err => { | ||
// eslint-disable-next-line no-unused-expressions | ||
this.cache?.set(key, value, args, err => { | ||
resolve(err ? undefined : value) | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
module.exports = Cache |
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
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