lib/dry_ice.rb in dry_ice-0.1.0 vs lib/dry_ice.rb in dry_ice-0.1.1

- old
+ new

@@ -32,14 +32,14 @@ # cache :store => 'file', :timeout => 600, :location => '/tmp/' # # # Use your own cache store (see +AbstractStore+ class below) # cache :store => 'memcached', :timeout => 600, :server => '192.168.1.1:1001' # - def cache(cache) + def cache(cache, options = {}) return @cache = nil unless cache raise "cache instance must respond_to #read, #write and #delete" unless cache.respond_to?(:read) && cache.respond_to?(:write) && cache.respond_to?(:delete) - @cache = IceCache.new(cache) + @cache = IceCache.new(cache, options) end def get_cache @cache || false end @@ -100,11 +100,12 @@ class IceCache require 'msgpack' - def initialize(cache) + def initialize(cache, options = {}) + @options = {:serialize => true}.merge(options) @cache = cache end def write(name, value, options = {}) @cache.write(name, serialize_response(value), options) @@ -113,15 +114,21 @@ def serialize_response(response) headers = response.headers.dup body = response.body.dup parsed_response = response.parsed_response - [headers,body,parsed_response].to_msgpack + if @options[:serialize] + [headers,body,parsed_response].to_msgpack + else + [headers,body,parsed_response] + end end def build_response(serialized_response) - res = MessagePack.unpack(serialized_response) - CachedHTTPartyResponse.new(res[0], res[1], res[2]) + if @options[:serialize] + serialized_response = MessagePack.unpack(serialized_response) + end + CachedHTTPartyResponse.new(serialized_response[0], serialized_response[1], serialized_response[2]) end def read(*args) found = @cache.read(*args) build_response(found) if found