lib/Myriad/Storage/Implementation/Redis.pm

filter: Statementstmt 100% 6 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 100% 8 / 8 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« lib/Myriad/Storage/Implementation/Memory.pm ↑ Summary lib/Myriad/Subscription/Implementation/Memory.pm »
1 package Myriad::Storage::Implementation::Redis;
2  
3 9 ▶use Myriad::Class does => [
Subroutine BEGIN: called CC=1 SCAR=0.0
4 'Myriad::Role::Storage'
5 9 ];
6  
7 our $VERSION = '1.001'; # VERSION
8 our $AUTHORITY = 'cpan:DERIV'; # AUTHORITY
9  
10 =encoding utf8
11  
12 =head1 NAME
13  
14 Myriad::Storage::Implementation::Redis - access to microservice storage via Redis
15  
16 =head1 DESCRIPTION
17  
18 This provides an implementation of L<Myriad::Role::Storage> using L<https://redis.io>
19 as the underlying storage mechanism and transport layer.
20  
21 See L<Myriad::Role::Storage> for API details.
22  
23 =cut
24  
25 9 ▶use constant STORAGE_PREFIX => 'storage';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 # L<Myriad::Transport::Redis> instance to manage the connections.
28 has $redis;
29  
30 BUILD (%args) {
31 $redis = delete $args{redis} // die 'need a Transport instance';
32 }
33  
34 =head2 apply_prefix
35  
36 Add the storage prefix to the key before sending it to Redis
37  
38 =cut
39  
40 method apply_prefix($key) {
41 return STORAGE_PREFIX . '.' . $key;
42 }
43  
44 =head2 get
45  
46 Takes the following parameters:
47  
48 =over 4
49  
50 =item * C<< $k >> - the relative key in storage
51  
52 =back
53  
54 Returns a L<Future> which will resolve to the corresponding value, or C<undef> if none.
55  
56 =cut
57  
58 async method get ($k) {
59 await $redis->get($self->apply_prefix($k));
60 }
61  
62 =head2 set
63  
64 Takes the following parameters:
65  
66 =over 4
67  
68 =item * C<< $k >> - the relative key in storage
69  
70 =item * C<< $v >> - the scalar value to set
71  
72 =back
73  
74 Note that references are currently B<not> supported - attempts to write an arrayref, hashref
75 or object will fail.
76  
77 Returns a L<Future> which will resolve on completion.
78  
79 =cut
80  
81 async method set ($k, $v) {
82 die 'value cannot be a reference for ' . $k . ' - ' . ref($v) if ref $v;
83 await $redis->set($self->apply_prefix($k) => $v);
84 }
85  
86 =head2 getset
87  
88 Takes the following parameters:
89  
90 =over 4
91  
92 =item * C<< $k >> - the relative key in storage
93  
94 =item * C<< $v >> - the scalar value to set
95  
96 =back
97  
98 Note that references are currently B<not> supported - attempts to write an arrayref, hashref
99 or object will fail.
100  
101 Returns a L<Future> which will resolve to the original value on completion.
102  
103 =cut
104  
105 async method getset ($k, $v) {
106 die 'value cannot be a reference for ' . $k . ' - ' . ref($v) if ref $v;
107 return await $redis->getset($self->apply_prefix($k) => $v);
108 }
109  
110 =head2 incr
111  
112 Takes the following parameters:
113  
114 =over 4
115  
116 =item * C<< $k >> - the relative key in storage
117  
118 =back
119  
120 Returns a L<Future> which will resolve to the corresponding incremented value, or C<undef> if none.
121  
122 =cut
123  
124 async method incr ($k) {
125 await $redis->incr($self->apply_prefix($k));
126 }
127  
128 =head2 observe
129  
130 Observe a specific key.
131  
132 Returns a L<Ryu::Source> which will emit the current and all subsequent values.
133  
134 =cut
135  
136 method observe ($k) {
137 return $redis->subscribe($self->apply_prefix($k));
138 }
139  
140 =head2 observe_namespace
141  
142 Observe and entire namespace.
143  
144 =cut
145  
146 async method watch_keyspace ($keyspace) {
147 my $sub = await $redis->watch_keyspace($self->apply_prefix($keyspace));
148 my $pattern = STORAGE_PREFIX . '\.';
149 return $sub->map(sub {
150 $_ =~ s/$pattern//;
151 return $_;
152 });
153 }
154  
155 =head2 push
156  
157 Takes the following parameters:
158  
159 =over 4
160  
161 =item * C<< $k >> - the relative key in storage
162  
163 =item * C<< $v >> - the scalar value to set
164  
165 =back
166  
167 Returns a L<Future> which will resolve to .
168  
169 =cut
170  
171 async method push ($k, @v) {
172 die 'value cannot be a reference for ' . $k . ' - ' . ref($_) for grep { ref } @v;
173 await $redis->rpush($self->apply_prefix($k), @v);
174 }
175  
176 =head2 unshift
177  
178 Takes the following parameters:
179  
180 =over 4
181  
182 =item *
183  
184 =back
185  
186 Returns a L<Future> which will resolve to .
187  
188 =cut
189  
190 async method unshift ($k, @v) {
191 die 'value cannot be a reference for ' . $k . ' - ' . ref($_) for grep { ref } @v;
192 await $redis->lpush($self->apply_prefix($k), @v);
193 }
194  
195 =head2 pop
196  
197 Takes the following parameters:
198  
199 =over 4
200  
201 =item *
202  
203 =back
204  
205 Returns a L<Future> which will resolve to .
206  
207 =cut
208  
209 async method pop ($k) {
210 await $redis->rpop($self->apply_prefix($k));
211 }
212  
213 =head2 shift
214  
215 Takes the following parameters:
216  
217 =over 4
218  
219 =item *
220  
221 =back
222  
223 Returns a L<Future> which will resolve to .
224  
225 =cut
226  
227 async method shift ($k) {
228 await $redis->lpop($self->apply_prefix($k));
229 }
230  
231 =head2 hash_set
232  
233 Takes the following parameters:
234  
235 =over 4
236  
237 =item *
238  
239 =back
240  
241 Returns a L<Future> which will resolve to .
242  
243 =cut
244  
245 async method hash_set ($k, $hash_key, $v) {
246 die 'value cannot be a reference for ' . $k . ' - ' . ref($v) if ref $v;
247 await $redis->hset($k, $self->apply_prefix($hash_key), $v);
248 }
249  
250 =head2 hash_get
251  
252 Takes the following parameters:
253  
254 =over 4
255  
256 =item *
257  
258 =back
259  
260 Returns a L<Future> which will resolve to the scalar value for this key.
261  
262 =cut
263  
264 async method hash_get ($k, $hash_key) {
265 await $redis->hget($k, $self->apply_prefix($hash_key));
266 }
267  
268 =head2 hash_add
269  
270 Takes the following parameters:
271  
272 =over 4
273  
274 =item *
275  
276 =back
277  
278 Returns a L<Future> indicating success or failure.
279  
280 =cut
281  
282 async method hash_add ($k, $hash_key, $v) {
283 $v //= 1;
284 die 'value cannot be a reference for ' . $k . ' - ' . ref($v) if ref $v;
285 await $redis->hincrby($k, $self->apply_prefix($hash_key), $v);
286 }
287  
288 =head2 hash_keys
289  
290 Takes the following parameters:
291  
292 =over 4
293  
294 =item *
295  
296 =back
297  
298 Returns a L<Future> which will resolve to a list of the keys in no defined order.
299  
300 =cut
301  
302 async method hash_keys ($k) {
303 }
304  
305 =head2 hash_values
306  
307 Takes the following parameters:
308  
309 =over 4
310  
311 =item *
312  
313 =back
314  
315 Returns a L<Future> which will resolve to a list of the values in no defined order.
316  
317 =cut
318  
319 async method hash_values ($k) {
320 }
321  
322 =head2 hash_exists
323  
324 Takes the following parameters:
325  
326 =over 4
327  
328 =item *
329  
330 =back
331  
332 Returns a L<Future> which will resolve to true if the key exists in this hash.
333  
334 =cut
335  
336 async method hash_exists ($k, $hash_key) {
337 }
338  
339 =head2 hash_count
340  
341 Takes the following parameters:
342  
343 =over 4
344  
345 =item *
346  
347 =back
348  
349 Returns a L<Future> which will resolve to the count of the keys in this hash.
350  
351 =cut
352  
353 async method hash_count ($k) {
354 }
355  
356 =head2 hash_as_list
357  
358 Takes the following parameters:
359  
360 =over 4
361  
362 =item *
363  
364 =back
365  
366 Returns a L<Future> which will resolve to a list of key/value pairs,
367 suitable for assigning to a hash.
368  
369 =cut
370  
371 async method hash_as_list ($k) {
372 }
373  
374  
375 =head2 orderedset_add
376  
377 Takes the following parameters:
378  
379 =over 4
380  
381 =item * C<< $k >> - the relative key in storage
382  
383 =item * C<< $s >> - the scalar score to be attached to member
384  
385 =item * C<< $m >> - the scalar member value
386  
387 =back
388  
389 Note that references are B<not> supported - attempts to write an arrayref, hashref
390 or object will fail.
391  
392 Redis sorted sets data structure family.
393 add a scored member value to a storage key
394  
395 Returns a L<Future> which will resolve on completion.
396  
397 =cut
398  
399 async method orderedset_add ($k, $s, $m) {
400 die 'score & member values cannot be a reference for key:' . $k . ' - ' . ref($m) . ref($s) if (ref $m or ref $s);
401 await $redis->zadd($self->apply_prefix($k), $s => $m);
402 }
403  
404 =head2 orderedset_remove_member
405  
406 Takes the following parameters:
407  
408 =over 4
409  
410 =item * C<< $k >> - the relative key in storage
411  
412 =item * C<< $m >> - the scalar member value
413  
414 =back
415  
416 Redis sorted sets data structure family.
417 removes a specific member from ordered set in storage
418  
419 Returns a L<Future> which will resolve on completion.
420  
421 =cut
422  
423 async method orderedset_remove_member ($k, $m) {
424 await $redis->zrem($self->apply_prefix($k), $m);
425 }
426  
427 =head2 orderedset_remove_byscore
428  
429 Takes the following parameters:
430  
431 =over 4
432  
433 =item * C<< $k >> - the relative key in storage
434  
435 =item * C<< $min >> - the minimum score
436  
437 =item * C<< $max >> - the max score
438  
439 =back
440  
441 Redis sorted sets data structure family.
442 removes all members with scores between minimum and maximum within an ordered set in storage
443  
444 Returns a L<Future> which will resolve on completion.
445  
446 =cut
447  
448 async method orderedset_remove_byscore ($k, $min, $max) {
449 await $redis->zremrangebyscore($self->apply_prefix($k), $min => $max);
450 }
451  
452 =head2 orderedset_member_count
453  
454 Takes the following parameters:
455  
456 =over 4
457  
458 =item * C<< $k >> - the relative key in storage
459  
460 =item * C<< $min >> - the minimum score
461  
462 =item * C<< $max >> - the max score
463  
464 =back
465  
466 Redis sorted sets data structure family.
467 gives the members count within the provided min and max scores in an ordered set.
468  
469 Returns a L<Future> which will resolve on completion.
470  
471 =cut
472  
473 async method orderedset_member_count ($k, $min = '-inf', $max = '+inf') {
474 await $redis->zcount($self->apply_prefix($k), $min => $max);
475 }
476  
477 =head2 orderedset_members
478  
479 Takes the following parameters:
480  
481 =over 4
482  
483 =item * C<< $k >> - the relative key in storage
484  
485 =item * C<< $min >> - the minimum score
486  
487 =item * C<< $max >> - the max score
488  
489 =item * C<< $with_score >> - flag to return members with or without scores
490  
491 =back
492  
493 Redis sorted sets data structure family.
494 gives list of members within the provided min and max scores in an ordered set.
495  
496 Returns a L<Future> which will resolve on completion.
497  
498 =cut
499  
500 async method orderedset_members ($k, $min = '-inf', $max = '+inf', $with_score = 0) {
501 await $redis->zrange($self->apply_prefix($k), $min => $max, 'BYSCORE', ($with_score ? 'WITHSCORES' : ()));
502 }
503  
504 1;
505  
506 =head1 AUTHOR
507  
508 Deriv Group Services Ltd. C<< DERIV@cpan.org >>.
509  
510 See L<Myriad/CONTRIBUTORS> for full details.
511  
512 =head1 LICENSE
513  
514 Copyright Deriv Group Services Ltd 2020-2022. Licensed under the same terms as Perl itself.
515  
« lib/Myriad/Storage/Implementation/Memory.pm ↑ Summary lib/Myriad/Subscription/Implementation/Memory.pm »