app/models/response_cache.rb in radiant-0.6.4 vs app/models/response_cache.rb in radiant-0.6.5
- old
+ new
@@ -53,26 +53,26 @@
read_response(path, response, request)
end
response
end
- # Returns true if a response is cached at the specified path.
+ # Returns metadata for path.
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'] >= Time.now)
- return metadata
- end
- end
- rescue
+ content = File.open(name, "rb") { |f| f.read }
+ metadata = YAML::load(content)
+ metadata if metadata['expires'] >= Time.now
+ end
+ rescue
nil
end
+
+ # Returns true if a response is cached at the specified path.
def response_cached?(path)
- !!read_metadata(path)
+ perform_caching && !!read_metadata(path)
end
# Expires the cached response for the specified path.
def expire_response(path)
path = clean(path)
@@ -90,24 +90,17 @@
def self.instance
@@instance ||= new
end
private
- # Construct the filename for the file in the cache directory for path
- # This DOES NOT include the extension
- def page_cache_file(path)
- name = ((path.empty? || path == "/") ? "/index" : URI.unescape(path))
- end
-
# Ensures that path begins with a slash and remove extra slashes.
def clean(path)
path = path.gsub(%r{/+}, '/')
%r{^/?(.*?)/?$}.match(path)
"/#{$1}"
end
-
# 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'] || {})
@@ -146,37 +139,36 @@
'expires' => expires
}.to_yaml
cache_page(metadata, response.body, path)
end
- def page_cache_path(path)
- root_dir = File.expand_path(page_cache_directory)
- cache_path = File.expand_path(File.join(root_dir,path), root_dir)
- if(cache_path.index(root_dir) == 0)
- cache_path
- end
+ def page_cache_path(path)
+ path = (path.empty? || path == "/") ? "/_site-root" : URI.unescape(path)
+ root_dir = File.expand_path(page_cache_directory)
+ cache_path = File.expand_path(File.join(root_dir, path), root_dir)
+ cache_path if cache_path.index(root_dir) == 0
end
def expire_page(path)
return unless perform_caching
if path = page_cache_path(path)
benchmark "Expired page: #{path}" do
File.delete("#{path}.yml") if File.exists?("#{path}.yml")
File.delete("#{path}.data") if File.exists?("#{path}.data")
- end
+ end
end
end
def cache_page(metadata, content, path)
return unless perform_caching
- if path = page_cache_path(path)
+ if 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}.data", "wb+") { |f| f.write(content) }
File.open("#{path}.yml", "wb+") { |f| f.write(metadata) }
- end
+ end
end
end
end