rubyforge.org/
magic_multi_connections/) allows you to connect to different databases concurrently
from the same application. This is very useful when using one master and several
read-only slaves serving the same models. The syntax is transparent; it uses module
namespaces and imports the models (ActiveRecord::Base subclasses) into the
namespaces.
For a single-master situation, you could define another database connection in
database.yml for the read slave:
read_slave:
adapter: postgresql
database: read_only_production
username: user
password: pass
host: read_slave_host
This database is backed by a module, which mirrors the ActiveRecord classes using
this database connection:
require 'magic_multi_connections'
module ReadSlave
establish_connection :read_slave
end
Now, all pre-existing models can be accessed through the read_slave connection by
prefixing the model class with ReadSlave::.
Caching | 119
# use the read-only connection
@user = ReadSlave::User.find(params[:id])
# write to the master (can't use @user.update_attributes because it would
# try to write to the read slave)
User.update(@user.id, :login => "new_login")
Caching
If you have far more reads than writes, model caching may help lighten the load on
the database server. The standard in-memory cache these days is memcached.* Developed
for LiveJournal, memcached is a distributed cache that functions as a giant
hashtable.
Pages:
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187