mirror of
https://github.com/hkalexling/Mango.git
synced 2025-08-03 11:25:29 -04:00
Make entry generic
This commit is contained in:
parent
9e90aa17b9
commit
5e919d3e19
@ -1,5 +1,7 @@
|
|||||||
require "digest"
|
require "digest"
|
||||||
|
|
||||||
|
require "./entry"
|
||||||
|
|
||||||
class InfoCache
|
class InfoCache
|
||||||
alias ProgressCache = Tuple(String, Int32)
|
alias ProgressCache = Tuple(String, Int32)
|
||||||
|
|
||||||
@ -158,23 +160,46 @@ class InfoCache
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private class SortedEntriesCacheEntry
|
private class CacheEntry(SaveT, ReturnT)
|
||||||
@value : Array(String)
|
|
||||||
|
|
||||||
getter key : String, atime : Time
|
getter key : String, atime : Time
|
||||||
|
|
||||||
def initialize(@ctime : Time, @key : String, value : Array(Entry))
|
@value : SaveT
|
||||||
@atime = @ctime
|
|
||||||
@value = value.map &.id
|
def initialize(@key : String, value : ReturnT)
|
||||||
|
@atime = @ctime = Time.utc
|
||||||
|
@value = self.class.to_save_t value
|
||||||
end
|
end
|
||||||
|
|
||||||
def value
|
def value
|
||||||
@atime = Time.utc
|
@atime = Time.utc
|
||||||
SortedEntriesCacheEntry.ids2entries @value
|
self.class.to_return_t @value
|
||||||
end
|
end
|
||||||
|
|
||||||
# private?
|
def self.to_save_t(value : ReturnT)
|
||||||
def self.ids2entries(ids : Array(String))
|
value
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.to_return_t(value : SaveT)
|
||||||
|
value
|
||||||
|
end
|
||||||
|
|
||||||
|
def instance_size
|
||||||
|
instance_sizeof(CacheEntry(SaveT, ReturnT)) + # sizeof itself
|
||||||
|
instance_sizeof(String) + @key.bytesize + # allocated memory for @key
|
||||||
|
@value.instance_size
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class SortedEntriesCacheEntry < CacheEntry(Array(String), Array(Entry))
|
||||||
|
def self.to_save_t(value : Array(Entry))
|
||||||
|
value.map &.id
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.to_return_t(value : Array(String))
|
||||||
|
ids2entries value
|
||||||
|
end
|
||||||
|
|
||||||
|
private def self.ids2entries(ids : Array(String))
|
||||||
e_map = Library.default.deep_entries.to_h { |entry| {entry.id, entry} }
|
e_map = Library.default.deep_entries.to_h { |entry| {entry.id, entry} }
|
||||||
entries = [] of Entry
|
entries = [] of Entry
|
||||||
begin
|
begin
|
||||||
@ -202,11 +227,21 @@ private class SortedEntriesCacheEntry
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
alias CacheEntryType = SortedEntriesCacheEntry
|
||||||
|
|
||||||
|
def generate_cache_entry(key : String, value : Array(Entry) | Int32 | String)
|
||||||
|
if value.is_a? Array(Entry)
|
||||||
|
SortedEntriesCacheEntry.new key, value
|
||||||
|
else
|
||||||
|
CacheEntry(typeof(value), typeof(value)).new key, value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# LRU Cache
|
# LRU Cache
|
||||||
class SortedEntriesCache
|
class SortedEntriesCache
|
||||||
@@limit : Int128 = Int128.new 0
|
@@limit : Int128 = Int128.new 0
|
||||||
# key => entry
|
# key => entry
|
||||||
@@cache = {} of String => SortedEntriesCacheEntry
|
@@cache = {} of String => CacheEntryType
|
||||||
|
|
||||||
def self.init
|
def self.init
|
||||||
enabled = Config.current.sorted_entries_cache_enable
|
enabled = Config.current.sorted_entries_cache_enable
|
||||||
@ -221,8 +256,9 @@ class SortedEntriesCache
|
|||||||
return entry.value unless entry.nil?
|
return entry.value unless entry.nil?
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.set(key : String, value : Array(Entry))
|
def self.set(cache_entry : CacheEntryType)
|
||||||
@@cache[key] = SortedEntriesCacheEntry.new Time.utc, key, value
|
key = cache_entry.key
|
||||||
|
@@cache[key] = cache_entry
|
||||||
Logger.debug "LRUCache Cached #{key}"
|
Logger.debug "LRUCache Cached #{key}"
|
||||||
remove_victim_cache
|
remove_victim_cache
|
||||||
end
|
end
|
||||||
|
@ -346,7 +346,7 @@ class Title
|
|||||||
def sorted_entries(username, opt : SortOptions? = nil)
|
def sorted_entries(username, opt : SortOptions? = nil)
|
||||||
cache_key = SortedEntriesCacheEntry.gen_key @id, username, @entries, opt
|
cache_key = SortedEntriesCacheEntry.gen_key @id, username, @entries, opt
|
||||||
cached_entries = SortedEntriesCache.get cache_key
|
cached_entries = SortedEntriesCache.get cache_key
|
||||||
return cached_entries if cached_entries
|
return cached_entries if cached_entries.is_a? Array(Entry)
|
||||||
|
|
||||||
if opt.nil?
|
if opt.nil?
|
||||||
opt = SortOptions.from_info_json @dir, username
|
opt = SortOptions.from_info_json @dir, username
|
||||||
@ -382,7 +382,7 @@ class Title
|
|||||||
ary.reverse! unless opt.not_nil!.ascend
|
ary.reverse! unless opt.not_nil!.ascend
|
||||||
|
|
||||||
if Config.current.sorted_entries_cache_enable
|
if Config.current.sorted_entries_cache_enable
|
||||||
SortedEntriesCache.set cache_key, ary
|
SortedEntriesCache.set generate_cache_entry cache_key, ary
|
||||||
end
|
end
|
||||||
ary
|
ary
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user