Skip to content

Commit

Permalink
Cache TitleInfo using LRUCache
Browse files Browse the repository at this point in the history
  • Loading branch information
Leeingnyo committed Sep 4, 2021
1 parent de410f4 commit 847f516
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/library/types.cr
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ class TitleInfo
@@mutex_hash = {} of String => Mutex

def self.new(dir, &)
key = "#{dir}:info.json"
info = LRUCache.get key
if info.is_a? String
begin
instance = TitleInfo.from_json info
instance.dir = dir
yield instance
return
rescue
end
end

if @@mutex_hash[dir]?
mutex = @@mutex_hash[dir]
else
Expand All @@ -105,12 +117,15 @@ class TitleInfo
instance = TitleInfo.from_json File.read json_path
end
instance.dir = dir
LRUCache.set generate_cache_entry key, instance.to_json
yield instance
end
end

def save
json_path = File.join @dir, "info.json"
File.write json_path, self.to_pretty_json
key = "#{@dir}:info.json"
LRUCache.set generate_cache_entry key, self.to_json
end
end

0 comments on commit 847f516

Please sign in to comment.