mirror of
https://github.com/hkalexling/Mango.git
synced 2025-08-03 11:25:29 -04:00
Avoid N+1 queries problem
This commit is contained in:
parent
bd2ed1b338
commit
0f94288bab
@ -72,7 +72,7 @@ class Entry
|
|||||||
def sort_title
|
def sort_title
|
||||||
sort_title_cached = @sort_title
|
sort_title_cached = @sort_title
|
||||||
return sort_title_cached if sort_title_cached
|
return sort_title_cached if sort_title_cached
|
||||||
sort_title = Storage.default.get_entry_sort_title id
|
sort_title = @book.entry_sort_title_db id
|
||||||
if sort_title
|
if sort_title
|
||||||
@sort_title = sort_title
|
@sort_title = sort_title
|
||||||
return sort_title
|
return sort_title
|
||||||
@ -89,11 +89,12 @@ class Entry
|
|||||||
@sort_title = sort_title
|
@sort_title = sort_title
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@book.entry_sort_title_cache = nil
|
||||||
@book.remove_sorted_caches [SortMethod::Auto, SortMethod::Title], username
|
@book.remove_sorted_caches [SortMethod::Auto, SortMethod::Title], username
|
||||||
end
|
end
|
||||||
|
|
||||||
def sort_title_db
|
def sort_title_db
|
||||||
Storage.default.get_entry_sort_title id
|
@book.entry_sort_title_db @id
|
||||||
end
|
end
|
||||||
|
|
||||||
def display_name
|
def display_name
|
||||||
|
@ -8,11 +8,14 @@ class Title
|
|||||||
entries : Array(Entry), title : String, id : String,
|
entries : Array(Entry), title : String, id : String,
|
||||||
encoded_title : String, mtime : Time, signature : UInt64,
|
encoded_title : String, mtime : Time, signature : UInt64,
|
||||||
entry_cover_url_cache : Hash(String, String)?
|
entry_cover_url_cache : Hash(String, String)?
|
||||||
setter entry_cover_url_cache : Hash(String, String)?
|
setter entry_cover_url_cache : Hash(String, String)?,
|
||||||
|
entry_sort_title_cache : Hash(String, String | Nil)?
|
||||||
|
|
||||||
@[YAML::Field(ignore: true)]
|
@[YAML::Field(ignore: true)]
|
||||||
@sort_title : String?
|
@sort_title : String?
|
||||||
@[YAML::Field(ignore: true)]
|
@[YAML::Field(ignore: true)]
|
||||||
|
@entry_sort_title_cache : Hash(String, String | Nil)?
|
||||||
|
@[YAML::Field(ignore: true)]
|
||||||
@entry_display_name_cache : Hash(String, String)?
|
@entry_display_name_cache : Hash(String, String)?
|
||||||
@[YAML::Field(ignore: true)]
|
@[YAML::Field(ignore: true)]
|
||||||
@entry_cover_url_cache : Hash(String, String)?
|
@entry_cover_url_cache : Hash(String, String)?
|
||||||
@ -324,6 +327,15 @@ class Title
|
|||||||
Storage.default.get_title_sort_title id
|
Storage.default.get_title_sort_title id
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def entry_sort_title_db(entry_id)
|
||||||
|
unless @entry_sort_title_cache
|
||||||
|
@entry_sort_title_cache =
|
||||||
|
Storage.default.get_entries_sort_title @entries.map &.id
|
||||||
|
end
|
||||||
|
|
||||||
|
@entry_sort_title_cache.not_nil![entry_id]?
|
||||||
|
end
|
||||||
|
|
||||||
def tags
|
def tags
|
||||||
Storage.default.get_title_tags @id
|
Storage.default.get_title_tags @id
|
||||||
end
|
end
|
||||||
|
@ -371,6 +371,22 @@ class Storage
|
|||||||
sort_title
|
sort_title
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def get_entries_sort_title(ids : Array(String))
|
||||||
|
results = Hash(String, String | Nil).new
|
||||||
|
MainFiber.run do
|
||||||
|
get_db do |db|
|
||||||
|
db.query "select id, sort_title from ids where id in (#{ids.join "," { |id| "'#{id}'" }})" do |rs|
|
||||||
|
rs.each do
|
||||||
|
id = rs.read String
|
||||||
|
sort_title = rs.read String | Nil
|
||||||
|
results[id] = sort_title
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
results
|
||||||
|
end
|
||||||
|
|
||||||
def set_entry_sort_title(entry_id : String, sort_title : String | Nil)
|
def set_entry_sort_title(entry_id : String, sort_title : String | Nil)
|
||||||
sort_title = nil if sort_title == ""
|
sort_title = nil if sort_title == ""
|
||||||
MainFiber.run do
|
MainFiber.run do
|
||||||
|
Loading…
x
Reference in New Issue
Block a user