Remove caching verbosely, add cached_cover_url

This commit is contained in:
Leeingnyo 2021-09-06 02:23:02 +09:00
parent c5b6a8b5b9
commit 565a535d22
5 changed files with 5 additions and 34 deletions

View File

@ -70,21 +70,6 @@ class SortedEntriesCacheEntry < CacheEntry(Array(String), Array(Entry))
end end
end end
class SortOptionsCacheEntry < CacheEntry(Tuple(String, Bool), SortOptions)
def self.to_save_t(value : SortOptions)
value.to_tuple
end
def self.to_return_t(value : Tuple(String, Bool))
SortOptions.from_tuple value
end
def instance_size
instance_sizeof(SortOptionsCacheEntry) +
@value[0].instance_size
end
end
class String class String
def instance_size def instance_size
instance_sizeof(String) + bytesize instance_sizeof(String) + bytesize
@ -105,18 +90,14 @@ struct Tuple(*T)
end end
end end
alias CacheableType = Array(Entry) | String | Tuple(String, Int32) | alias CacheableType = Array(Entry) | String | Tuple(String, Int32)
SortOptions
alias CacheEntryType = SortedEntriesCacheEntry | alias CacheEntryType = SortedEntriesCacheEntry |
SortOptionsCacheEntry |
CacheEntry(String, String) | CacheEntry(String, String) |
CacheEntry(Tuple(String, Int32), Tuple(String, Int32)) CacheEntry(Tuple(String, Int32), Tuple(String, Int32))
def generate_cache_entry(key : String, value : CacheableType) def generate_cache_entry(key : String, value : CacheableType)
if value.is_a? Array(Entry) if value.is_a? Array(Entry)
SortedEntriesCacheEntry.new key, value SortedEntriesCacheEntry.new key, value
elsif value.is_a? SortOptions
SortOptionsCacheEntry.new key, value
else else
CacheEntry(typeof(value), typeof(value)).new key, value CacheEntry(typeof(value), typeof(value)).new key, value
end end

View File

@ -81,8 +81,6 @@ class Entry
def cover_url def cover_url
return "#{Config.current.base_url}img/icon.png" if @err_msg return "#{Config.current.base_url}img/icon.png" if @err_msg
cached_cover_url = LRUCache.get "#{@id}:cover_url"
return cached_cover_url if cached_cover_url.is_a? String
unless @book.entry_cover_url_cache unless @book.entry_cover_url_cache
TitleInfo.new @book.dir do |info| TitleInfo.new @book.dir do |info|
@ -98,7 +96,6 @@ class Entry
url = File.join Config.current.base_url, info_url url = File.join Config.current.base_url, info_url
end end
end end
LRUCache.set generate_cache_entry "#{@id}:cover_url", url
url url
end end

View File

@ -11,6 +11,7 @@ class Title
@entry_display_name_cache : Hash(String, String)? @entry_display_name_cache : Hash(String, String)?
@entry_cover_url_cache : Hash(String, String)? @entry_cover_url_cache : Hash(String, String)?
@cached_display_name : String? @cached_display_name : String?
@cached_cover_url : String?
def initialize(@dir : String, @parent_id) def initialize(@dir : String, @parent_id)
storage = Storage.default storage = Storage.default
@ -230,8 +231,8 @@ class Title
end end
def cover_url def cover_url
cached_cover_url = LRUCache.get "#{@id}:cover_url" cached_cover_url = @cached_cover_url
return cached_cover_url if cached_cover_url.is_a? String return cached_cover_url unless cached_cover_url.nil?
url = "#{Config.current.base_url}img/icon.png" url = "#{Config.current.base_url}img/icon.png"
readable_entries = @entries.select &.err_msg.nil? readable_entries = @entries.select &.err_msg.nil?
@ -244,12 +245,11 @@ class Title
url = File.join Config.current.base_url, info_url url = File.join Config.current.base_url, info_url
end end
end end
LRUCache.set generate_cache_entry "#{@id}:cover_url", url @cached_cover_url = url
url url
end end
def set_cover_url(url : String) def set_cover_url(url : String)
LRUCache.invalidate "#{@id}:cover_url"
TitleInfo.new @dir do |info| TitleInfo.new @dir do |info|
info.cover_url = url info.cover_url = url
info.save info.save
@ -257,8 +257,6 @@ class Title
end end
def set_cover_url(entry_name : String, url : String) def set_cover_url(entry_name : String, url : String)
selected_entry = @entries.find { |entry| entry.display_name == entry_name }
LRUCache.invalidate "#{selected_entry.id}:cover_url" if selected_entry
TitleInfo.new @dir do |info| TitleInfo.new @dir do |info|
info.entry_cover_url[entry_name] = url info.entry_cover_url[entry_name] = url
info.save info.save

View File

@ -35,16 +35,12 @@ class SortOptions
end end
def self.from_info_json(dir, username) def self.from_info_json(dir, username)
key = "#{dir}:#{username}:sort_opt"
cached_opt = LRUCache.get key
return cached_opt if cached_opt.is_a? SortOptions
opt = SortOptions.new opt = SortOptions.new
TitleInfo.new dir do |info| TitleInfo.new dir do |info|
if info.sort_by.has_key? username if info.sort_by.has_key? username
opt = SortOptions.from_tuple info.sort_by[username] opt = SortOptions.from_tuple info.sort_by[username]
end end
end end
LRUCache.set generate_cache_entry key, opt
opt opt
end end

View File

@ -121,7 +121,6 @@ macro get_and_save_sort_opt(dir)
sort_opt = SortOptions.new sort_method, is_ascending sort_opt = SortOptions.new sort_method, is_ascending
key = "#{{{dir}}}:#{username}:sort_opt" key = "#{{{dir}}}:#{username}:sort_opt"
LRUCache.set generate_cache_entry key, sort_opt
TitleInfo.new {{dir}} do |info| TitleInfo.new {{dir}} do |info|
info.sort_by[username] = sort_opt.to_tuple info.sort_by[username] = sort_opt.to_tuple
info.save info.save