Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

API rate limit using Cache::Mmap need help

by swiftlet (Acolyte)
on Jul 26, 2020 at 06:01 UTC ( [id://11119813]=perlquestion: print w/replies, xml ) Need Help??

swiftlet has asked for the wisdom of the Perl Monks concerning the following question:

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?

Replies are listed 'Best First'.
Re: API rate limit using Cache::Mmap need help
by jcb (Parson) on Jul 27, 2020 at 03:00 UTC

    While I am unfamiliar with Cache::Mmap, your error suggests that it is using Storable internally. You are trying to store a simple value, but Storable only works with references. Try changing your code like so: (untested)

    my $key1 = "Test"; my $val1 = $cache->read($key1); print "read key1 count: ",$val1->{count},"\n"; $val1->{count}++; print "val1 count:",$val1->{count},"\n"; $cache->write($key1,$val1); $val1 = $cache->read($key1); print "read key1 count after write: ",$val1->{count},"\n";

    Note the change: the values stored in the cache are now hashrefs. You could also use scalar references, reading them with $$val1 and incrementing with ${$val1}++.

    Lastly, your code as written will not compile under strict. While it would not have caught this issue, good practice is to always use strict; and use warnings;.

Re: API rate limit using Cache::Mmap need help
by swiftlet (Acolyte) on Jul 29, 2020 at 08:34 UTC
    Thanks all for your input, and thanks perlfan for the suggestions about Memcached and Redis. I am using Memcached now, which is working perfectly fine in my threaded codes.
Re: API rate limit using Cache::Mmap need help
by Anonymous Monk on Jul 27, 2020 at 10:55 UTC
    Your code doesnt use threads
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11119813]
Approved by marto
Front-paged by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-26 04:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found