Created
January 18, 2010 06:52
-
-
Save mikhailov/279839 to your computer and use it in GitHub Desktop.
Rails on Redis - is all about high performance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/controllers/posts_controller.rb | |
class PostsController < ApplicationController | |
def index | |
@posts.all_cached # Retrive only at once by every 5 minutes | |
expires_in 5.minutes, :private => false, :public => true | |
end | |
end | |
# app/models/post.rb | |
Class Post | |
def self.all_cached | |
Rails.cache.fetch(:posts) do | |
self.all | |
end | |
end | |
end | |
# config/initializer/session_store.rb | |
ActionController::Base.session_store = Rack::Session::Redis | |
# config/environment.rb | |
require 'rack/cache' | |
require "redis-store" | |
Rails::Initializer.run do |config| | |
... | |
config.middleware.use Rack::Cache, | |
:verbose => true, | |
:metastore => "redis://localhost:6379/0", | |
:entitystore => "redis://localhost:6379/1" | |
if $0 == 'script/server' | |
# Check for redis-store | |
begin | |
# check if redis-store is running; if it is, use that instead of the default memory cache | |
Timeout.timeout(0.5) { TCPSocket.open("localhost", 6379) { } } | |
config.cache_store = :redis_store | |
$stderr.puts "\033[0;32mUsing redis-store on localhost:6379\033[0;34m" | |
rescue StandardError | |
$stderr.puts "\033[0;34mredis-store not running, you should run of \033[0;31m$ redis-server\033[0;29m" | |
raise | |
end | |
# Check for sphinx | |
begin | |
# check if memcached is running; if it is, use that instead of the default memory cache | |
Timeout.timeout(0.5) { TCPSocket.open("localhost", 9312) { } } | |
$stderr.puts "\033[0;32mUsing sphinx on localhost:9312\033[0;29m" | |
rescue StandardError | |
$stderr.puts "\033[0;34msphinx not running, you should run of \033[0;31m$ rake ts:start\033[0;29m" | |
raise | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment