Created
March 26, 2012 12:14
-
-
Save chytreg/2204719 to your computer and use it in GitHub Desktop.
Active Resource with ConditionalGet using wrest and dalli gems.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'wrest' | |
require 'dalli' | |
module Wrest::Caching | |
class Memcached | |
def initialize(server_urls=nil, options={}) | |
@memcached = Dalli::Client.new(server_urls, options) | |
end | |
def [](key) | |
@memcached.get(key) | |
end | |
def []=(key, value) | |
@memcached.set(key, value) | |
end | |
# should be compatible with Hash - return value of the deleted element. | |
def delete(key) | |
value = self[key] | |
@memcached.delete key | |
return value | |
end | |
end | |
end | |
Wrest::Caching.default_store = Wrest::Caching::Memcached.new | |
module ActiveResource | |
class Connection | |
# Executes a GET request. | |
# Used to get (find) resources. | |
# def get(path, headers = {}) | |
# with_auth { request(:get, path, build_request_headers(headers, :get, self.site.merge(path))) } | |
# end | |
def get(path, headers = {}) | |
result = ActiveSupport::Notifications.instrument("request.active_resource") do |payload| | |
payload[:method] = 'get' | |
payload[:request_uri] = "#{site.scheme}://#{site.host}:#{site.port}#{path}" | |
payload[:result] = with_auth { payload[:request_uri].to_uri.using_memcached.get({}, build_request_headers(headers, :get, self.site.merge(path))) } | |
end | |
handle_response(result) | |
rescue Timeout::Error => e | |
raise TimeoutError.new(e.message) | |
rescue OpenSSL::SSL::SSLError => e | |
raise SSLError.new(e.message) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment