-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Custom cache provider #1017
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
Merged
Merged
Custom cache provider #1017
Changes from 10 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
5834b59
custom cache
huozhi 05c955d
deprecate cache comment
huozhi 9644b7e
Merge remote-tracking branch 'upstream/master' into custom-cache
huozhi 271e0e1
Merge branch 'master' into custom-cache
shuding 46a1e8f
tweak the createCache api
shuding 69ec9df
refactor createCache
shuding fb0fea6
merge master
shuding 18eac1f
simplify cache interface
shuding 90f4f55
improve tests
shuding 0b23bec
resolve conflicts
shuding 47de697
bind swr state to the cache object; simplify internal logic
shuding 37c699b
Merge branch 'master' into custom-cache
huozhi 5fcde9d
fix merge errors and add cache to deps
huozhi 35f7ec4
remove deps
shuding e5bd7d0
revert preset changes
shuding 9a610c7
refactor code
shuding 2242b85
refactor code
shuding 411e6a2
add multi-level cache test
shuding e8c21fa
add isolated cache test
shuding 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 hidden or 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,96 +1,8 @@ | ||
import { Cache as CacheType, Key, CacheListener } from './types' | ||
import hash from './libs/hash' | ||
import { Cache } from './types' | ||
|
||
export default class Cache implements CacheType { | ||
private cache: Map<string, any> | ||
private subs: CacheListener[] | ||
|
||
constructor(initialData: any = {}) { | ||
this.cache = new Map(Object.entries(initialData)) | ||
this.subs = [] | ||
} | ||
|
||
get(key: Key): any { | ||
const [_key] = this.serializeKey(key) | ||
return this.cache.get(_key) | ||
} | ||
|
||
set(key: Key, value: any): any { | ||
const [_key] = this.serializeKey(key) | ||
this.cache.set(_key, value) | ||
this.notify() | ||
} | ||
|
||
keys() { | ||
return Array.from(this.cache.keys()) | ||
} | ||
|
||
has(key: Key) { | ||
const [_key] = this.serializeKey(key) | ||
return this.cache.has(_key) | ||
} | ||
|
||
clear() { | ||
this.cache.clear() | ||
this.notify() | ||
} | ||
|
||
delete(key: Key) { | ||
const [_key] = this.serializeKey(key) | ||
this.cache.delete(_key) | ||
this.notify() | ||
} | ||
|
||
// TODO: introduce namespace for the cache | ||
serializeKey(key: Key): [string, any, string, string] { | ||
let args = null | ||
if (typeof key === 'function') { | ||
try { | ||
key = key() | ||
} catch (err) { | ||
// dependencies not ready | ||
key = '' | ||
} | ||
} | ||
|
||
if (Array.isArray(key)) { | ||
// args array | ||
args = key | ||
key = hash(key) | ||
} else { | ||
// convert null to '' | ||
key = String(key || '') | ||
} | ||
|
||
const errorKey = key ? 'err@' + key : '' | ||
const isValidatingKey = key ? 'validating@' + key : '' | ||
|
||
return [key, args, errorKey, isValidatingKey] | ||
} | ||
|
||
subscribe(listener: CacheListener) { | ||
if (typeof listener !== 'function') { | ||
throw new Error('Expected the listener to be a function.') | ||
} | ||
|
||
let isSubscribed = true | ||
this.subs.push(listener) | ||
|
||
return () => { | ||
if (!isSubscribed) return | ||
isSubscribed = false | ||
const index = this.subs.indexOf(listener) | ||
if (index > -1) { | ||
this.subs[index] = this.subs[this.subs.length - 1] | ||
this.subs.length-- | ||
} | ||
} | ||
} | ||
|
||
// Notify Cache subscribers about a change in the cache | ||
private notify() { | ||
for (let listener of this.subs) { | ||
listener() | ||
} | ||
} | ||
export function wrapCache<Data = any>(provider: Cache<Data>): Cache { | ||
// We might want to inject an extra layer on top of `provider` in the future, | ||
// such as key serialization, auto GC, etc. | ||
// For now, it's just a `Map` interface without any modifications. | ||
return provider | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,28 @@ | ||
import hash from './hash' | ||
import { Key } from '../types' | ||
|
||
export function serialize(key: Key): [string, any, string, string] { | ||
let args = null | ||
if (typeof key === 'function') { | ||
try { | ||
key = key() | ||
} catch (err) { | ||
// dependencies not ready | ||
key = '' | ||
} | ||
} | ||
|
||
if (Array.isArray(key)) { | ||
// args array | ||
args = key | ||
key = hash(key) | ||
} else { | ||
// convert null to '' | ||
key = String(key || '') | ||
} | ||
|
||
const errorKey = key ? 'err@' + key : '' | ||
const isValidatingKey = key ? 'req@' + key : '' | ||
|
||
return [key, args, errorKey, isValidatingKey] | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.