Repository is archived
No commit activity in last 3 years
No release in over 3 years
A simple redis backed cache for Sinatra
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

~> 10.0
~> 3.0
~> 1.3
 Project Readme

sinatra/redis-cache

A simple redis backed cache for Sinatra applications.

Installing

  • Add the gem to your Gemfile

    source 'https://rubygems.org'
    gem 'sinatra-redis-cache'
  • Require it in your app after including Sinatra

    require 'sinatra/redis-cache'

Using in your app

Use cache_do anywhere you want to cache the output of a block of code.

The following will be cached for 60 seconds with the key test-key.

cache_do('test-key', 60) do
  'do something here'
end

If there is an unexpired object in the cache with the same key, it will be returned. If not, the code will be executed, returned, and stored in the cache.

Configuration

Include the following block in your app after including sinatra/redis-cache to configure. Defaults are shown.

Sinatra::RedisCache::Config.config do
  redis_conn      Redis.new
  namespace       'sinatra_cache'
  default_expires 3600
  lock_timeout    5
  environments    [:production]
  logger          Logger.new(STDERR)
end