mirror of
https://github.com/hkalexling/Mango.git
synced 2025-08-02 10:55:30 -04:00
Cache sorted titles
This commit is contained in:
parent
1ec8dcbfda
commit
1cd777d27d
@ -1,6 +1,7 @@
|
||||
require "digest"
|
||||
|
||||
require "./entry"
|
||||
require "./title"
|
||||
require "./types"
|
||||
|
||||
# Base class for an entry in the LRU cache.
|
||||
@ -81,6 +82,31 @@ class SortedEntriesCacheEntry < CacheEntry(Array(String), Array(Entry))
|
||||
end
|
||||
end
|
||||
|
||||
class SortedTitlesCacheEntry < CacheEntry(Array(String), Array(Title))
|
||||
def self.to_save_t(value : Array(Title))
|
||||
value.map &.id
|
||||
end
|
||||
|
||||
def self.to_return_t(value : Array(String))
|
||||
value.map { |title_id| Library.default.title_hash[title_id].not_nil! }
|
||||
end
|
||||
|
||||
def instance_size
|
||||
instance_sizeof(SortedTitlesCacheEntry) + # sizeof itself
|
||||
instance_sizeof(String) + @key.bytesize + # allocated memory for @key
|
||||
@value.size * (instance_sizeof(String) + sizeof(String)) +
|
||||
@value.sum(&.bytesize) # elements in Array(String)
|
||||
end
|
||||
|
||||
def self.gen_key(username : String, titles : Array(Title), opt : SortOptions?)
|
||||
titles_sig = Digest::SHA1.hexdigest (titles.map &.id).to_s
|
||||
user_context = opt && opt.method == SortMethod::Progress ? username : ""
|
||||
sig = Digest::SHA1.hexdigest (titles_sig + user_context +
|
||||
(opt ? opt.to_tuple.to_s : "nil"))
|
||||
"#{sig}:sorted_titles"
|
||||
end
|
||||
end
|
||||
|
||||
class String
|
||||
def instance_size
|
||||
instance_sizeof(String) + bytesize
|
||||
@ -101,14 +127,17 @@ struct Tuple(*T)
|
||||
end
|
||||
end
|
||||
|
||||
alias CacheableType = Array(Entry) | String | Tuple(String, Int32)
|
||||
alias CacheableType = Array(Entry) | Array(Title) | String | Tuple(String, Int32)
|
||||
alias CacheEntryType = SortedEntriesCacheEntry |
|
||||
SortedTitlesCacheEntry |
|
||||
CacheEntry(String, String) |
|
||||
CacheEntry(Tuple(String, Int32), Tuple(String, Int32))
|
||||
|
||||
def generate_cache_entry(key : String, value : CacheableType)
|
||||
if value.is_a? Array(Entry)
|
||||
SortedEntriesCacheEntry.new key, value
|
||||
elsif value.is_a? Array(Title)
|
||||
SortedTitlesCacheEntry.new key, value
|
||||
else
|
||||
CacheEntry(typeof(value), typeof(value)).new key, value
|
||||
end
|
||||
|
@ -89,13 +89,7 @@ class Entry
|
||||
@sort_title = sort_title
|
||||
end
|
||||
|
||||
[false, true].each do |ascend|
|
||||
[SortMethod::Auto, SortMethod::Title].each do |sort_method|
|
||||
sorted_entries_cache_key = SortedEntriesCacheEntry.gen_key @book.id,
|
||||
username, @book.entries, SortOptions.new(sort_method, ascend)
|
||||
LRUCache.invalidate sorted_entries_cache_key
|
||||
end
|
||||
end
|
||||
@book.remove_sorted_caches [SortMethod::Auto, SortMethod::Title], username
|
||||
end
|
||||
|
||||
def sort_title_db
|
||||
@ -213,11 +207,7 @@ class Entry
|
||||
@book.parents.each do |parent|
|
||||
LRUCache.invalidate "#{parent.id}:#{username}:progress_sum"
|
||||
end
|
||||
[false, true].each do |ascend|
|
||||
sorted_entries_cache_key = SortedEntriesCacheEntry.gen_key @book.id,
|
||||
username, @book.entries, SortOptions.new(SortMethod::Progress, ascend)
|
||||
LRUCache.invalidate sorted_entries_cache_key
|
||||
end
|
||||
@book.remove_sorted_caches [SortMethod::Progress], username
|
||||
|
||||
TitleInfo.new @book.dir do |info|
|
||||
if info.progress[username]?.nil?
|
||||
|
@ -317,14 +317,7 @@ class Title
|
||||
@sort_title = sort_title
|
||||
end
|
||||
|
||||
[false, true].each do |ascend|
|
||||
[SortMethod::Auto, SortMethod::Title].each do |sort_method|
|
||||
sorted_entries_cache_key =
|
||||
SortedEntriesCacheEntry.gen_key @id, username, @entries,
|
||||
SortOptions.new(sort_method, ascend)
|
||||
LRUCache.invalidate sorted_entries_cache_key
|
||||
end
|
||||
end
|
||||
remove_sorted_caches [SortMethod::Auto, SortMethod::Title], username
|
||||
end
|
||||
|
||||
def sort_title_db
|
||||
@ -605,17 +598,32 @@ class Title
|
||||
zip + titles.flat_map &.deep_entries_with_date_added
|
||||
end
|
||||
|
||||
def remove_sorted_caches(sort_methods : Array(SortMethod), username : String)
|
||||
[false, true].each do |ascend|
|
||||
sort_methods.each do |sort_method|
|
||||
sorted_entries_cache_key =
|
||||
SortedEntriesCacheEntry.gen_key @id, username, @entries,
|
||||
SortOptions.new(sort_method, ascend)
|
||||
LRUCache.invalidate sorted_entries_cache_key
|
||||
end
|
||||
end
|
||||
parents.each do |parent|
|
||||
[false, true].each do |ascend|
|
||||
sort_methods.each do |sort_method|
|
||||
sorted_titles_cache_key = SortedTitlesCacheEntry.gen_key username,
|
||||
parent.titles, SortOptions.new(sort_method, ascend)
|
||||
LRUCache.invalidate sorted_titles_cache_key
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def bulk_progress(action, ids : Array(String), username)
|
||||
LRUCache.invalidate "#{@id}:#{username}:progress_sum"
|
||||
parents.each do |parent|
|
||||
LRUCache.invalidate "#{parent.id}:#{username}:progress_sum"
|
||||
end
|
||||
[false, true].each do |ascend|
|
||||
sorted_entries_cache_key =
|
||||
SortedEntriesCacheEntry.gen_key @id, username, @entries,
|
||||
SortOptions.new(SortMethod::Progress, ascend)
|
||||
LRUCache.invalidate sorted_entries_cache_key
|
||||
end
|
||||
remove_sorted_caches [SortMethod::Progress], username
|
||||
|
||||
selected_entries = ids
|
||||
.map { |id|
|
||||
|
@ -86,7 +86,11 @@ def env_is_true?(key : String) : Bool
|
||||
val.downcase.in? "1", "true"
|
||||
end
|
||||
|
||||
def sort_titles(titles : Array(Title), opt : SortOptions, username : String)
|
||||
def sort_titles(titles : Array(Title), opt : SortOptions, username : String) : Array(Title)
|
||||
cache_key = SortedTitlesCacheEntry.gen_key username, titles, opt
|
||||
cached_titles = LRUCache.get cache_key
|
||||
return cached_titles if cached_titles.is_a? Array(Title)
|
||||
|
||||
ary = titles
|
||||
|
||||
case opt.method
|
||||
@ -110,6 +114,7 @@ def sort_titles(titles : Array(Title), opt : SortOptions, username : String)
|
||||
|
||||
ary.reverse! unless opt.not_nil!.ascend
|
||||
|
||||
LRUCache.set generate_cache_entry cache_key, ary
|
||||
ary
|
||||
end
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user