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?