mirror of
https://github.com/hkalexling/Mango.git
synced 2025-08-02 19:05:32 -04:00
Rename config fields and improve logging
This commit is contained in:
parent
79ef7bcd1c
commit
51806f18db
@ -20,8 +20,9 @@ class Config
|
|||||||
property plugin_path : String = File.expand_path "~/mango/plugins",
|
property plugin_path : String = File.expand_path "~/mango/plugins",
|
||||||
home: true
|
home: true
|
||||||
property download_timeout_seconds : Int32 = 30
|
property download_timeout_seconds : Int32 = 30
|
||||||
property sorted_entries_cache_enable = false
|
property cache_enabled = false
|
||||||
property sorted_entries_cache_size_mbs = 50
|
property cache_size_mbs = 50
|
||||||
|
property cache_log_enabled = true
|
||||||
property disable_login = false
|
property disable_login = false
|
||||||
property default_username = ""
|
property default_username = ""
|
||||||
property auth_proxy_header_name = ""
|
property auth_proxy_header_name = ""
|
||||||
|
@ -106,23 +106,26 @@ end
|
|||||||
# LRU Cache
|
# LRU Cache
|
||||||
class LRUCache
|
class LRUCache
|
||||||
@@limit : Int128 = Int128.new 0
|
@@limit : Int128 = Int128.new 0
|
||||||
|
@@should_log = true
|
||||||
# key => entry
|
# key => entry
|
||||||
@@cache = {} of String => CacheEntryType
|
@@cache = {} of String => CacheEntryType
|
||||||
|
|
||||||
def self.enabled
|
def self.enabled
|
||||||
Config.current.sorted_entries_cache_enable
|
Config.current.cache_enabled
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.init
|
def self.init
|
||||||
cache_size = Config.current.sorted_entries_cache_size_mbs
|
cache_size = Config.current.cache_size_mbs
|
||||||
@@limit = Int128.new cache_size * 1024 * 1024 if enabled
|
@@limit = Int128.new cache_size * 1024 * 1024 if enabled
|
||||||
|
@@should_log = Config.current.cache_log_enabled
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.get(key : String)
|
def self.get(key : String)
|
||||||
return unless enabled
|
return unless enabled
|
||||||
entry = @@cache[key]?
|
entry = @@cache[key]?
|
||||||
Logger.debug "LRUCache Cache Hit! #{key}" unless entry.nil?
|
if @@should_log
|
||||||
Logger.debug "LRUCache Cache Miss #{key}" if entry.nil?
|
Logger.debug "LRUCache #{entry.nil? ? "miss" : "hit"} #{key}"
|
||||||
|
end
|
||||||
return entry.value unless entry.nil?
|
return entry.value unless entry.nil?
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -130,8 +133,8 @@ class LRUCache
|
|||||||
return unless enabled
|
return unless enabled
|
||||||
key = cache_entry.key
|
key = cache_entry.key
|
||||||
@@cache[key] = cache_entry
|
@@cache[key] = cache_entry
|
||||||
Logger.debug "LRUCache Cached #{key}"
|
Logger.debug "LRUCache cached #{key}" if @@should_log
|
||||||
remove_victim_cache
|
remove_least_recent_access
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.invalidate(key : String)
|
def self.invalidate(key : String)
|
||||||
@ -140,6 +143,7 @@ class LRUCache
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.print
|
def self.print
|
||||||
|
return unless @@should_log
|
||||||
sum = @@cache.sum { |_, entry| entry.instance_size }
|
sum = @@cache.sum { |_, entry| entry.instance_size }
|
||||||
Logger.debug "---- LRU Cache ----"
|
Logger.debug "---- LRU Cache ----"
|
||||||
Logger.debug "Size: #{sum} Bytes"
|
Logger.debug "Size: #{sum} Bytes"
|
||||||
@ -155,14 +159,17 @@ class LRUCache
|
|||||||
sum > @@limit
|
sum > @@limit
|
||||||
end
|
end
|
||||||
|
|
||||||
private def self.remove_victim_cache
|
private def self.remove_least_recent_access
|
||||||
|
Logger.debug "Removing entries from LRUCache" if @@should_log
|
||||||
while is_cache_full && @@cache.size > 0
|
while is_cache_full && @@cache.size > 0
|
||||||
Logger.debug "LRUCache Cache Full! Remove LRU"
|
min_tuple = @@cache.min_by { |_, entry| entry.atime }
|
||||||
min = @@cache.min_by? { |_, entry| entry.atime }
|
min_key = min_tuple[0]
|
||||||
|
min_entry = min_tuple[1]
|
||||||
|
|
||||||
Logger.debug " \
|
Logger.debug " \
|
||||||
Target: #{min[0]}, \
|
Target: #{min_key}, \
|
||||||
Last Access Time: #{min[1].atime}" if min
|
Last Access Time: #{min_entry.atime}" if @@should_log
|
||||||
invalidate min[0] if min
|
invalidate min_key
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user