Skip to content

Instantly share code, notes, and snippets.

@nkallen
Created February 20, 2009 20:52
Show Gist options
  • Save nkallen/67686 to your computer and use it in GitHub Desktop.
Save nkallen/67686 to your computer and use it in GitHub Desktop.
module Cash
class Lock
def self.synchronize(*args, &block)
$lock.synchronize(*args, &block)
end
end
module Accessor
module ClassMethods
alias_method :expire_cache, :expire
alias_method :fetch_cache, :fetch
alias_method :set_cache, :set
alias_method :get_cache, :get
def get_cache(*args, &block)
options = args.last.is_a?(Hash) ? args.pop : {}
keys = args.flatten
case keys.size
when 1
get(keys.first, options) do
fetch_cachable_data(keys.first, &block)
end
else
get(keys, options) { |missing| find(missing) }
end
end
def fetch_cachable_data(cache_id = nil, &block)
if block_given?
yield(cache_id)
else
find(cache_id).shallow_clone
end
end
def cache_key(key)
"#{name}:#{cache_config.version}/#{key.to_s.gsub('%', '%%').gsub(' ', '%')}"[0, 255]
end
end
module InstanceMethods
def set_cache(key = id)
self.class.set key, self
end
def expire_cache(key = id)
self.class.expire_cache key
end
end
end
end
ActionView::Helpers::CacheHelper.class_eval do
def cache(name = {}, options = nil, &block)
@controller.cache_erb_fragment(block, name, options)
end
end
ActionController::Base.fragment_cache_store = $memcache
class << $memcache
def read(key, options)
get(key)
end
def write(key, value, options)
set(key, false)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment