Use redis as a cache store
This commit is contained in:
@@ -3,42 +3,30 @@ require "json"
|
||||
module Adamantium
|
||||
module ViewCache
|
||||
class Cacher
|
||||
def call(key:, content_proc:, expiry:)
|
||||
cached_content = read(key: key)
|
||||
include Deps["settings"]
|
||||
|
||||
def call(key:, params:, content_proc:, expiry:)
|
||||
calculated_key = "adamantium:#{key}_#{params.join("_")}"
|
||||
cached_content = cache_store.read(key: calculated_key)
|
||||
|
||||
return cached_content if cached_content
|
||||
|
||||
rendered_content = content_proc.call
|
||||
rendered_content = content_proc.call(*params)
|
||||
|
||||
data = JSON.generate(expire: expiry.to_i, content: rendered_content)
|
||||
|
||||
write(key: key, content: data)
|
||||
cache_store.write(key: calculated_key, content: data, expiry: expiry)
|
||||
|
||||
rendered_content
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def write(key:, content:)
|
||||
filename = "#{key}.json"
|
||||
path = File.join(Hanami.app.root, "tmp", filename)
|
||||
|
||||
File.write(path, content)
|
||||
end
|
||||
|
||||
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
|
||||
def cache_store
|
||||
if settings.cache_store == :redis_cache_store
|
||||
RedisCacheStore.new
|
||||
else
|
||||
cached_data["content"]
|
||||
FileStore.new
|
||||
end
|
||||
end
|
||||
end
|
||||
|
28
lib/adamantium/view_cache/file_store.rb
Normal file
28
lib/adamantium/view_cache/file_store.rb
Normal file
@@ -0,0 +1,28 @@
|
||||
module Adamantium
|
||||
module ViewCache
|
||||
class FileStore
|
||||
def write(key:, content:, expiry:)
|
||||
filename = "#{key}.json"
|
||||
path = File.join(Hanami.app.root, "tmp", filename)
|
||||
|
||||
File.write(path, content)
|
||||
end
|
||||
|
||||
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
|
18
lib/adamantium/view_cache/redis_cache_store.rb
Normal file
18
lib/adamantium/view_cache/redis_cache_store.rb
Normal file
@@ -0,0 +1,18 @@
|
||||
module Adamantium
|
||||
module ViewCache
|
||||
class RedisCacheStore
|
||||
include Deps["clients.redis"]
|
||||
|
||||
def read(key:)
|
||||
cached_data = redis.get(key)
|
||||
|
||||
JSON.parse(cached_data)["content"] if cached_data
|
||||
end
|
||||
|
||||
def write(key:, content:, expiry:)
|
||||
redis.set(key, content, exat: expiry.to_i)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user