Easily build a great vocabulary without studying! Now on the App Store!
Click here for more info about the app

Roll Your Own Full Page Caching in Sinatra


It's as simple as this.

# app.rb
get '/:slug' do
  @slug   = File.basename("#{params[:slug]}")
  @cache = "public/cache/#{@slug}.html"

  if File.exists?(@cache)
    @post = Post.find_by(page: params[:slug])
    html = erb(:post)
    File.write(@cache, html)

Use File.basename to make sure that params[:page] doesn't contain path information. This way arbitrary requests aren't able to traverse the filesystem. Then just check to see if the file exists. If it does read and return it. Otherwise, got to the database, compile the template, write to disk and serve!

You will need to expire the cache if the resource changes. The simple approach is to add a callback to your model that removes the cache. You could also put this logic in the controller.

class Post < ActiveRecord::Base
  after_save do
    FileUtils.rm("public/cache/#{slug}.html") if File.exists?("public/cache/#{slug}.html")

There are a number of Gems for caching that are worth a look if you need something a little more robust. But this is a good start.

Tagged w/ #sinatra #caching #diy