From 3abe3ae530ca922a77888c86261ba7b1dfcfce27 Mon Sep 17 00:00:00 2001 From: Tom Molesworth Date: Thu, 4 Jul 2024 11:11:41 +0800 Subject: [PATCH] Support for `list_count` storage method --- lib/Myriad/Role/Storage.pm | 17 +++++++++++++++++ lib/Myriad/Storage/Implementation/Memory.pm | 4 ++++ lib/Myriad/Storage/Implementation/Redis.pm | 4 ++++ 3 files changed, 25 insertions(+) diff --git a/lib/Myriad/Role/Storage.pm b/lib/Myriad/Role/Storage.pm index 751c83ee..1daa2856 100644 --- a/lib/Myriad/Role/Storage.pm +++ b/lib/Myriad/Role/Storage.pm @@ -351,6 +351,7 @@ our @READ_METHODS = qw( hash_exists hash_count hash_as_list + list_count list_range orderedset_member_count orderedset_members @@ -483,6 +484,22 @@ suitable for assigning to a hash. method hash_as_list; +=head2 list_count + +Takes the following parameters: + +=over 4 + +=item * key + +=back + +Returns a L which will resolve to the integer count of values currently in the list. + +=cut + +method list_count; + =head2 list_range Takes the following parameters: diff --git a/lib/Myriad/Storage/Implementation/Memory.pm b/lib/Myriad/Storage/Implementation/Memory.pm index a199a508..a726a6d8 100644 --- a/lib/Myriad/Storage/Implementation/Memory.pm +++ b/lib/Myriad/Storage/Implementation/Memory.pm @@ -413,6 +413,10 @@ async method hash_as_list : Defer ($k) { return $data{$k}->%*; } +async method list_count : Defer ($k) { + return 0 + $data{$k}->@*; +} + async method list_range : Defer ($k, $start = 0, $end = -1) { my $len = 0 + $data{$k}->@* or return [ ]; diff --git a/lib/Myriad/Storage/Implementation/Redis.pm b/lib/Myriad/Storage/Implementation/Redis.pm index c9d8e29e..bb57e4d1 100644 --- a/lib/Myriad/Storage/Implementation/Redis.pm +++ b/lib/Myriad/Storage/Implementation/Redis.pm @@ -215,6 +215,10 @@ async method watch_keyspace ($keyspace) { }); } +async method list_count ($k) { + await $redis->llen($self->apply_prefix($k)); +} + async method list_range ($k, $start = 0, $end = -1) { await $redis->lrange($self->apply_prefix($k), $start, $end); }