Skip to content
Browse files

Initial commit.

  • Loading branch information...
0 parents commit b2b560a4512666780fabf08bd977da1c6bdabeb2 @ms committed Mar 31, 2012
Showing with 194 additions and 0 deletions.
  1. +17 −0 .gitignore
  2. +4 −0 Gemfile
  3. +14 −0 LICENSE
  4. +67 −0 README.md
  5. +2 −0 Rakefile
  6. +3 −0 lib/nesta-plugin-cache.rb
  7. +52 −0 lib/nesta-plugin-cache/init.rb
  8. +7 −0 lib/nesta-plugin-cache/version.rb
  9. +28 −0 nesta-plugin-cache.gemspec
17 .gitignore
@@ -0,0 +1,17 @@
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
+Gemfile.lock
+InstalledFiles
+_yardoc
+coverage
+doc/
+lib/bundler/man
+pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
4 Gemfile
@@ -0,0 +1,4 @@
+source 'https://rubygems.org'
+
+# Specify your gem's dependencies in nesta-plugin-cache.gemspec
+gemspec
14 LICENSE
@@ -0,0 +1,14 @@
+Copyright (c) 2012 Max Sadrieh
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
67 README.md
@@ -0,0 +1,67 @@
+# Nesta::Plugin::Cache
+
+This plugin provides caching directives on each page that lets Nesta return
+appropriate caching headers to the client and any proxy between the server and
+the client. This improves performance.
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+ gem 'nesta-plugin-cache'
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself as:
+
+ $ gem install nesta-plugin-cache
+
+## Usage
+
+### Global settings
+
+Set the entries `expires` and `expires\_type` to the appropriate values in your
+`config.yml`.
+
+ expires
+ The amount of time each page can be cached for by the client. If your
+ pages changes very often, set this to a small value. If on the other hand
+ your pages are mostly static, set it to a large value. Within the time you
+ put, a browser that has a copy of the file in its cache might choose not
+ to requery your website for the latest version. Can be overriden in
+ individual pages. In seconds.
+ Default: 0
+
+ expires: 3600
+
+ expires\_type
+ Whether or not proxies can cache the pages we serve. If set to public,
+ both proxies between the browser and the web server and the browser itself
+ can cache the pages. If set to private, only the client's browser can.
+ In practice, if the pages are modified depending on who requests
+ them, you should set this to private. Otherwise if all clients get the
+ same page (usually the case, particularly on nesta websites), set it to
+ public.
+ Default: public
+
+ expires\_type: private
+
+### Per-page settings
+
+If you want to change the global settings on a page by page basis (for a page
+that is updated very often for example), you can set the appropriate meta data
+fields: `Expires` and `Expires Type`.
+
+For more info on the expires types, see the following RFC:
+
+* http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.1
+
+## Contributing
+
+1. Fork it
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Added some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. Create new Pull Request
2 Rakefile
@@ -0,0 +1,2 @@
+#!/usr/bin/env rake
+require "bundler/gem_tasks"
3 lib/nesta-plugin-cache.rb
@@ -0,0 +1,3 @@
+require "nesta-plugin-cache/version"
+
+Nesta::Plugin.register(__FILE__)
52 lib/nesta-plugin-cache/init.rb
@@ -0,0 +1,52 @@
+module Nesta
+ module Plugin
+ module Cache
+ module Helpers
+ # If your plugin needs any helper methods, add them here...
+ end
+ end
+ end
+
+ class Config
+ @settings += %w[expires expires_type]
+ end
+
+ class Page
+ def expires
+ metadata('expires').nil? ? nil : metadata('expires').to_i
+ end
+
+ def expires_type
+ metadata('expires type').nil? ? nil : metadata('expires type').to_sym
+ end
+ end
+
+ class App
+ helpers Nesta::Plugin::Cache::Helpers
+
+ # Add the Nesta config entries to the Sinatra settings object
+ set :expires_time, Config.expires
+ set :expires_type, (Config.expires_type.nil? ?
+ nil : Config.expires_type.to_sym)
+
+ # The default expires values
+ before do
+ unless settings.expires_time.nil?
+ expires settings.expires_time,
+ settings.expires_type || :public,
+ :must_revalidate
+ end
+ end
+
+ after do
+ # Check that this is a regular page (ie not the sitemap, atom feed, an
+ # attachement or a CSS stylesheet): these don't have metadata; and check
+ # that the page defined a per-page expires directive.
+ unless @page.nil? || @page.expires.nil?
+ expires @page.expires,
+ @page.expires_type || settings.expires_type || :public,
+ :must_revalidate
+ end
+ end
+ end
+end
7 lib/nesta-plugin-cache/version.rb
@@ -0,0 +1,7 @@
+module Nesta
+ module Plugin
+ module Cache
+ VERSION = "0.0.1"
+ end
+ end
+end
28 nesta-plugin-cache.gemspec
@@ -0,0 +1,28 @@
+# -*- encoding: utf-8 -*-
+require File.expand_path('../lib/nesta-plugin-cache/version', __FILE__)
+
+Gem::Specification.new do |gem|
+ gem.authors = ["Max Sadrieh"]
+ gem.email = ["max.sadrieh@gmail.com"]
+ gem.description = %q{Cache-control headers plugin for Nesta, a Ruby CMS}
+ gem.summary = <<-EOF
+This Nesta plugin lets you define cache control headers globally for your site
+or on a page by page basis. This can improve performance tremendously since the
+client browsers can use cached versions of your pages for a set amount of time.
+
+The global values are set in the config.yml by using expires and expires_type
+and the per-page values are defined in each page's metadata by using the
+Expires and Expires Type fields.
+ EOF
+ gem.homepage = "http://www.sadrieh.me/projects/nesta-plugin-cache"
+
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ gem.files = `git ls-files`.split("\n")
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ gem.name = "nesta-plugin-cache"
+ gem.require_paths = ["lib"]
+ gem.version = Nesta::Plugin::Cache::VERSION
+ gem.add_dependency("nesta", "~> 0.9.11")
+ gem.add_dependency("sinatra", "~> 1.2")
+ gem.add_development_dependency("rake")
+end

0 comments on commit b2b560a

Please sign in to comment.
Something went wrong with that request. Please try again.