No commit activity in last 3 years
No release in over 3 years
Creating a fragment cache with sinatra!
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 5.5

Runtime

~> 1.4
 Project Readme

Sinatra::FragmentCache

This gem is to be used to generate cache similar to FragmentCache. You need to pass the arguments: a block of code, the cache key and the minutos that the caches should expires.

Requirements

  • Ruby 1.9.3 or greater
  • Sinatra ~>1.4

Installation

# Gemfile
source :rubygems

gem 'sinatra', '~1.4'
gem 'sinatra_fragment_cache', '0.0.1'

Gem options

Default

app.set :fragment_cache_enabled, true
app.set :fragment_cache_output_dir, lambda { "#{ app.root }/tmp/cache" }

Using

Simple usage

require 'rubygems'
require 'bundler'
Bundler.require :default

module DummyApp < Sinatra::Base
  register Sinatra::FragmentCache

  get '/' do
    fragment_cache('cache_key') do
      { foo: 'Bar' }.to_json
    end
  end
end

Options of cache generation

You can pass these options on the caller of the cache generator:

expires_in: Should be an Integer. It is the time in minutes of cache expiration.

file_name: Should be a String. It is the name that the cache will be created.

require 'rubygems'
require 'bundler'
Bundler.require :default

module DummyApp < Sinatra::Base
  register Sinatra::FragmentCache

  get '/' do
    fragment_cache('cache_key', file_name: 'index.json', expires_in: 60) do
      { foo: 'Bar' }.to_json
    end
  end
end

Disabling the cache on environment development

require 'rubygems'
require 'bundler'
Bundler.require :default

module DummyApp < Sinatra::Base
  register Sinatra::FragmentCache

  configure :development do
    set :fragment_cache_enabled, false
  end

  get '/' do
    fragment_cache('cache_key') do
      { foo: 'Bar' }.to_json
    end
  end
end

Changing the cache path

require 'rubygems'
require 'bundler'
Bundler.require :default

module DummyApp < Sinatra::Base
  register Sinatra::FragmentCache

  configure do
    set :fragment_cache_output_dir, "#{ FileUtils.pwd }/public/cache"
  end

  get '/' do
    fragment_cache('cache_key') do
      { foo: 'Bar' }.to_json
    end
  end
end

License

MIT