Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A Sinatra Extension that makes Page and Fragment Caching easy
Ruby
Branch: wip
Pull request Compare This branch is 66 commits behind master.

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib/sinatra
test
.gitignore
LICENSE
README.textile
Rakefile
VERSION.yml
sinatra-cache.gemspec

README.textile

Sinatra::Cache

Adds a very simple Page Caching functionality to Sinatra

NB! 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 :)

TODOs

  • Improve the logging output to the standard Sinatra output (if we are having :logging enabled )
  • Add fragment and action (?) 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_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 dir to the root of the /public directory.
    set :cache_dir, '' 
  
    <snip....>

Usage

Basic Page Caching into static HTML files


  get '/contact' do
    # NB!  options is currently not working
    cache( erb( :contact ), :options => '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_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

Something went wrong with that request. Please try again.