Add action cache and cache now playing requests

This commit is contained in:
2023-11-02 20:43:59 +11:00
parent e0c03ac5ae
commit 85d3b9214e
9 changed files with 62 additions and 14 deletions

View File

@@ -0,0 +1,39 @@
require "json"
module Adamantium
module ViewCache
class Cacher
def call(key:, content:, expiry:)
cached_content = read(key: key)
return cached_content if cached_content
data = JSON.generate(expire: expiry.to_i, content: content)
path = "#{key}.json"
File.write(File.join(Hanami.app.root, "tmp", path), data)
content
end
private
def read(key:)
filename = "#{key}.json"
path = File.join(Hanami.app.root, "tmp", filename)
return nil unless File.exist?(path)
cached_data = JSON.parse(File.read(path))
if Time.strptime(cached_data["expire"].to_s, "%s") < Time.now
File.delete(path)
nil
else
cached_data["content"]
end
end
end
end
end