| 1 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 2 | # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de> | ||
| 3 | # File: GX/Cache/Memcached.pm | ||
| 4 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 5 | |||
| 6 | package GX::Cache::Memcached; | ||
| 7 | |||
| 8 | use GX::Exception; | ||
| 9 | |||
| 10 | use Cache::Memcached (); | ||
| 11 | |||
| 12 | |||
| 13 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 14 | # Class setup | ||
| 15 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 16 | |||
| 17 | use GX::Class; | ||
| 18 | |||
| 19 | extends 'GX::Cache'; | ||
| 20 | |||
| 21 | has 'memcached' => ( | ||
| 22 | isa => 'Object', | ||
| 23 | constraint => sub { $_->isa( 'Cache::Memcached' ) }, | ||
| 24 | initialize => 1, | ||
| 25 | accessors => { | ||
| 26 | '_get_memcached' => { type => 'get' }, | ||
| 27 | '_set_memcached' => { type => 'set' } | ||
| 28 | } | ||
| 29 | ); | ||
| 30 | |||
| 31 | has 'namespace' => ( | ||
| 32 | isa => 'Scalar', | ||
| 33 | initialize => 1, | ||
| 34 | accessors => { | ||
| 35 | '_get_namespace' => { type => 'get' }, | ||
| 36 | '_set_namespace' => { type => 'set' } | ||
| 37 | } | ||
| 38 | ); | ||
| 39 | |||
| 40 | has 'options' => ( | ||
| 41 | isa => 'Hash', | ||
| 42 | initialize => 1, | ||
| 43 | accessors => { | ||
| 44 | '_get_options' => { type => 'get_reference' } | ||
| 45 | } | ||
| 46 | ); | ||
| 47 | |||
| 48 | has 'servers' => ( | ||
| 49 | isa => 'Array', | ||
| 50 | initialize => 1, | ||
| 51 | accessors => { | ||
| 52 | '_get_servers' => { type => 'get_reference' } | ||
| 53 | } | ||
| 54 | ); | ||
| 55 | |||
| 56 | build; | ||
| 57 | |||
| 58 | |||
| 59 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 60 | # Export mechanism | ||
| 61 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 62 | |||
| 63 | __PACKAGE__->_install_import_method; | ||
| 64 | |||
| 65 | |||
| 66 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 67 | # Public methods | ||
| 68 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 69 | |||
| 70 | sub add { | ||
| 71 | |||
| 72 | return shift->memcached->add( @_ ); | ||
| 73 | |||
| 74 | } | ||
| 75 | |||
| 76 | sub clear { | ||
| 77 | |||
| 78 | return $_[0]->memcached->flush_all; | ||
| 79 | |||
| 80 | } | ||
| 81 | |||
| 82 | sub get { | ||
| 83 | |||
| 84 | my $invocant = shift; | ||
| 85 | |||
| 86 | if ( @_ > 1 ) { | ||
| 87 | |||
| 88 | my $data = $invocant->memcached->get_multi( @_ ); | ||
| 89 | |||
| 90 | if ( $data ) { | ||
| 91 | no warnings 'uninitialized'; | ||
| 92 | return wantarray ? @$data{@_} : $data; | ||
| 93 | } | ||
| 94 | else { | ||
| 95 | return; | ||
| 96 | } | ||
| 97 | |||
| 98 | } | ||
| 99 | else { | ||
| 100 | return $invocant->memcached->get( $_[0] ); | ||
| 101 | } | ||
| 102 | |||
| 103 | } | ||
| 104 | |||
| 105 | sub memcached { | ||
| 106 | |||
| 107 | return $_[0]->instance->_get_memcached; | ||
| 108 | |||
| 109 | } | ||
| 110 | |||
| 111 | sub namespace { | ||
| 112 | |||
| 113 | return $_[0]->instance->_get_namespace; | ||
| 114 | |||
| 115 | } | ||
| 116 | |||
| 117 | sub options { | ||
| 118 | |||
| 119 | return %{$_[0]->instance->_get_options}; | ||
| 120 | |||
| 121 | } | ||
| 122 | |||
| 123 | sub remove { | ||
| 124 | |||
| 125 | return shift->memcached->remove( @_ ); | ||
| 126 | |||
| 127 | } | ||
| 128 | |||
| 129 | sub replace { | ||
| 130 | |||
| 131 | return shift->memcached->replace( @_ ); | ||
| 132 | |||
| 133 | } | ||
| 134 | |||
| 135 | sub set { | ||
| 136 | |||
| 137 | return shift->memcached->set( @_ ); | ||
| 138 | |||
| 139 | } | ||
| 140 | |||
| 141 | sub servers { | ||
| 142 | |||
| 143 | return @{$_[0]->instance->_get_servers}; | ||
| 144 | |||
| 145 | } | ||
| 146 | |||
| 147 | |||
| 148 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 149 | # Private methods | ||
| 150 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 151 | |||
| 152 | sub _create_memcached { | ||
| 153 | |||
| 154 | my $self = shift; | ||
| 155 | |||
| 156 | my $memcached = eval { | ||
| 157 | Cache::Memcached->new( | ||
| 158 | $self->options, | ||
| 159 | namespace => $self->namespace, | ||
| 160 | servers => [ $self->servers ] | ||
| 161 | ); | ||
| 162 | }; | ||
| 163 | |||
| 164 | if ( ! $memcached ) { | ||
| 165 | GX::Exception->throw( | ||
| 166 | message => "Cannot instantiate Cache::Memcached", | ||
| 167 | subexception => $@ | ||
| 168 | ); | ||
| 169 | } | ||
| 170 | |||
| 171 | return $memcached; | ||
| 172 | |||
| 173 | } | ||
| 174 | |||
| 175 | sub _setup { | ||
| 176 | |||
| 177 | my $self = shift; | ||
| 178 | my $args = shift; | ||
| 179 | |||
| 180 | $self->SUPER::_setup( $args ); | ||
| 181 | |||
| 182 | $self->_setup_memcached; | ||
| 183 | |||
| 184 | return; | ||
| 185 | |||
| 186 | } | ||
| 187 | |||
| 188 | sub _setup_memcached { | ||
| 189 | |||
| 190 | my $self = shift; | ||
| 191 | |||
| 192 | if ( ! $self->memcached ) { | ||
| 193 | $self->_set_memcached( $self->_create_memcached ); | ||
| 194 | } | ||
| 195 | |||
| 196 | return; | ||
| 197 | |||
| 198 | } | ||
| 199 | |||
| 200 | sub _setup_config { | ||
| 201 | |||
| 202 | my $self = shift; | ||
| 203 | my $args = shift; | ||
| 204 | |||
| 205 | if ( exists $args->{'namespace'} ) { | ||
| 206 | $self->_set_namespace( delete $args->{'namespace'} ); | ||
| 207 | } | ||
| 208 | else { | ||
| 209 | $self->_set_namespace( ref $self ); | ||
| 210 | } | ||
| 211 | |||
| 212 | if ( exists $args->{'servers'} ) { | ||
| 213 | |||
| 214 | my $servers = delete $args->{'servers'}; | ||
| 215 | |||
| 216 | if ( ref $servers ne 'ARRAY' ) { | ||
| 217 | throw "Invalid option (\"servers\" must be an array reference)"; | ||
| 218 | } | ||
| 219 | |||
| 220 | @{$self->_get_servers} = @$servers; | ||
| 221 | |||
| 222 | } | ||
| 223 | else { | ||
| 224 | @{$self->_get_servers} = qw( 127.0.0.1:11211 ); | ||
| 225 | } | ||
| 226 | |||
| 227 | if ( exists $args->{'options'} ) { | ||
| 228 | |||
| 229 | my $options = delete $args->{'options'}; | ||
| 230 | |||
| 231 | if ( ref $options ne 'HASH' ) { | ||
| 232 | throw "Invalid option (\"options\" must be a hash reference)"; | ||
| 233 | } | ||
| 234 | |||
| 235 | %{$self->_get_options} = %$options; | ||
| 236 | |||
| 237 | } | ||
| 238 | |||
| 239 | $self->SUPER::_setup_config( $args ); | ||
| 240 | |||
| 241 | return; | ||
| 242 | |||
| 243 | } | ||
| 244 | |||
| 245 | sub _start { | ||
| 246 | |||
| 247 | my $self = shift; | ||
| 248 | |||
| 249 | $self->memcached->disconnect_all; | ||
| 250 | |||
| 251 | return; | ||
| 252 | |||
| 253 | } | ||
| 254 | |||
| 255 | |||
| 256 | 1; | ||
| 257 | |||
| 258 | __END__ |