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¶m2=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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |