Skip to content

Instantly share code, notes, and snippets.

@dstrelau
Created December 14, 2009 21:09
Show Gist options
  • Save dstrelau/256427 to your computer and use it in GitHub Desktop.
Save dstrelau/256427 to your computer and use it in GitHub Desktop.
Rails::Initializer.run do |config|
# Read from config/memcached.yml to setup Memcache instance
# Individual caching mechanisms are enabled in environment specific configs
require 'memcache'
mservers = if (File.exist?(mconfig = Rails.root+'config'+'memcached.yml'))
YAML.load_file(mconfig)[RAILS_ENV].delete('servers')
end
MemCacheConnection = MemCache.new(mservers || 'localhost:11211', :namespace => "_myapp")
end
# `load` this file in your deploy.rb
# add the :memcached role to your server with a private_ip attribute:
# server 'app01.myapp.com', :app, :memcached, :private_ip => '10.1.1.1'
# or just set(:memcached_servers) { ['10.1.1.1', '10.1.1.2'] }
_cset(:memcached_servers) do
if roles[:memcached].servers.any?
unless roles[:memcached].servers.all? {|s| s.options[:private_ip] }
abort "Set :private_ip for all :memcached servers or "+
"set :memcached_servers explicitly."
end
roles[:memcached].servers.map {|s| s.options[:private_ip] + ":11211" }
else
%w[localhost:11211]
end
end
namespace :deploy do
namespace :memcached do
desc "Generate the memcached.yml in the shared directory"
task :config, :roles => :app do
config = {
environment => {
'servers' => fetch(:memcached_servers)
}
}
put YAML.dump(config), "#{shared_path}/config/memcached.yml"
end
end
end
config.cache_store = :mem_cache_store, MemCacheConnection # Rails.cache
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment