app/models/response_cache.rb in radiant-0.6.2 vs app/models/response_cache.rb in radiant-0.6.3
- old
+ new
@@ -59,15 +59,17 @@
def read_metadata(path)
path = clean(path)
name = "#{page_cache_path(path)}.yml"
if File.exists?(name) and not File.directory?(name)
content = File.open(name,"rb") { |f| f.read }
- metadata = YAML::load(content)
- if(metadata['expires'] && metadata['expires'] >= Time.now)
+ metadata = YAML::load(content)
+ if(metadata['expires'] >= Time.now)
return metadata
end
- end
+ end
+ rescue
+ nil
end
def response_cached?(path)
!!read_metadata(path)
end
@@ -107,13 +109,13 @@
# Reads a cached response from disk and updates a response object.
def read_response(path, response, request)
file_path = page_cache_path(path)
if metadata = read_metadata(path)
response.headers.merge!(metadata['headers'] || {})
- if client_has_cache?(metadata, request)
+ if client_has_cache?(metadata, request)
response.headers.merge!('Status' => '304 Not Modified')
- elsif use_x_sendfile
+ elsif use_x_sendfile
response.headers.merge!('X-Sendfile' => "#{file_path}.data")
else
response.body = File.open("#{file_path}.data", "rb") {|f| f.read}
end
end
@@ -163,11 +165,12 @@
def cache_page(metadata, content, path)
return unless perform_caching
path = page_cache_path(path)
benchmark "Cached page: #{path}" do
- FileUtils.makedirs(File.dirname(path))
+ FileUtils.makedirs(File.dirname(path))
+ #dont want yml without data
+ File.open("#{path}.data", "wb+") { |f| f.write(content) }
File.open("#{path}.yml", "wb+") { |f| f.write(metadata) }
- File.open("#{path}.data", "wb+") { |f| f.write(content) }
end
end
end