Fix title sort bug, invalidate titles of the Library

Refactor remove cache
This commit is contained in:
Leeingnyo 2021-12-26 04:27:27 +09:00
parent b711072492
commit ecaec307d6
3 changed files with 35 additions and 16 deletions

View File

@ -90,7 +90,8 @@ class Entry
end
@book.entry_sort_title_cache = nil
@book.remove_sorted_caches [SortMethod::Auto, SortMethod::Title], username
@book.remove_sorted_entries_cache [SortMethod::Auto, SortMethod::Title],
username
end
def sort_title_db

View File

@ -320,7 +320,13 @@ class Title
@sort_title = sort_title
end
remove_sorted_caches [SortMethod::Auto, SortMethod::Title], username
if parents.size > 0
target = parents[-1].titles
else
target = Library.default.titles
end
remove_sorted_titles_cache target,
[SortMethod::Auto, SortMethod::Title], username
end
def sort_title_db
@ -612,7 +618,8 @@ class Title
zip + titles.flat_map &.deep_entries_with_date_added
end
def remove_sorted_caches(sort_methods : Array(SortMethod), username : String)
def remove_sorted_entries_cache(sort_methods : Array(SortMethod),
username : String)
[false, true].each do |ascend|
sort_methods.each do |sort_method|
sorted_entries_cache_key =
@ -621,15 +628,14 @@ class Title
LRUCache.invalidate sorted_entries_cache_key
end
end
end
def remove_sorted_caches(sort_methods : Array(SortMethod), username : String)
remove_sorted_entries_cache sort_methods, username
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
remove_sorted_titles_cache parent.titles, sort_methods, username
end
remove_sorted_titles_cache Library.default.titles, sort_methods, username
end
def bulk_progress(action, ids : Array(String), username)

View File

@ -91,25 +91,25 @@ def sort_titles(titles : Array(Title), opt : SortOptions, username : String)
cached_titles = LRUCache.get cache_key
return cached_titles if cached_titles.is_a? Array(Title)
ary = titles
case opt.method
when .time_modified?
ary.sort { |a, b| (a.mtime <=> b.mtime).or \
ary = titles.sort { |a, b| (a.mtime <=> b.mtime).or \
compare_numerically a.sort_title, b.sort_title }
when .progress?
ary.sort do |a, b|
ary = titles.sort do |a, b|
(a.load_percentage(username) <=> b.load_percentage(username)).or \
compare_numerically a.sort_title, b.sort_title
end
when .title?
ary.sort { |a, b| compare_numerically a.sort_title, b.sort_title }
ary = titles.sort do |a, b|
compare_numerically a.sort_title, b.sort_title
end
else
unless opt.method.auto?
Logger.warn "Unknown sorting method #{opt.not_nil!.method}. Using " \
"Auto instead"
end
ary.sort { |a, b| compare_numerically a.sort_title, b.sort_title }
ary = titles.sort { |a, b| compare_numerically a.sort_title, b.sort_title }
end
ary.reverse! unless opt.not_nil!.ascend
@ -118,6 +118,18 @@ def sort_titles(titles : Array(Title), opt : SortOptions, username : String)
ary
end
def remove_sorted_titles_cache(titles : Array(Title),
sort_methods : Array(SortMethod),
username : String)
[false, true].each do |ascend|
sort_methods.each do |sort_method|
sorted_titles_cache_key = SortedTitlesCacheEntry.gen_key username,
titles, SortOptions.new(sort_method, ascend)
LRUCache.invalidate sorted_titles_cache_key
end
end
end
class String
# Returns the similarity (in [0, 1]) of two paths.
# For the two paths, separate them into arrays of components, count the