lib/rack/cache/entitystore.rb in rtomayko-rack-cache-0.3.0 vs lib/rack/cache/entitystore.rb in rtomayko-rack-cache-0.3.9
- old
+ new
@@ -1,8 +1,9 @@
require 'digest/sha1'
module Rack::Cache
+
# Entity stores are used to cache response bodies across requests. All
# Implementations are required to calculate a SHA checksum of the data written
# which becomes the response body's key.
class EntityStore
@@ -11,21 +12,27 @@
# call it after iteration is complete. Return a two-tuple of the form:
# [ hexdigest, size ].
def slurp(body)
digest, size = Digest::SHA1.new, 0
body.each do |part|
- size += part.length
+ size += bytesize(part)
digest << part
yield part
end
body.close if body.respond_to? :close
[ digest.hexdigest, size ]
end
- private :slurp
+ if ''.respond_to?(:bytesize)
+ def bytesize(string); string.bytesize; end
+ else
+ def bytesize(string); string.size; end
+ end
+ private :slurp, :bytesize
+
# Stores entity bodies on the heap using a Hash object.
class Heap < EntityStore
# Create the store with the specified backing Hash.
def initialize(hash={})
@@ -88,11 +95,11 @@
def exist?(key)
File.exist?(body_path(key))
end
def read(key)
- File.read(body_path(key))
+ File.open(body_path(key), 'rb') { |f| f.read }
rescue Errno::ENOENT
nil
end
class Body < ::File #:nodoc:
@@ -239,9 +246,8 @@
end
end
MEMCACHE = MemCache
MEMCACHED = MemCache
-
end
end