| 1 | package PAGI::FastAPI::Queue::Driver::Memory; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use v5.38; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use experimental 'class'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use version; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | our $VERSION = qv('v1.7.3'); | |||||||||||||||||||||||||||
| 8 | 3 | our $AUTHORITY = 'cpan:MANWAR'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use PAGI::FastAPI::Queue::Driver; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 3 | class PAGI::FastAPI::Queue::Driver::Memory :isa(PAGI::FastAPI::Queue::Driver) { | |||||||||||||||||||||||||||
| 14 | 3 | field %queues; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 31 | ▶ | async method push ($topic, $payload) { | ||||||||||||||||||||||||||
|
Subroutine
push: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 31 | ▶ | push @{ $queues{$topic} //= [] }, $payload; | ||||||||||||||||||||||||||
|
Condition100%2/2
$queues{$topic} //= []
MC/DC100%1/1
$queues{$topic} //= []
$queues{$topic}
Truth table100%2/2
A
A= $queues{$topic}
| |||||||||||||||||||||||||||||
| 18 | 31 | return 1; | |||||||||||||||||||||||||||
| 19 | 3 | } | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 15 | ▶ | async method pop ($topic) { | ||||||||||||||||||||||||||
|
Subroutine
pop: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 15 | ▶ | return undef unless exists $queues{$topic} && @{ $queues{$topic} }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $queues{$topic} and @{$queues{$topic};}
MC/DC100%2/2
exists $queues{$topic} and @{$queues{$topic};}
exists $queues{$topic}
@{$queues{$topic};}
Truth table100%3/3
A and B
A= exists $queues{$topic}B= @{$queues{$topic};}
| |||||||||||||||||||||||||||||
| 23 | 11 | return shift @{ $queues{$topic} }; | |||||||||||||||||||||||||||
| 24 | 3 | } | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 21 | ▶ | async method size ($topic = undef) { | ||||||||||||||||||||||||||
|
Subroutine
size: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 21 | ▶ | if (defined $topic) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 16 | ▶ | return scalar @{ $queues{$topic} // [] }; | ||||||||||||||||||||||||||
|
Condition100%2/2
$queues{$topic} // []
MC/DC100%1/1
$queues{$topic} // []
$queues{$topic}
Truth table100%2/2
A
A= $queues{$topic}
| |||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 5 | my $total = 0; | |||||||||||||||||||||||||||
| 32 | 5 | $total += scalar @$_ for values %queues; | |||||||||||||||||||||||||||
| 33 | 5 | return $total; | |||||||||||||||||||||||||||
| 34 | 3 | } | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 2 | ▶ | method clear ($topic = undef) { | ||||||||||||||||||||||||||
|
Subroutine
clear: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | 2 | ▶ | if (defined $topic) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 1 | delete $queues{$topic}; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | else { | ||||||||||||||||||||||||||||
| 41 | 1 | %queues = (); | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | 2 | return $self; | |||||||||||||||||||||||||||
| 44 | 3 | } | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =encoding utf-8 | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =head1 NAME | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | PAGI::FastAPI::Queue::Driver::Memory - Default In-Memory Storage Driver for PAGI::FastAPI::Queue | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =head1 VERSION | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | Version v1.7.3 | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | use PAGI::FastAPI::Queue::Driver::Memory; | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | my $driver = PAGI::FastAPI::Queue::Driver::Memory->new; | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | $driver->push('emails', { to => 'a@b.com' })->get; | ||||||||||||||||||||||||||||
| 64 | my $item = $driver->pop('emails')->get; | ||||||||||||||||||||||||||||
| 65 | my $count = $driver->size('emails')->get; | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # Memory-driver-specific extension (not part of the base Driver contract) | ||||||||||||||||||||||||||||
| 68 | $driver->clear('emails'); # empty a single topic | ||||||||||||||||||||||||||||
| 69 | $driver->clear; # empty every topic | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | C<PAGI::FastAPI::Queue::Driver::Memory> is the standard implementation of | ||||||||||||||||||||||||||||
| 74 | L<PAGI::FastAPI::Queue::Driver>. It comes with the core L<PAGI::FastAPI> | ||||||||||||||||||||||||||||
| 75 | package and is utilised directly by L<PAGI::FastAPI::Queue> unless another | ||||||||||||||||||||||||||||
| 76 | driver is used. | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | All messages of a certain topic are stored as an array in an in-process | ||||||||||||||||||||||||||||
| 79 | hash table; C<push> adds a message and C<pop> removes it in C<FIFO> order. | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | B<Note:> Everything happens entirely in process memory and is kept private | ||||||||||||||||||||||||||||
| 82 | to the driver instance. Messages in queue are B<not> accessible from outside, | ||||||||||||||||||||||||||||
| 83 | that is from other processes (e.g. Starman, Hypnotoad) or different instances | ||||||||||||||||||||||||||||
| 84 | of C<PAGI::FastAPI::Queue::Driver::Memory>. Use distributed storage driver | ||||||||||||||||||||||||||||
| 85 | (e.g. Redis or SQS) in production. | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =head1 METHODS | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | Inherits C<push>, C<pop>, and C<size> from L<PAGI::FastAPI::Queue::Driver>. | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =head2 C<new()> | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | Instantiates a new in-memory queue driver. Takes no required arguments. | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =head2 C<push($topic, $payload)> | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | Appends C<$payload> (any scalar) to the end of C<$topic>. Returns a | ||||||||||||||||||||||||||||
| 98 | L<Future> resolving to C<1>. | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =head2 C<pop($topic)> | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | Removes and returns a L<Future> resolving to the item at the front of | ||||||||||||||||||||||||||||
| 103 | C<$topic>, or C<undef> if C<$topic> is empty or has never been pushed to. | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =head2 C<size($topic = undef)> | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | Returns a L<Future> resolving to the number of pending items in | ||||||||||||||||||||||||||||
| 108 | C<$topic>, or C<0> if unknown/empty. If C<$topic> is omitted (or | ||||||||||||||||||||||||||||
| 109 | C<undef>), resolves to the sum of pending items across every topic. | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =head2 C<clear($topic = undef)> | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | Memory-driver-specific extension, not part of the base | ||||||||||||||||||||||||||||
| 114 | L<PAGI::FastAPI::Queue::Driver> contract and not exposed through | ||||||||||||||||||||||||||||
| 115 | L<PAGI::FastAPI::Queue>. Empties C<$topic> if given, or every topic if | ||||||||||||||||||||||||||||
| 116 | omitted. Returns C<$self> for chaining. Synchronous (does not return a | ||||||||||||||||||||||||||||
| 117 | L<Future>). | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | L<PAGI::FastAPI::Queue::Driver>, L<PAGI::FastAPI::Queue> | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >> | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =head1 REPOSITORY | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | L<https://github.com/manwar/PAGI-FastAPI> | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =head1 BUGS | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/PAGI-FastAPI/issues>. | ||||||||||||||||||||||||||||
| 134 | I will be notified and then you'll automatically be notified of progress on your | ||||||||||||||||||||||||||||
| 135 | bug as I make changes. | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | perldoc PAGI::FastAPI::Queue::Driver::Memory | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | You can also look for information at: | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =over 4 | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =item * BUG Report | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | L<https://github.com/manwar/PAGI-FastAPI/issues> | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =item * Search MetaCPAN | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | L<https://metacpan.org/dist/PAGI-FastAPI/> | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =back | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | Copyright (C) 2026 Mohammad Sajid Anwar. | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | This program is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 162 | the terms of the Artistic License (2.0). | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =cut | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 3 | 1; # End of PAGI::FastAPI::Queue::Driver::Memory | |||||||||||||||||||||||||||