Never Sweep or Expire Cache!
One of my favorite advances in Rails 2.1 is cache handling. I've been a fan of CacheFu for a while now so to see better native caching show up in Rails is awesome. I work on a few sites that were done before data caching and action caching was as well developed as it is now.
Those sites make heavy use of fragment caching and as a result have emaciated controllers and big fat smart(logic heavy) views. They are a pain in the ass to update because the controller has to handle a lot of fragment expiration which can be cumbersome without common naming conventions or a map of object touch points. Taking a lesson from these projects I've worked out a method that I'm using as much as possible to save myself from writing expiration handling or sweepers.
The Idea
Use data caching (aka model caching) in conjunction with the key used for a web request. Huh? Keep an object cached and accessible by params[:id] then let your models expire the key after updates.
Action Caching
Most of the sites I work on are content heavy public facing apps so I'm framing this example with that in mind. During the 2008 Beijing Olympics we were able to handle the millions and millions of hits on the US Olympic Committee's TeamUSA site using Rails' built in Action Caching with CacheFu and Memcached. I love action caching but I hate writing logic to expire everything.
You absolutely get the best bang for your buck with action caching. Of course I know there will be times you can't use action caching. I do think most websites don't always need to have up-to-the-second content updates. That philosophy allows me to dismiss many of the issues people might have when using action caching with highly interactive websites or internal app sites.
Code
#app/controllers/pages_controller.rb
class PagesController < ApplicationController
# Use a proc to add the object's cache_key so you never need to expire your action cached pages.
# Every time the object updates you'll have a fresh key and action cache will look for a new key
caches_action :show, :cache_path => proc{ |c| { :cache_key => c.page.cache_key } }
...
# GET /pages/1
# GET /pages/1.xml
def show
@page = page
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @page }
end
end
...
def page
Page.get(params[:id])
end
end
CacheGet
CacheGet is a module that will assign some generic methods to your Rails modules. Requiring this file allows you to cache exact id matches and then automagically expire them on update.
# lib/cache_get.rb
# don't forget to `require 'cache_get'` in config/environment.rb
module CacheGet
def self.included(base) #:nodoc:
super
base.extend(ClassMethods)
base.after_save :delete_cache
base.after_destroy :delete_cache
end
module ClassMethods
def get(id)
Rails.cache.fetch(self.get_key(id)){ find(id) }
end
def get_key(id)
key = "CacheGet/#{self.name.tableize}/#{id}"
end
end
def delete_cache
Rails.cache.delete(self.class.get_key(self.id))
end
end
ActiveRecord::Base.send :include, CacheGet
Using the above with memcached will give you results around 400-500 reqs/sec depending on what else you've got going on.
Check back in a few days and I'll show how to do some extra magic like view counts without a database hit every request and updating an cached action when an associated object updates.
Snippy is a Freaking Ripoff of Pastie
Updated 2008-09-05 – Updated some instructions, fixed the github clone links and added a public demo: Snippy
Snippy is a Freaking Ripoff of Pastie (and I did the ripping ;-)
Snippy is an open source clone of Josh Goebel’s Pastie.
A lot of Pastie users (including me) wanted a private version. Snippy fills that void – enjoy.
I wrote it using Ruby on Rails 2.1 as a weekend project (about 2 months ago and I’m just now getting it out). I felt like one of the best features of Pastie is code highlighting so I made it a priority. Code highlighting is accomplished using a custom renderer with Ultraviolet. The Thinking Sphinx plugin has been on my radar for a little while now so I felt like it was a good time to throw that in.
On Github
Git has also been on my radar for a little while. So I figured I would host the project at Github – everyone wins. Get the source on Github.
Install
- git clone git://github.com/actsasflinn/snippy.git
- cd snippy
- cp config/database.yml.example config/database.yml
- sudo apt-get install libonig2 libonig-dev
- rake gems:install
- rake db:create
- rake db:migrate
- rake bootstrap
- rake thinking_sphinx:configure
- rake thinking_sphinx:index
- rake thinking_sphinx:start
- ./script/server
- Start Snipping
5 Reasons IE8 Will Rule over Firefox

- Developers
- Developers
- Developers
- Developers
- Developers…
So you may have heard that IE8 Beta 2 Shipped. It’s awesome…relative to any other previous release of IE. It’s like the Windows XP of Internet Explorers.
I’ve been developing web apps for a long time… which means I obligatorily hate IE. I still do hate it but I’m surprised at how good a release Microsoft has shipped. The look and feel is less bubble gum and a little more crisp. There is even a developer tool that’s almost as good as Firebug… no really. In addition to the dom browser there’s actually a javascript debugger in IE! I’m really amazed. They did manage to sneak in some hook ups to MSN’s crappy services but hey what do you expect?
I actually think there is something to the developer tools in IE8. Microsoft is actually listening to the biggest criticisms coming from the web development community about how difficult it is to develop for IE. I work on some fairly high profile sites along with some pretty large teams for names you’ve heard of (Time, Sports Illustrated, Turner, etc.) and I’m seeing the vast majority of web developers using Macs with Firefox (and Firebug of course). Microsoft really used to cater to developers but the web has always been one of the hardest markets for Microsoft to capture developer market share in. Why? Because Microsoft has made the web suck and our jobs a lot harder. But I think they are starting to get it.
So will IE8 rule over Firefox…not for me but I’m sure it will for the countless masses still using IE. The side benefit for all of us web slaves is an easier to use and program for version of IE. Now please Microsoft just phase out IE6!
Sorry had to put that there ;-)




