public
Description: Page Caching for Sinatra
Homepage:
Clone URL: git://github.com/kematzy/sinatra-cache.git
name age message
file .gitignore Tue Feb 24 09:31:33 -0800 2009 ignoring the gem build directory 'pkg' [kematzy]
file LICENSE Mon Jul 28 15:14:02 -0700 2008 Initial import of dirty version with many incom... [Kematzy]
file README.textile Sat Mar 14 02:13:50 -0700 2009 Updated the README with some further info and t... [kematzy]
file Rakefile Tue Feb 24 09:20:26 -0800 2009 Gemified the whole thing with Jeweler. First at... [kematzy]
file VERSION.yml Sat Mar 14 01:47:56 -0700 2009 Version bump to 0.2.2 [kematzy]
directory lib/ Sat Mar 14 02:10:03 -0700 2009 Put the <!-- page_cached_timestamp --> at the b... [kematzy]
file sinatra-cache.gemspec Sat Mar 14 02:14:31 -0700 2009 Bumped the version to 0.2.2 [kematzy]
directory test/ Sat Mar 14 02:10:03 -0700 2009 Put the <!-- page_cached_timestamp --> at the b... [kematzy]
README.textile

Sinatra::Cache

Adds a very simple Page Caching functionality to Sinatra


PLEASE KEEP THIS IN MIND

This is unfinished work, so use with care.

If you find it lacking or badly coded, please feel free to fork and improve. I’ll be very happy :)


TODO’s

= Make caching an option of the normal erb()/haml()/sass() methods, sort of like this:


  erb(:index, :cache => true/false)

  1. alternatively enable caching by setting a global config like this
    set :cache_enabled_for :all || :only/except => [‘/’,‘/contact’,…]

= Improve the logging output to the standard Sinatra output (if we are having :logging enabled )

= Add fragment caching…

= Enable the options functionality for the :cache & :cache_expire methods, so that they can take custom extensions and more


CONFIGURATION

Example 1: Sinatra ‘classic’ app.



require ‘sinatra/cache’

  1. toggle for the cache functionality.
    set :cache_enabled, true
  2. sets the default extension for cached files
    set :cache_page_extension, ‘.html’
  3. sets the Cache dir to the root of the /public directory.
    set :cache_output_dir, ’’ # was :cache_dir

Example 2: Sinatra ‘sub-classed’ app.


  <snip....>
  require 'sinatra/cache'
  
  class MyApp < Sinatra::Base
    
    register Sinatra::Cache
    
    # toggle for the cache functionality.
    set :cache_enabled, true
    # sets the default extension for cached files
    set :cache_page_extension, '.html'
    # sets the Cache output dir to the root of the /public directory.
    set :cache_output_dir, '' # was :cache_dir
    
    <snip....>

USAGE

Basic Page Caching into static HTML files


  get '/contact' do
    # NB!  options is currently not working
    cache( erb( :contact ), :options => 'should go here' )
  end

Expiring old pages (ie: after POST or PUT events)


  # not very good example
  post '/contact' do
    cache_expire( '/contact', options )
  end

See the test and test/fixtures directories for more examples.


DEFAULT OPTIONS

  • :cache_enabled => toggle for the cache functionality. Default is: true
  • :cache_page_extension => sets the default extension for cached files. Default is: .html
  • :cache_output_dir => sets cache directory where cached files are stored. Default is: ‘’(empty) == root of /public.
    set to empty, since the ideal ’system/cache/’ does not currently work with Passenger & mod_rewrite :(
  • :cache_logging => toggle for logging the cache calls. Default is: true
  • :cache_logging_level => sets the level of the cache logger. Default is: :info.
    Options:(unused atm) [:info, :warn, :debug]

Credits

A big Thank You! goes to rtomayko, blakemizerany and others working on the Sinatra framework.

Inspired by code from Rails & Merb
and sinatra-mailer