Skip to content

worldlywisdom/sinatra-redis-fragment-caching-helper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Sinatra Redis Fragment Caching Helper

A Sinatra helper extension that makes fragment caching with Redis easy.

Features

  • Makes fragment caching in Sinatra super simple.
  • Can be used in views and routes.
  • Expiration can be managed individually for each fragment.
  • The cache can be bypassed inline for debugging.
  • Works in both development and production via easy ENV["REDIS_URL"] configuration.
  • No dependencies outside of Sinatra and Redis.

Installation

Place the helper in a directory (like /app or /lib), then include it in your main Sinatra application. I usually use something like:

### Require all app files
Dir['./app/*.rb'].each { |f| require(f) }

Then, copy the commands in Rakefile into your application. Two commands are available, which will allow you to manage keys in the cache:

cache:reset # Remove all keys from the cache
cache:key:delete key="yourkey" # Remove a specific key from the cache

How To Use

Usage in ERB views or partials:

<% cache key: "cache-key", expires: 86400, bypass: false do %>
  This is the block to evaluate.
<% end %>

Usage in Sinatra routes:

get '/' do
  cache key: "cache-key", expires: 86400, bypass: false do
    This is the block to evaluate.
  end
end

Connecting To Redis

The helper relies on ENV["REDIS_URL"] to connect to Redis. This should automatically allow you to Redis in development, then switch over to production seamlessly.

Credits

Special thanks to Bryan Thompson for sharing a Gist that inspired this helper.

About

A Redis fragment caching helper for Sinatra

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages