-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Adds schema caching capabilities (5s by default) #2286
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3ea3ca3
Adds schema caching capabilities (off by default)
flovilmart faaaff3
Use InMemoryCacheAdapter
flovilmart cbf3f44
Uses proper adapter to generate a cache
flovilmart e835d9f
Fix bugs when running disabled cache
flovilmart cfa9347
nits
flovilmart 190b983
nits
flovilmart 0a6eaf3
Use options object instead of boolean
flovilmart eb59295
Imrpove concurrency of loadSchema
flovilmart 5bb6c99
Adds testing with SCHEMA_CACHE_ON
flovilmart 4e8c355
Use CacheController instead of generator
flovilmart 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
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
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
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,68 @@ | ||
const MAIN_SCHEMA = "__MAIN_SCHEMA"; | ||
const SCHEMA_CACHE_PREFIX = "__SCHEMA"; | ||
const ALL_KEYS = "__ALL_KEYS"; | ||
|
||
import { randomString } from '../cryptoUtils'; | ||
|
||
export default class SchemaCache { | ||
cache: Object; | ||
|
||
constructor(cacheController, ttl = 30) { | ||
this.ttl = ttl; | ||
if (typeof ttl == 'string') { | ||
this.ttl = parseInt(ttl); | ||
} | ||
this.cache = cacheController; | ||
this.prefix = SCHEMA_CACHE_PREFIX+randomString(20); | ||
} | ||
|
||
put(key, value) { | ||
return this.cache.get(this.prefix+ALL_KEYS).then((allKeys) => { | ||
allKeys = allKeys || {}; | ||
allKeys[key] = true; | ||
return Promise.all([this.cache.put(this.prefix+ALL_KEYS, allKeys, this.ttl), this.cache.put(key, value, this.ttl)]); | ||
}); | ||
} | ||
|
||
getAllClasses() { | ||
if (!this.ttl) { | ||
return Promise.resolve(null); | ||
} | ||
return this.cache.get(this.prefix+MAIN_SCHEMA); | ||
} | ||
|
||
setAllClasses(schema) { | ||
if (!this.ttl) { | ||
return Promise.resolve(null); | ||
} | ||
return this.put(this.prefix+MAIN_SCHEMA, schema); | ||
} | ||
|
||
setOneSchema(className, schema) { | ||
if (!this.ttl) { | ||
return Promise.resolve(null); | ||
} | ||
return this.put(this.prefix+className, schema); | ||
} | ||
|
||
getOneSchema(className) { | ||
if (!this.ttl) { | ||
return Promise.resolve(null); | ||
} | ||
return this.cache.get(this.prefix+className); | ||
} | ||
|
||
clear() { | ||
// That clears all caches... | ||
let promise = Promise.resolve(); | ||
return this.cache.get(this.prefix+ALL_KEYS).then((allKeys) => { | ||
if (!allKeys) { | ||
return; | ||
} | ||
let promises = Object.keys(allKeys).map((key) => { | ||
return this.cache.del(key); | ||
}); | ||
return Promise.all(promises); | ||
}); | ||
} | ||
} |
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.
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.
If we set one schema/class it won't be available in getAllClasses() - is this a concern?
Uh oh!
There was an error while loading. Please reload this page.
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.
Not really, at that moment, the cache is just on the top of the DB queries to make things simple.
It could be further refined