Skip to content

Instantly share code, notes, and snippets.

@betamatt
Created April 20, 2010 14:32
Show Gist options
  • Select an option

  • Save betamatt/372534 to your computer and use it in GitHub Desktop.

Select an option

Save betamatt/372534 to your computer and use it in GitHub Desktop.
A wrapper for memcache-client that swallows cache errors
class MemCacheWrapper
def initialize(repository)
@repository = repository
end
def self.handle_errors_for(*args)
options = args.extract_options!
args.each do |method|
define_method method do |*x|
wrap(options[:return]) { @repository.send(method, *x) }
end
end
end
handle_errors_for :get, :set, :add, :delete
handle_errors_for :get_multi, :return => {}
private
def method_missing(*args)
@repository.send(*args)
end
def wrap(error_result)
yield
rescue MemCache::MemCacheError => e
error_result
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment