Skip to content

adrianotadao/sinatra-fragment-cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages