| 1 | package Myriad::Role::Storage; | ||
| 2 | |||
| 3 | 34 | ▶ | use Myriad::Class type => 'role'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 34 | our $VERSION = '1.001'; # VERSION | |
| 6 | 34 | our $AUTHORITY = 'cpan:DERIV'; # AUTHORITY | |
| 7 | |||
| 8 | =encoding utf8 | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Myriad::Role::Storage - microservice storage abstraction | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | my $storage = $myriad->storage; | ||
| 17 | await $storage->get('some_key'); | ||
| 18 | await $storage->hash_add('some_key', 'hash_key', 13); | ||
| 19 | |||
| 20 | =head1 DESCRIPTION | ||
| 21 | |||
| 22 | Provides an abstraction over the Redis-based data model used by L<Myriad> services. | ||
| 23 | |||
| 24 | For more information on the API design, please see the official | ||
| 25 | L<Redis commands list|https://redis.io/commands>. This model was | ||
| 26 | used as the basis for the methods even when non-Redis backend | ||
| 27 | storage systems are used. | ||
| 28 | |||
| 29 | =head1 Implementation | ||
| 30 | |||
| 31 | Note that this is defined as a rĂ´le, so it does not provide | ||
| 32 | a concrete implementation - instead, see classes such as: | ||
| 33 | |||
| 34 | =over 4 | ||
| 35 | |||
| 36 | =item * L<Myriad::Storage::Implementation::Redis> | ||
| 37 | |||
| 38 | =item * L<Myriad::Storage::Implementation::Memory> | ||
| 39 | |||
| 40 | =back | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | =head1 METHODS - Write | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | 34 | our @WRITE_METHODS = qw(set getset incr push unshift pop shift hash_set hash_add orderedset_add orderedset_remove_member orderedset_remove_byscore ); | |
| 49 | |||
| 50 | =head2 set | ||
| 51 | |||
| 52 | Takes the following parameters: | ||
| 53 | |||
| 54 | =over 4 | ||
| 55 | |||
| 56 | =item * C<< $k >> - the relative key in storage | ||
| 57 | |||
| 58 | =item * C<< $v >> - the scalar value to set | ||
| 59 | |||
| 60 | =back | ||
| 61 | |||
| 62 | Note that references are currently B<not> supported - attempts to write an arrayref, hashref | ||
| 63 | or object will fail. | ||
| 64 | |||
| 65 | Returns a L<Future> which will resolve on completion. | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | 34 | method set; | |
| 70 | |||
| 71 | =head2 getset | ||
| 72 | |||
| 73 | Performs the same operation as L</set>, but additionally returns the original key value, if any. | ||
| 74 | |||
| 75 | Takes the following parameters: | ||
| 76 | |||
| 77 | =over 4 | ||
| 78 | |||
| 79 | =item * C<< $k >> - the relative key in storage | ||
| 80 | |||
| 81 | =item * C<< $v >> - the scalar value to set | ||
| 82 | |||
| 83 | =back | ||
| 84 | |||
| 85 | Note that references are currently B<not> supported - attempts to write an arrayref, hashref | ||
| 86 | or object will fail. | ||
| 87 | |||
| 88 | Returns a L<Future> which will resolve on completion to the original value, or C<undef> if none. | ||
| 89 | |||
| 90 | =cut | ||
| 91 | |||
| 92 | 34 | method getset; | |
| 93 | |||
| 94 | =head2 push | ||
| 95 | |||
| 96 | Takes the following parameters: | ||
| 97 | |||
| 98 | =over 4 | ||
| 99 | |||
| 100 | =item * C<< $k >> - the relative key in storage | ||
| 101 | |||
| 102 | =item * C<< $v >> - the scalar value to set | ||
| 103 | |||
| 104 | =back | ||
| 105 | |||
| 106 | Returns a L<Future>. | ||
| 107 | |||
| 108 | =cut | ||
| 109 | |||
| 110 | 34 | method push; | |
| 111 | |||
| 112 | =head2 unshift | ||
| 113 | |||
| 114 | Takes the following parameters: | ||
| 115 | |||
| 116 | =over 4 | ||
| 117 | |||
| 118 | =item * | ||
| 119 | |||
| 120 | =back | ||
| 121 | |||
| 122 | Returns a L<Future>. | ||
| 123 | |||
| 124 | =cut | ||
| 125 | |||
| 126 | 34 | method unshift; | |
| 127 | |||
| 128 | =head2 pop | ||
| 129 | |||
| 130 | Returns a L<Future> which will resolve to the item removed from the list, | ||
| 131 | or C<undef> if none available. | ||
| 132 | |||
| 133 | =cut | ||
| 134 | |||
| 135 | 34 | method pop; | |
| 136 | |||
| 137 | =head2 shift | ||
| 138 | |||
| 139 | Returns a L<Future> which will resolve to the item removed from the list, | ||
| 140 | or C<undef> if none available. | ||
| 141 | |||
| 142 | =cut | ||
| 143 | |||
| 144 | 34 | method shift; | |
| 145 | |||
| 146 | =head2 hash_set | ||
| 147 | |||
| 148 | Takes the following parameters: | ||
| 149 | |||
| 150 | =over 4 | ||
| 151 | |||
| 152 | =item * | ||
| 153 | |||
| 154 | =back | ||
| 155 | |||
| 156 | Returns a L<Future> which will resolve to . | ||
| 157 | |||
| 158 | =cut | ||
| 159 | |||
| 160 | 34 | method hash_set; | |
| 161 | |||
| 162 | =head2 hash_add | ||
| 163 | |||
| 164 | Takes the following parameters: | ||
| 165 | |||
| 166 | =over 4 | ||
| 167 | |||
| 168 | =item * | ||
| 169 | |||
| 170 | =back | ||
| 171 | |||
| 172 | Returns a L<Future> indicating success or failure. | ||
| 173 | |||
| 174 | =cut | ||
| 175 | |||
| 176 | 34 | method hash_add; | |
| 177 | |||
| 178 | =head2 orderedset_add | ||
| 179 | |||
| 180 | Adds a member to an orderedset structure | ||
| 181 | Takes the following parameters: | ||
| 182 | |||
| 183 | =over 4 | ||
| 184 | |||
| 185 | =item * C<< $k >> - the relative key in storage | ||
| 186 | |||
| 187 | =item * C<< $s >> - the scalar value of the score attached to member | ||
| 188 | |||
| 189 | =item * C<< $m >> - the scalar value of member | ||
| 190 | |||
| 191 | =back | ||
| 192 | |||
| 193 | Returns a L<Future>. | ||
| 194 | |||
| 195 | =cut | ||
| 196 | |||
| 197 | 34 | method orderedset_add; | |
| 198 | |||
| 199 | =head2 orderedset_remove_memeber | ||
| 200 | |||
| 201 | Removes a member from an orderedset structure | ||
| 202 | Takes the following parameters: | ||
| 203 | |||
| 204 | =over 4 | ||
| 205 | |||
| 206 | =item * C<< $k >> - the relative key in storage | ||
| 207 | |||
| 208 | =item * C<< $m >> - the scalar value of member | ||
| 209 | |||
| 210 | =back | ||
| 211 | |||
| 212 | Returns a L<Future>. | ||
| 213 | |||
| 214 | =cut | ||
| 215 | |||
| 216 | 34 | method orderedset_remove_member; | |
| 217 | |||
| 218 | =head2 orderedset_remove_byscore | ||
| 219 | |||
| 220 | Removes members that have scores within the range passed from an orderedset structure | ||
| 221 | Takes the following parameters: | ||
| 222 | |||
| 223 | =over 4 | ||
| 224 | |||
| 225 | =item * C<< $k >> - the relative key in storage | ||
| 226 | |||
| 227 | =item * C<< $min >> - the value of minimum score | ||
| 228 | |||
| 229 | =item * C<< $max >> - the value of maximum score | ||
| 230 | |||
| 231 | =back | ||
| 232 | |||
| 233 | Returns a L<Future>. | ||
| 234 | |||
| 235 | =cut | ||
| 236 | |||
| 237 | 34 | method orderedset_remove_byscore; | |
| 238 | |||
| 239 | =head1 METHODS - Read | ||
| 240 | |||
| 241 | =cut | ||
| 242 | |||
| 243 | 34 | our @READ_METHODS = qw(get observe watch_keyspace hash_get hash_keys hash_values hash_exists hash_count hash_as_list orderedset_member_count orderedset_members); | |
| 244 | |||
| 245 | =head2 get | ||
| 246 | |||
| 247 | Takes the following parameters: | ||
| 248 | |||
| 249 | =over 4 | ||
| 250 | |||
| 251 | =item * C<< $k >> - the relative key in storage | ||
| 252 | |||
| 253 | =back | ||
| 254 | |||
| 255 | Returns a L<Future> which will resolve to the corresponding value, or C<undef> if none. | ||
| 256 | |||
| 257 | =cut | ||
| 258 | |||
| 259 | 34 | method get; | |
| 260 | |||
| 261 | =head2 observe | ||
| 262 | |||
| 263 | Observe a specific key. | ||
| 264 | |||
| 265 | Returns a L<Ryu::Observable> which will emit the current and all subsequent values. | ||
| 266 | |||
| 267 | =cut | ||
| 268 | |||
| 269 | 34 | method observe; | |
| 270 | |||
| 271 | =head2 hash_get | ||
| 272 | |||
| 273 | Takes the following parameters: | ||
| 274 | |||
| 275 | =over 4 | ||
| 276 | |||
| 277 | =item * | ||
| 278 | |||
| 279 | =back | ||
| 280 | |||
| 281 | Returns a L<Future> which will resolve to the scalar value for this key. | ||
| 282 | |||
| 283 | =cut | ||
| 284 | |||
| 285 | 34 | method hash_get; | |
| 286 | |||
| 287 | =head2 hash_keys | ||
| 288 | |||
| 289 | Takes the following parameters: | ||
| 290 | |||
| 291 | =over 4 | ||
| 292 | |||
| 293 | =item * | ||
| 294 | |||
| 295 | =back | ||
| 296 | |||
| 297 | Returns a L<Future> which will resolve to a list of the keys in no defined order. | ||
| 298 | |||
| 299 | =cut | ||
| 300 | |||
| 301 | 34 | method hash_keys; | |
| 302 | |||
| 303 | =head2 hash_values | ||
| 304 | |||
| 305 | Takes the following parameters: | ||
| 306 | |||
| 307 | =over 4 | ||
| 308 | |||
| 309 | =item * | ||
| 310 | |||
| 311 | =back | ||
| 312 | |||
| 313 | Returns a L<Future> which will resolve to a list of the values in no defined order. | ||
| 314 | |||
| 315 | =cut | ||
| 316 | |||
| 317 | 34 | method hash_values; | |
| 318 | |||
| 319 | =head2 hash_exists | ||
| 320 | |||
| 321 | Takes the following parameters: | ||
| 322 | |||
| 323 | =over 4 | ||
| 324 | |||
| 325 | =item * | ||
| 326 | |||
| 327 | =back | ||
| 328 | |||
| 329 | Returns a L<Future> which will resolve to true if the key exists in this hash. | ||
| 330 | |||
| 331 | =cut | ||
| 332 | |||
| 333 | 34 | method hash_exists; | |
| 334 | |||
| 335 | =head2 hash_count | ||
| 336 | |||
| 337 | Takes the following parameters: | ||
| 338 | |||
| 339 | =over 4 | ||
| 340 | |||
| 341 | =item * | ||
| 342 | |||
| 343 | =back | ||
| 344 | |||
| 345 | Returns a L<Future> which will resolve to the count of the keys in this hash. | ||
| 346 | |||
| 347 | =cut | ||
| 348 | |||
| 349 | 34 | method hash_count; | |
| 350 | |||
| 351 | =head2 hash_as_list | ||
| 352 | |||
| 353 | Takes the following parameters: | ||
| 354 | |||
| 355 | =over 4 | ||
| 356 | |||
| 357 | =item * | ||
| 358 | |||
| 359 | =back | ||
| 360 | |||
| 361 | Returns a L<Future> which will resolve to a list of key/value pairs, | ||
| 362 | suitable for assigning to a hash. | ||
| 363 | |||
| 364 | =cut | ||
| 365 | |||
| 366 | 34 | method hash_as_list; | |
| 367 | |||
| 368 | =head2 orderedset_member_count | ||
| 369 | |||
| 370 | Returns the count of members that have scores within the range passed from an orderedset structure | ||
| 371 | Takes the following parameters: | ||
| 372 | |||
| 373 | =over 4 | ||
| 374 | |||
| 375 | =item * C<< $k >> - the relative key in storage | ||
| 376 | |||
| 377 | =item * C<< $min >> - the value of minimum score | ||
| 378 | |||
| 379 | =item * C<< $max >> - the value of maximum score | ||
| 380 | |||
| 381 | =back | ||
| 382 | |||
| 383 | Returns a L<Future>. | ||
| 384 | |||
| 385 | =cut | ||
| 386 | |||
| 387 | 34 | method orderedset_member_count; | |
| 388 | |||
| 389 | =head2 orderedset_members | ||
| 390 | |||
| 391 | Returns the members that have scores within the range passed from an orderedset structure | ||
| 392 | Takes the following parameters: | ||
| 393 | |||
| 394 | =over 4 | ||
| 395 | |||
| 396 | =item * C<< $k >> - the relative key in storage | ||
| 397 | |||
| 398 | =item * C<< $min >> - the value of minimum score | ||
| 399 | |||
| 400 | =item * C<< $max >> - the value of maximum score | ||
| 401 | |||
| 402 | =back | ||
| 403 | |||
| 404 | Returns a L<Future>. | ||
| 405 | |||
| 406 | =cut | ||
| 407 | |||
| 408 | 34 | method orderedset_members; | |
| 409 | |||
| 410 | 34 | 1; | |
| 411 | |||
| 412 | =head1 AUTHOR | ||
| 413 | |||
| 414 | Deriv Group Services Ltd. C<< DERIV@cpan.org >>. | ||
| 415 | |||
| 416 | See L<Myriad/CONTRIBUTORS> for full details. | ||
| 417 | |||
| 418 | =head1 LICENSE | ||
| 419 | |||
| 420 | Copyright Deriv Group Services Ltd 2020-2022. Licensed under the same terms as Perl itself. | ||
| 421 | |||