Created
March 22, 2017 16:48
-
-
Save jarod022/01dddda28c52b955ec70734d8b8736de to your computer and use it in GitHub Desktop.
Rails: how to save up database queries using cache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GenericCacher | |
attr_reader :cache_store | |
def initialize | |
@cache_store = ActiveSupport::Cache::FileStore.new(“#{Rails.root}# {location}”) | |
end | |
def write | |
cache_store.write(cache_key_env_based, subject) | |
end | |
def fetch | |
cache_store.fetch(cache_key_env_based) do | |
subject | |
end | |
end | |
def delete | |
cache_store.delete(cache_key_env_based) | |
end | |
private | |
def cache_key | |
raise NotImplementedError | |
end | |
def cache_key_env_based | |
“#{cache_key}_#{Rails.env}” | |
end | |
def subject | |
raise NotImplementedError | |
end | |
def location | |
raise NotImplementedError | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment