I am going to implement an API rate limit function, allow several number of calls per second. Cache::FastMmap was my first choice, however, it's not working under threads. I am looking at Cache::Mmap, but I can't get it working even using the example from the module, it failed at write

use Cache::Mmap; my %options = ( expiry => 3, ); my $filename = "/tmp/cache-test"; $cache=Cache::Mmap->new($filename,\%options); my $key1 = "Test"; $val1=$cache->read($key1); print "read key1: $val1\n"; $val1++; print "val1: $val1\n"; $cache->write($key1,$val1); $val1=$cache->read($key1); print "read key1 after write: $val1\n";

Error message:

main::(Cache-Mmap.pl:13): print "val1: $val1\n"; DB<1> val1: 1 main::(Cache-Mmap.pl:14): $cache->write($key1,$val1); DB<1> not a reference at /usr/local/lib64/perl5/Cache/Mmap.pm line 1050. at /usr/lib64/perl5/vendor_perl/Storable.pm line 38. Storable::__ANON__[/usr/lib64/perl5/vendor_perl/Storable.pm:39 +]('not a reference') called at /usr/lib64/perl5/vendor_perl/Storable. +pm line 335 Storable::_freeze('CODE(0x185e640)', 1) called at /usr/lib64/p +erl5/vendor_perl/Storable.pm line 319 Storable::freeze(1) called at /usr/local/lib64/perl5/Cache/Mma +p.pm line 1050 Cache::Mmap::_encode('Cache::Mmap=HASH(0x197e3d0)', 1, 0) call +ed at /usr/local/lib64/perl5/Cache/Mmap.pm line 401 Cache::Mmap::write('Cache::Mmap=HASH(0x197e3d0)', 'Test', 1) c +alled at Cache-Mmap.pl line 14 Debugged program terminated. Use q to quit or R to restart, use o inhibit_exit to avoid stopping after program termination, h q, h R or h o to get additional info.

Any idea? Any other thread-safe modules for API rate limit implementation?


In reply to API rate limit using Cache::Mmap need help by swiftlet

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.