Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Cache proxy for deferred DB-access
We're deferring the execution of the database accessing code until it is really need. With this lazy evaluation we don't have to think about whether s/th is cached or not in the controller.
class CacheProxy
# undef all methods:
instance_methods.each do |method|
undef_method(method) if method !~ /^(__)/
end
def initialize(&cache_proxy_block)
@cache_proxy_block = cache_proxy_block
end
def logger
RAILS_DEFAULT_LOGGER
end
def method_missing(method, *args, &block)
unless @cache_proxy_obj
logger.info "cache-proxy-block evaluated, triggered by ##{method}"
@cache_proxy_obj = @cache_proxy_block.call # only call the block once
end
@cache_proxy_obj.send(method, *args, &block) # delegate unknown methods to the called block
end
end
def index
@albums = CacheProxy.new { Audio::Album.fresh_latest_songs }
end
- cache 'albums' do
- unless @albums.empty?
= # show the albums
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.