The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Catalyst::Model::Memcached - Wrapper for memcached imitating Catalyst models

SYNOPSIS

  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;

DESCRIPTION

Simple Model for Catalyst for storing data in memcached

USAGE

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'.

METHODS

create( hashref )
  $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.

search( hashref )
  $c->model( 'Cached::Token' )->search( { token => 'aaaa' } );

Searches data in memcached by primary_key key and returns memcached answer. hashref must contains primary_key.

find( hashref )

The same as search.

find_or_new( hashref )

Calls find, if nothing found - calls create.

delete( hashref )

Delete record with primary_key.

AUTHOR

    Denis Pokataev
    CPAN ID: CATONE
    Sponsored by Openstat.com
    catone@cpan.org

COPYRIGHT

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.

SEE ALSO

Catalyst, Cache::Memcached::Fast, perl(1).