Skip to content

Instantly share code, notes, and snippets.

@jarod022
Created March 22, 2017 16:48
Show Gist options
  • Save jarod022/01dddda28c52b955ec70734d8b8736de to your computer and use it in GitHub Desktop.
Save jarod022/01dddda28c52b955ec70734d8b8736de to your computer and use it in GitHub Desktop.
Rails: how to save up database queries using cache
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