|
| 1 | +require "digest" |
| 2 | + |
| 3 | +require "./entry" |
| 4 | +require "./types" |
| 5 | + |
| 6 | +# Base class for an entry in the LRU cache. |
| 7 | +# There are two ways to use it: |
| 8 | +# 1. Use it as it is by instantiating with the appropriate `SaveT` and |
| 9 | +# `ReturnT`. Note that in this case, `SaveT` and `ReturnT` must be the |
| 10 | +# same type. That is, the input value will be stored as it is without |
| 11 | +# any transformation. |
| 12 | +# 2. You can also subclass it and provide custom implementations for |
| 13 | +# `to_save_t` and `to_return_t`. This allows you to transform and store |
| 14 | +# the input value to a different type. See `SortedEntriesCacheEntry` as |
| 15 | +# an example. |
| 16 | +private class CacheEntry(SaveT, ReturnT) |
| 17 | + getter key : String, atime : Time |
| 18 | + |
| 19 | + @value : SaveT |
| 20 | + |
| 21 | + def initialize(@key : String, value : ReturnT) |
| 22 | + @atime = @ctime = Time.utc |
| 23 | + @value = self.class.to_save_t value |
| 24 | + end |
| 25 | + |
| 26 | + def value |
| 27 | + @atime = Time.utc |
| 28 | + self.class.to_return_t @value |
| 29 | + end |
| 30 | + |
| 31 | + def self.to_save_t(value : ReturnT) |
| 32 | + value |
| 33 | + end |
| 34 | + |
| 35 | + def self.to_return_t(value : SaveT) |
| 36 | + value |
| 37 | + end |
| 38 | + |
| 39 | + def instance_size |
| 40 | + instance_sizeof(CacheEntry(SaveT, ReturnT)) + # sizeof itself |
| 41 | + instance_sizeof(String) + @key.bytesize + # allocated memory for @key |
| 42 | + @value.instance_size |
| 43 | + end |
| 44 | +end |
| 45 | + |
| 46 | +class SortedEntriesCacheEntry < CacheEntry(Array(String), Array(Entry)) |
| 47 | + def self.to_save_t(value : Array(Entry)) |
| 48 | + value.map &.id |
| 49 | + end |
| 50 | + |
| 51 | + def self.to_return_t(value : Array(String)) |
| 52 | + ids_to_entries value |
| 53 | + end |
| 54 | + |
| 55 | + private def self.ids_to_entries(ids : Array(String)) |
| 56 | + e_map = Library.default.deep_entries.to_h { |entry| {entry.id, entry} } |
| 57 | + entries = [] of Entry |
| 58 | + begin |
| 59 | + ids.each do |id| |
| 60 | + entries << e_map[id] |
| 61 | + end |
| 62 | + return entries if ids.size == entries.size |
| 63 | + rescue |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + def instance_size |
| 68 | + instance_sizeof(SortedEntriesCacheEntry) + # sizeof itself |
| 69 | + instance_sizeof(String) + @key.bytesize + # allocated memory for @key |
| 70 | + @value.size * (instance_sizeof(String) + sizeof(String)) + |
| 71 | + @value.sum(&.bytesize) # elements in Array(String) |
| 72 | + end |
| 73 | + |
| 74 | + def self.gen_key(book_id : String, username : String, |
| 75 | + entries : Array(Entry), opt : SortOptions?) |
| 76 | + entries_sig = Digest::SHA1.hexdigest (entries.map &.id).to_s |
| 77 | + user_context = opt && opt.method == SortMethod::Progress ? username : "" |
| 78 | + sig = Digest::SHA1.hexdigest (book_id + entries_sig + user_context + |
| 79 | + (opt ? opt.to_tuple.to_s : "nil")) |
| 80 | + "#{sig}:sorted_entries" |
| 81 | + end |
| 82 | +end |
| 83 | + |
| 84 | +class String |
| 85 | + def instance_size |
| 86 | + instance_sizeof(String) + bytesize |
| 87 | + end |
| 88 | +end |
| 89 | + |
| 90 | +struct Tuple(*T) |
| 91 | + def instance_size |
| 92 | + sizeof(T) + # total size of non-reference types |
| 93 | + self.sum do |e| |
| 94 | + next 0 unless e.is_a? Reference |
| 95 | + if e.responds_to? :instance_size |
| 96 | + e.instance_size |
| 97 | + else |
| 98 | + instance_sizeof(typeof(e)) |
| 99 | + end |
| 100 | + end |
| 101 | + end |
| 102 | +end |
| 103 | + |
| 104 | +alias CacheableType = Array(Entry) | String | Tuple(String, Int32) |
| 105 | +alias CacheEntryType = SortedEntriesCacheEntry | |
| 106 | + CacheEntry(String, String) | |
| 107 | + CacheEntry(Tuple(String, Int32), Tuple(String, Int32)) |
| 108 | + |
| 109 | +def generate_cache_entry(key : String, value : CacheableType) |
| 110 | + if value.is_a? Array(Entry) |
| 111 | + SortedEntriesCacheEntry.new key, value |
| 112 | + else |
| 113 | + CacheEntry(typeof(value), typeof(value)).new key, value |
| 114 | + end |
| 115 | +end |
| 116 | + |
| 117 | +# LRU Cache |
| 118 | +class LRUCache |
| 119 | + @@limit : Int128 = Int128.new 0 |
| 120 | + @@should_log = true |
| 121 | + # key => entry |
| 122 | + @@cache = {} of String => CacheEntryType |
| 123 | + |
| 124 | + def self.enabled |
| 125 | + Config.current.cache_enabled |
| 126 | + end |
| 127 | + |
| 128 | + def self.init |
| 129 | + cache_size = Config.current.cache_size_mbs |
| 130 | + @@limit = Int128.new cache_size * 1024 * 1024 if enabled |
| 131 | + @@should_log = Config.current.cache_log_enabled |
| 132 | + end |
| 133 | + |
| 134 | + def self.get(key : String) |
| 135 | + return unless enabled |
| 136 | + entry = @@cache[key]? |
| 137 | + if @@should_log |
| 138 | + Logger.debug "LRUCache #{entry.nil? ? "miss" : "hit"} #{key}" |
| 139 | + end |
| 140 | + return entry.value unless entry.nil? |
| 141 | + end |
| 142 | + |
| 143 | + def self.set(cache_entry : CacheEntryType) |
| 144 | + return unless enabled |
| 145 | + key = cache_entry.key |
| 146 | + @@cache[key] = cache_entry |
| 147 | + Logger.debug "LRUCache cached #{key}" if @@should_log |
| 148 | + remove_least_recent_access |
| 149 | + end |
| 150 | + |
| 151 | + def self.invalidate(key : String) |
| 152 | + return unless enabled |
| 153 | + @@cache.delete key |
| 154 | + end |
| 155 | + |
| 156 | + def self.print |
| 157 | + return unless @@should_log |
| 158 | + sum = @@cache.sum { |_, entry| entry.instance_size } |
| 159 | + Logger.debug "---- LRU Cache ----" |
| 160 | + Logger.debug "Size: #{sum} Bytes" |
| 161 | + Logger.debug "List:" |
| 162 | + @@cache.each do |k, v| |
| 163 | + Logger.debug "#{k} | #{v.atime} | #{v.instance_size}" |
| 164 | + end |
| 165 | + Logger.debug "-------------------" |
| 166 | + end |
| 167 | + |
| 168 | + private def self.is_cache_full |
| 169 | + sum = @@cache.sum { |_, entry| entry.instance_size } |
| 170 | + sum > @@limit |
| 171 | + end |
| 172 | + |
| 173 | + private def self.remove_least_recent_access |
| 174 | + if @@should_log && is_cache_full |
| 175 | + Logger.debug "Removing entries from LRUCache" |
| 176 | + end |
| 177 | + while is_cache_full && @@cache.size > 0 |
| 178 | + min_tuple = @@cache.min_by { |_, entry| entry.atime } |
| 179 | + min_key = min_tuple[0] |
| 180 | + min_entry = min_tuple[1] |
| 181 | + |
| 182 | + Logger.debug " \ |
| 183 | + Target: #{min_key}, \ |
| 184 | + Last Access Time: #{min_entry.atime}" if @@should_log |
| 185 | + invalidate min_key |
| 186 | + end |
| 187 | + end |
| 188 | +end |
0 commit comments