Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
81 lines (53 sloc) 1.96 KB


Bust caches before your users do (in your Rails app).

Works with memcache-client, Dalli, or memcached.


gem install background_cache

Dynamic Configuration

Create lib/background_cache_config.rb in your Rails app: do

  # Configure using block methods

  group('every_hour').layout(false).only("sections_teams_#{tag.permalink}") do
    Tag::League.find(:all).each do |tag|
      cache(:path => "/#{tag.permalink}")

  # Configure using options

  Tag::League.find(:all).each do |tag|
      # Route params
      :controller => 'sections',
      :action => 'teams',
      :tag => tag.permalink,

      # Or specify a path
      :path => "/#{tag.permalink}",

      # Background cache options
      :group => 'every_hour',
      :layout => false,
      :only => "sections_teams_#{tag.permalink}"

The only and except options take cache fragment ids or arrays of cache fragment ids.

If no fragment is specified, all of the action's caches will regenerate.

Rake Task

Add rake background_cache to cron. All cache configurations are busted every time it is run.

To run a specific group of caches, run rake background_cache[every_hour] (as per the example above).

Daemon Mode

Create config/background_cache.yml in your Rails app:

redis: localhost:6379/0

Start a background_cache daemon from your Rails app:

$ cd path/to/app
$ background_cache

Run caches via Ruby:

require 'background_cache'

client ='/path/to/app')

# Cache group
client.cache(:group => "every_hour")

# Manual cache
client.cache(:path => "/")
Jump to Line
Something went wrong with that request. Please try again.