-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
chore(gatsby): Convert cache.js to TypeScript #20626
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
85d773f
Rename to .ts
ascorbic 513a5d1
Update to .ts
ascorbic 18243df
Remove lib option because we do need DOM
ascorbic e6ebadf
Merge branch 'master' into typescript-cache-js-ts
ascorbic d824c6c
Handle attempt to use non-initted cache
ascorbic 19a1fc0
Merge branch 'typescript-cache-js-ts' of github.com:gatsbyjs/gatsby i…
ascorbic 00a2404
Remove extraneous undefined
ascorbic b1b9690
Throw on uninitted cache
ascorbic 2c9dac2
Port tests to TS and add test for uninitted cache
ascorbic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 was deleted.
Oops, something went wrong.
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,83 @@ | ||
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 | ||
|
||
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(): string { | ||
return path.join(process.cwd(), `.cache/caches/${this.name}`) | ||
} | ||
|
||
init(): Cache { | ||
fs.ensureDirSync(this.directory) | ||
|
||
const configs: StoreConfig[] = [ | ||
{ | ||
store: `memory`, | ||
max: MAX_CACHE_SIZE, | ||
ttl: TTL, | ||
}, | ||
{ | ||
store: this.store, | ||
ttl: TTL, | ||
options: { | ||
path: this.directory, | ||
ttl: TTL, | ||
}, | ||
}, | ||
] | ||
|
||
const caches = configs.map(cache => manager.caching(cache)) | ||
|
||
this.cache = manager.multiCaching(caches) | ||
|
||
return this | ||
} | ||
|
||
get<T = unknown>(key): Promise<T | undefined> { | ||
return new Promise(resolve => { | ||
if (!this.cache) { | ||
throw new Error( | ||
`Cache wasn't initialised yet, please run the init method first` | ||
) | ||
} | ||
this.cache.get<T>(key, (err, res) => { | ||
resolve(err ? undefined : res) | ||
}) | ||
}) | ||
} | ||
|
||
set<T>( | ||
pvdz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
key: string, | ||
value: T, | ||
args: CachingConfig = { ttl: TTL } | ||
): Promise<T | undefined> { | ||
return new Promise(resolve => { | ||
if (!this.cache) { | ||
throw new Error( | ||
`Cache wasn't initialised yet, please run the init method first` | ||
) | ||
} | ||
this.cache.set(key, value, args, err => { | ||
resolve(err ? undefined : 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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prettier insists
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's normal :) because else it could be a return function of mockReset