-
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
Changes from all commits
303f317
5f14930
0c3ee8a
36e69ae
f8d6f2a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -214,23 +214,29 @@ module Avram::Queryable(T) | |
end | ||
|
||
def any? : Bool | ||
queryable = clone | ||
new_query = queryable.query.limit(1).select("1 AS one") | ||
results = database.query_one?(new_query.statement, args: new_query.args, queryable: schema_class.name, as: Int32) | ||
!results.nil? | ||
cache_store.fetch(cache_key, as: Bool) do | ||
queryable = clone | ||
new_query = queryable.query.limit(1).select("1 AS one") | ||
Comment on lines
+217
to
+219
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. No clue how to test the cache on this method since it would require re-opening a method within this block, but which one? Same for the |
||
results = database.query_one?(new_query.statement, args: new_query.args, queryable: schema_class.name, as: Int32) | ||
!results.nil? | ||
end | ||
end | ||
|
||
def none? : Bool | ||
!any? | ||
end | ||
|
||
def select_count : Int64 | ||
table = "(#{query.statement}) AS temp" | ||
new_query = Avram::QueryBuilder.new(table).select_count | ||
result = database.scalar new_query.statement, args: query.args, queryable: schema_class.name | ||
result.as(Int64) | ||
rescue e : DB::NoResultsError | ||
0_i64 | ||
cache_store.fetch(cache_key, as: Int64) do | ||
begin | ||
table = "(#{query.statement}) AS temp" | ||
new_query = Avram::QueryBuilder.new(table).select_count | ||
result = database.scalar new_query.statement, args: query.args, queryable: schema_class.name | ||
result.as(Int64) | ||
rescue e : DB::NoResultsError | ||
0_i64 | ||
end | ||
end | ||
end | ||
|
||
def each | ||
|
@@ -245,9 +251,19 @@ module Avram::Queryable(T) | |
@preloads << block | ||
end | ||
|
||
def cache_store | ||
Fiber.current.query_cache | ||
end | ||
|
||
def cache_key : String | ||
[query.statement, query.args].join(':') | ||
Comment on lines
+258
to
+259
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. I guess if you wanted a different cache_key, you could just override this method in your query class 🤔 |
||
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 | ||
|
||
|
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?