-
Notifications
You must be signed in to change notification settings - Fork 64
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
New Query Cache Feature (part deux) #763
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
303f317
Adds in the ability to store query results in cache. Fixes #63
jwoertink 5f14930
Move the query cache setup to Fiber so it's cleared per HTTP request.…
jwoertink 0c3ee8a
Caching queries for longer periods will be done at a lower level cach…
jwoertink 36e69ae
cache any? and select_count queryable methods. Making cache_key public
jwoertink f8d6f2a
lock lucky_cache in to specific version
jwoertink 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 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 @@ | ||
# https://crystal-lang.org/api/latest/Fiber.html | ||
class Fiber | ||
# This is stored on Fiber so it's released after each | ||
# HTTP Request. | ||
property query_cache : LuckyCache::BaseStore do | ||
if Avram.settings.query_cache_enabled | ||
LuckyCache::MemoryStore.new | ||
else | ||
LuckyCache::NullStore.new | ||
end | ||
end | ||
end |
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 |
---|---|---|
|
@@ -245,9 +245,19 @@ module Avram::Queryable(T) | |
@preloads << block | ||
end | ||
|
||
def cache_store | ||
Fiber.current.query_cache | ||
end | ||
|
||
private def cache_key : String | ||
[query.statement, query.args].join(':') | ||
end | ||
|
||
def results : Array(T) | ||
exec_query.tap do |records| | ||
preloads.each(&.call(records)) | ||
cache_store.fetch(cache_key, as: Array(T)) do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't log anything if it returns the cached value. We can't add logging to the block because that gets cached, so would it make sense to have |
||
exec_query.tap do |records| | ||
preloads.each(&.call(records)) | ||
end | ||
end | ||
end | ||
|
||
|
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.
What happens if you update some other column on users and then fetch in the same way?
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.
You'd get the cached version, not the updated version. Though, inserts and updates aren't cached here, which means if you did update a record, you'd have access to that updated record through the save operation. As of right now, the other option would be to flush all the cache.
We could do a
cache.delete(the_key)
type deal, but that goes to my question about the key. You'd have to know how it was derived... We can't just put acache_key
method on the models, because you're not necessarily caching a single model. Maybecache_key
becomes a public method?