Skip to content

Commit

Permalink
Merge branch 'caching'
Browse files Browse the repository at this point in the history
  • Loading branch information
eugenebolshakov committed Apr 13, 2011
2 parents 9e53056 + b17c792 commit 82c9fd3
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions lib/magento/connection.rb
Expand Up @@ -11,14 +11,34 @@ def connect
@client = XMLRPC::Client.new(config[:host], config[:path], config[:port])
@session = @client.call("login", config[:username], config[:api_key])
end

def call(method = nil, *args)
@logger.debug "call: #{method}, #{args.inspect}"
connect
@client.call("call", @session, method, args)
rescue XMLRPC::FaultException => e
@logger.debug "exception: #{e.faultCode} -> #{e.faultString}"
raise Magento::ApiError, "#{e.faultCode} -> #{e.faultString}"
cache? ? call_with_caching(method, *args) : call_without_caching(method, *args)
end

private

def cache?
!!config[:cache_store]
end

def call_without_caching(method = nil, *args)
@logger.debug "call: #{method}, #{args.inspect}"
connect
@client.call("call", @session, method, args)
rescue XMLRPC::FaultException => e
@logger.debug "exception: #{e.faultCode} -> #{e.faultString}"
raise Magento::ApiError, "#{e.faultCode} -> #{e.faultString}"
end

def call_with_caching(method = nil, *args)
config[:cache_store].fetch(cache_key(method, *args)) do
call_without_caching(method, *args)
end
end

def cache_key(method, *args)
"#{config[:username]}@#{config[:host]}:#{config[:port]}#{config[:path]}/#{method}/#{args.inspect}"
end
end
end
end

0 comments on commit 82c9fd3

Please sign in to comment.