Sinatralli
A Sinatra Dalli Gem for page, fragment and key value caching.
Installation
Add this line to your application's Gemfile:
gem 'sinatralli'
And then execute:
$ bundle
Or install it yourself as:
$ gem install sinatralli
Dependencies
This gem uses Dalli, a high performance memcached client for Ruby.
Getting started
If you're using a modern structure, you'll have to register Sinatralli.
class CatsAreAwesome < Sinatra::Base
register Sinatra::Sinatralli
get '/' do
erb :index
end
end
If you're using the classic structure, you'll just have to require it.
require "sinatralli"
get '/' do
erb :index
end
Default settings
There are a few settings
# Default cache server
set :cache_server, 'localhost:11211'
# Enable cache
set :cache_enabled, true
# Default environment
set :cache_environment, :production
# Default expires
set :cache_expires, 600
# Minify the page output
set :cache_minify_pages, true
# Cache pages
set :cache_pages, true
# Cache logging
set :cache_logging, true
Page caching
Page caching is enabled by default, you can turn off all page caching by setting :cache_pages
to false
.
Cache key: _sd_page_sitename.com/path_bar
You can also overide default settings for a specific page.
get '/foo' do
erb :bar, cache: false, expires: 10, minify: false
end
Fragment caching
Fragment caching is fairly straight forward, you can also set a custom expiry.
Cache key: _sd_frag_sitename.com/path_bar
<% cache 'bar', expires: 300 do %>
...
<% end %>
Key value caching
Store an item simple, there are 3 parameters: key, value & expires
cache_set('key', 'your value', 300)
To retretive an item, you just specify the key:
cache_get('key')
Delete cache
Delete an item by key:
cache_delete('key')
Flush all cache:
cache_flush
License
MIT License