| Catalyst-Model-DBIC-Schema documentation | Contained in the Catalyst-Model-DBIC-Schema distribution. |
Catalyst::TraitFor::Model::DBIC::Schema::Replicated - Replicated storage support for Catalyst::Model::DBIC::Schema
__PACKAGE__->config({
traits => ['Replicated']
connect_info =>
['dbi:mysql:master', 'user', 'pass'],
replicants => [
['dbi:mysql:slave1', 'user', 'pass'],
['dbi:mysql:slave2', 'user', 'pass'],
['dbi:mysql:slave3', 'user', 'pass'],
],
balancer_args => {
master_read_weight => 0.3
}
});
Sets your storage_type to DBIx::Class::Storage::DBI::Replicated and connects replicants provided in config. See that module for supported resultset attributes.
The default balancer_type in DBIx::Class::Storage::DBI::Replicated is
::Random.
Sets the
master_read_weight in DBIx::Class::Storage::DBI::Replicated::Balancer::Random to
1 by default, meaning that you have the same chance of reading from master as
you do from replicants. Set to 0 to turn off reads from master.
Array of connect_info settings for every replicant.
The following can be set via connect_info in Catalyst::Model::DBIC::Schema, or
as their own parameters. If set via separate parameters, they will override the
settings in connect_info.
Catalyst::Model::DBIC::Schema, DBIx::Class, DBIx::Class::Storage::DBI::Replicated, Catalyst::TraitFor::Model::DBIC::Schema::Caching
This program is free software, you can redistribute it and/or modify it under the same terms as Perl itself.
| Catalyst-Model-DBIC-Schema documentation | Contained in the Catalyst-Model-DBIC-Schema distribution. |
package Catalyst::TraitFor::Model::DBIC::Schema::Replicated; use namespace::autoclean; use Moose::Role; use Carp::Clan '^Catalyst::Model::DBIC::Schema'; use Catalyst::Model::DBIC::Schema::Types qw/ConnectInfos LoadedClass/; use MooseX::Types::Moose qw/Str HashRef/;
has replicants => ( is => 'ro', isa => ConnectInfos, coerce => 1, required => 1 ); has pool_type => (is => 'ro', isa => LoadedClass); has pool_args => (is => 'ro', isa => HashRef); has balancer_type => (is => 'ro', isa => Str); has balancer_args => (is => 'ro', isa => HashRef); after setup => sub { my $self = shift; # check storage_type compatibility (if configured) if (my $storage_type = $self->storage_type) { my $class = $storage_type =~ /^::/ ? "DBIx::Class::Storage$storage_type" : $storage_type; Class::MOP::load_class($class); croak "This storage_type cannot be used with replication" unless $class->isa('DBIx::Class::Storage::DBI::Replicated'); } else { $self->storage_type('::DBI::Replicated'); } my $connect_info = $self->connect_info; $connect_info->{pool_type} = $self->pool_type if $self->pool_type; $connect_info->{pool_args} = $self->pool_args if $self->pool_args; $connect_info->{balancer_type} = $self->balancer_type || $connect_info->{balancer_type} || '::Random'; $connect_info->{balancer_args} = $self->balancer_args || $connect_info->{balancer_args} || {}; $connect_info->{balancer_args}{master_read_weight} = 1 unless exists $connect_info->{balancer_args}{master_read_weight}; }; sub BUILD {} after BUILD => sub { my $self = shift; $self->storage->connect_replicants(map [ $_ ], @{ $self->replicants }); };
1;