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

NAME

Cache::Memcached::Fast::Logger - the simple logger object for writing and reading all log items to/from memcached

SYNOPSIS

    use Cache::Memcached::Fast::Logger;

    my $logger = Cache::Memcached::Fast::Logger->new( cache => Cache::Memcached::Fast->new(...) );

    # one or more processes log items to memcached like this method:
    $logger->log( \%item );

    # Other process - a parser of logs items reads all items by:
    $logger->read_all( sub { $item_hashref = shift; ... ; 1 } );

DESCRIPTION

Why this module? Sometime i need in helper for logging and parsing some statistics. To write in file a logs for parsing is very bad idea - I/O of HDD is very slow.

With this module many concurrent proccesses can write to memcached by "log" method and one process (for example a parser of logs) can read all logs in FIFO order from memcached by "read_all" method. This module is simple and it uses atomic "incr" in Cache::Memcached::Fast & "cas" in Cache::Memcached::Fast memcached's protocol methods (for internal counter of queue) for guarantee that all your items will not be lost during write phase in memcached (memcached doesn't guarantee a data keeping but if your cache has an enough free slabs you will not lose your log items)

CONSTRUCTOR

    my $logger = Cache::Memcached::Fast::Logger->new( %options )

OPTIONS

cache

Example:

    cache => Cache::Memcached::Fast->new(...)

Required. This option used and should be instance of Cache::Memcached::Fast object. All options of memcached specific features should be defined by creation of Cache::Memcached::Fast instance.

namespace

Example:

    namespace => 'log_1:'

Optional. This namespace will be used into inside "log" & "read_all" methods and restored from outside. If not defined the namespace will be as logger:. To see "namespace" in Cache::Memcached::Fast in details.

METHODS

log( $log_item )

$log_item cab be scalar, hashref or arrayref. It's serialized by Cache::Memcached::Fast.

read_all( $cb )

$cb is callback function (parser of one log item). It is called (by this way $cb->( $log_item )) for every item of log item written to memcached. This function should return true for continuation of parsing process (a log item will be deleted from cache) and false if callback wants to terminate a log reading proccess (a log item will not be deleted from cache so one will be read in next read_all again. It feature can be used for catching TERM signal for termination for example). This method is executed up to full reading process of log items from memcached.

NOTES

This module uses a following keys for log items (in namespace defined through same option): log_counter & log_N, where N is positive number from 0 to max integer of perl. After non-terminated <L/read_all> process a log_counter will be reseted to "0" (other processes will log from "0" again).

head1 AUTHOR

This module has been written by Perlover <perlover@perlover.com>, 2012 year

LICENSE

This module is free software and is published under the same terms as Perl itself.