Skip to content

Instantly share code, notes, and snippets.

View Gregg's full-sized avatar
🙄
Contemplating why Github needs a status bar.

Gregg Pollack Gregg

🙄
Contemplating why Github needs a status bar.
View GitHub Profile
# In your development.rb to enable caching in development mode
config.action_controller.perform_caching = true
# To change page_cache folder location in your environment.rb
config.action_controller.page_cache_directory = RAILS_ROOT + "/public/cache"
# Code snippets
caches_page :show, :index
expires_cache :controller => 'posts', :action => 'index'
expires_cache :controller => 'posts', :action => 'show', :id => @post
# Combining all your javascripts in production mode
<%= javascript_include_tag :all, :cache => true %>
<%= javascript_include_tag :all, :cache => "main" %>
<%= javascript_include_tag "product", "cart", "checkout", :cache => "shop" %>
# Same sort of stuff with stylesheets
<%= stylesheet_link_tag :all, :cache => true %>
# Declaring an asset host
ActionController::Base.asset_host = "assets.example.com"
# To add a folder for sweepers
# /config/environment.rb
config.load_paths += %W( #{RAILS_ROOT}/app/sweepers )
# The Typical Sweeper
# /app/sweepers/post_sweeper.rb
class PostSweeper < ActionController::Caching::Sweeper
observe Post
# action/fragment caching options for your environment.rb
config.cache_store = :memory_store
config.cache_store = :syncronized_memory_store
config.cache_store = :file_store, "/path/to/cache/directory"
config.cache_store = :drb_store, "druby://localhost:9192"
config.cache_store = :mem_cache_store
config.cache_store = :mem_cache_store, "123.456.78.9:1001"
config.cache_store = :mem_cache_store, { :namespace => 'storeapp' }
config.cache_store = :compressed_mem_cache_store
# Caching in the view
<% cache("#{@city.name}-photos") do %>
...
<% end %>
# Expiring the view
expire_fragment "#{city.name}-photos"
# Caching in the controller
write_fragment(key, content, options = nil)
# To configure memcached to be used
# /config/environment.rb
config.cache_store = :mem_cache_store
# Object Store cache commands
Rails.cache.read
Rails.cache.write
Rails.cache.fetch
# To set max-age
expires_in 10.minutes
# To set the etag
if stale?(:etag => @user)
# or
fresh_when(:etag => @user)
# You can also send in multiple models
# To properly use the client-side cache commands with a reverse-proxy cache
expires_in 10.minutes, :private => false, :public => true
fresh_when :last_modified => @user.updated_at.utc
headers['Cache-Control'] = 'public'
fresh_when :etag => @user
headers['Cache-Control'] = 'public'
# Used to graph results from autobench
#
# Usage: ruby autobench_grapher.rb result_from_autobench.tsv
#
# This will generate three svg & png graphs
require "rubygems"
require "scruffy"
require 'csv'
require 'yaml'
# Apache bench sample
$ ab -n 400 -c 10 http://127.0.0.1:3000/posts
# httperf sample
$ httperf --num-conns=100 --rate=10 --timeout=5
--server=localhost --port=3000 --uri=/posts