Written by Sean Behan on Fri Aug 23rd 2013

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) File.read(@cache) else @post = Post.find_by(page: params[:slug]) html = erb(:post) File.write(@cache, html) html end end

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 with..
#Sinatra #Caching #DIY

Just finishing up brewing up some fresh ground comments...