Catalyst::Model::Memcached - Wrapper for memcached imitating Catalyst models
package MyCatalyst::Model::Token; use Moose; use namespace::autoclean; BEGIN { extends 'Catalyst::Model::Memcached' }; __PACKAGE__->config( args => { servers => [ '127.0.0.1:11211' ], namespace => 'db' } ); # Alternatively, this could be specified through config file __PACKAGE__->set_primary_key( qw/token/ ); __PACKAGE__->set_ttl( 300 ); sub BUILD { my $self = shift; $self->{__once_initialized_object} = Object->new; return $self; } sub create { my ($self, $hash) = @_; $hash->{token} = $self->{__once_initialized_object}->create_id(); return $self->SUPER::create($hash) } 1;
Simple Model for Catalyst for storing data in memcached
Warning Module requires perl >= 5.10 and Catalyst >= 5.8 !
One subclass of model handle one set of primary_key and ttl params. You can think of it as one table in regular DB.
In case you want to use memcached to store different entities through this model, you can configure it like this in config file:
Model: Cached: class: MyApp::Store::Cached config: args: servers: - 127.0.0.1:11211 namespace: 'db.' ttl: 86400
Assuming your model class is named MyApp::Model::Cached, your memcached server is started on localhost on port 11211. With this configuration all classes MyApp::Store::Cached::* will be loaded with same memcached configuration and default ttl of 86400.
Primary key could be the same in different classes - to avoid clashes keys that are stored in memcached are constructed like 'global_namespace.last_part_of_module_name.primary_key'.
$c->model( 'Cached::Token' )->create( { token => 'aaaa', signature => 'abcd' } );
Creates record in memcached with key = primary_key, data = hashref, expire = ttl. hashref must contains primary_key.
primary_key
hashref
ttl
$c->model( 'Cached::Token' )->search( { token => 'aaaa' } );
Searches data in memcached by primary_key key and returns memcached answer. hashref must contains primary_key.
The same as search.
Calls find, if nothing found - calls create.
Delete record with primary_key.
Denis Pokataev CPAN ID: CATONE Sponsored by Openstat.com catone@cpan.org
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the LICENSE file included with this module.
Catalyst, Cache::Memcached::Fast, perl(1).
To install Catalyst::Model::Memcached, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Catalyst::Model::Memcached
CPAN shell
perl -MCPAN -e shell install Catalyst::Model::Memcached
For more information on module installation, please visit the detailed CPAN module installation guide.