Update: Changed max_age from 3 seconds to 1 hour. Was missed before posting.

Hi Jambo Hamon,

The following is a FCGI::ProcManager + MCE::Shared demonstration. One can make use of MCE::Shared and have a fast cache. Perhaps a nosql-like object is handy as well for session data during multi-page HTTP requests.

The MCE::Shared::Cache module is a hybrid LRU-plain implementation.

The MCE::Shared::Minidb nosql-like module is Redis-like for the API.

#!/usr/bin/perl # http://127.0.0.1/cgi-bin/test_shared.fcgi # http://127.0.0.1/cgi-bin/test_shared.fcgi?param1=foo&param2=baz use strict; use warnings; use MCE::Shared; use CGI::Fast; use FCGI::ProcManager; # construct any shared objects and mutexes (optional) my $cache = MCE::Shared->cache( max_keys => 500, max_age => 3600 ); my $count = MCE::Shared->scalar( 0 ); my $nosql = MCE::Shared->minidb(); # launch FCGI manager next my $proc_manager = FCGI::ProcManager->new({ n_processes => 4 }); $proc_manager->pm_manage(); # calling init enables parallel IPC for data flow # worker is assigned 1 of 12 channels MCE::Shared->init(); # at last, the loop while ( my $query = CGI::Fast->new() ) { $proc_manager->pm_pre_dispatch(); print "Content-type: text/html\r\n\r\n"; print "<hr>\n"; print "$_ = $ENV{$_}<br>\n" foreach sort keys %ENV; print "<hr>\n"; my %params; foreach ( sort $query->param() ) { $params{$_} = $query->param($_); print $_, " = ", $params{$_}, "<br>\n"; } print "<hr>\n"; print "$$: ", $count->incr(), "<br>\n"; my $val = $cache->get('foo') // do { $cache->set( foo => 'bar'.$count->get() ) }; print "$$: ", $val, "<br>\n"; $proc_manager->pm_post_dispatch(); }

For maximum performance, ensure Perl has Sereal::Encoder/Decoder 3.015+ installed. IO::FDPass 1.2+ is beneficial if wanting to construct a shared queue.

All was done in MCE::Shared::Cache and MCE::Shared::Minidb to run with low memory consumption and maximum performance. For example, MCE::Shared::Cache makes use of dualvar to hold the time expiration along with the key internally. Only enable what you want. Basically, do not enable max_age if not needed for maximum performance.

The OO interface for shared objects saves you from having to handle mutex at the application level, unless of course wanting to wrap a mutex (enter) around multiple shared actions.

Regards, Mario


In reply to Re^3: Why won't this Deadlock? by marioroy
in thread Why won't this Deadlock? by Jambo Hamon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.