| 1 | package Service::Engine::Data::Memcached; | ||
| 2 | |||
| 3 | use 5.010; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Data::Dumper; | ||
| 7 | |||
| 8 | use Service::Engine; | ||
| 9 | use Cache::Memcached::Fast; | ||
| 10 | |||
| 11 | our $Config; | ||
| 12 | our $Log; | ||
| 13 | |||
| 14 | sub new { | ||
| 15 | |||
| 16 | my ($class,$options) = @_; | ||
| 17 | |||
| 18 | # set some defaults | ||
| 19 | my $attributes = {'handle'=>''}; | ||
| 20 | |||
| 21 | # load options | ||
| 22 | if (ref($options) eq 'HASH') { | ||
| 23 | foreach my $option (keys %{$options}) { | ||
| 24 | $attributes->{$option} = $options->{$option}; | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | # pull in some Service::Engine globals | ||
| 29 | $Config = $Service::Engine::Config; | ||
| 30 | $Log = $Service::Engine::Log; | ||
| 31 | |||
| 32 | if (!$attributes->{ip} || !$attributes->{port}) { | ||
| 33 | $Log->log({msg=>"memcached requires a port and ip",level=>1}); | ||
| 34 | } else { | ||
| 35 | my $thismemd = | ||
| 36 | new Cache::Memcached::Fast { | ||
| 37 | 'servers' => [ $attributes->{ip}.':'.$attributes->{port} ], | ||
| 38 | 'compress_threshold' => 10_000, | ||
| 39 | }; | ||
| 40 | $thismemd->enable_compress(1) if $thismemd; | ||
| 41 | $attributes->{'handle'} = $thismemd; | ||
| 42 | } | ||
| 43 | |||
| 44 | my $self = bless $attributes, $class; | ||
| 45 | |||
| 46 | return $self; | ||
| 47 | |||
| 48 | } | ||
| 49 | |||
| 50 | sub handle { | ||
| 51 | my ($self) = @_; | ||
| 52 | $Log->log({msg=>'memcache_ip: ' . $self->{'ip'} . ' port: ' . $self->{'port'},level=>3}); | ||
| 53 | return $self->{'handle'}; | ||
| 54 | } | ||
| 55 | |||
| 56 | 1; |