Skip to content

Instantly share code, notes, and snippets.

@phildionne
Forked from plehoux/cachemire.rb
Last active December 24, 2015 02:09
Show Gist options
  • Save phildionne/6728341 to your computer and use it in GitHub Desktop.
Save phildionne/6728341 to your computer and use it in GitHub Desktop.
module Cachemire
extend ActiveSupport::Concern
included do
class_attribute :cache_store
end
module ClassMethods
private
def cache_result(&block)
cache_store.fetch("#{object_id}_#{caller_locations(1,1)[0].label}") do
block.call
end
end
end
private
def cache_result(&block)
cache_store.fetch("#{object_id}_#{caller_locations(1,1)[0].label}") do
block.call
end
end
end
class World
include Cachemire
self.cache_store = ActiveSupport::Cache::MemoryStore.new
def self.get_population_count
cache_result do
# Intense stuff...
end
end
def get_population_count
cache_result do
# Intense stuff...
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment