Skip to content

Commit

Permalink
Use Net::Async::Redis::XS if installed and PERL_REDIS_XS is defined
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-binary committed Dec 2, 2022
1 parent c5c721f commit 5012b7f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ requires 'Log::Any::Adapter::OpenTracing', '>= 0.001';
requires 'Metrics::Any::Adapter::Statsd', '>= 0.03';
# Transport
requires 'Net::Async::Redis', '>= 3.022';
recommends 'Net::Async::Redis::XS', '>= 0.004';
requires 'Net::Async::HTTP', '>= 0.48';
requires 'Net::Async::HTTP::Server', '>= 0.13';
# Introspection
Expand Down
22 changes: 20 additions & 2 deletions lib/Myriad/Transport/Redis.pm
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ use Net::Async::Redis::Cluster;

use List::Util qw(pairmap);

my $redis_class;
my $cluster_class;
BEGIN {
$redis_class = 'Net::Async::Redis';
$cluster_class = 'Net::Async::Redis::Cluster';
# Only enable XS mode on request
if($ENV{PERL_REDIS_XS}) {
eval {
require Net::Async::Redis::XS;
require Net::Async::Redis::Cluster::XS;
1;
} and do {
$redis_class = 'Net::Async::Redis::XS';
$cluster_class = 'Net::Async::Redis::Cluster::XS';
}
}
}

use Myriad::Exception::Builder category => 'transport_redis';

declare_exception 'NoSuchStream' => (
Expand Down Expand Up @@ -562,7 +580,7 @@ instance, depending on the setting of C<$use_cluster>.
async method redis () {
my $instance;
if($use_cluster) {
$instance = Net::Async::Redis::Cluster->new(
$instance = $cluster_class->new(
client_side_cache_size => $clientside_cache_size,
);
$self->add_child(
Expand All @@ -573,7 +591,7 @@ async method redis () {
port => $redis_uri->port,
);
} else {
$instance = Net::Async::Redis->new(
$instance = $redis_class->new(
host => $redis_uri->host,
port => $redis_uri->port,
client_side_cache_size => $clientside_cache_size,
Expand Down

0 comments on commit 5012b7f

Please sign in to comment.