1MCE::Shared::Cache(3) User Contributed Perl DocumentationMCE::Shared::Cache(3)
2
3
4

NAME

6       MCE::Shared::Cache - A hybrid LRU-plain cache helper class
7

VERSION

9       This document describes MCE::Shared::Cache version 1.864
10

DESCRIPTION

12       A cache helper class for use as a standalone or managed by MCE::Shared.
13
14       This module implements a least-recently used (LRU) cache with its
15       origin based on MCE::Shared::Ordhash, for its performance and low-
16       memory characteristics.  It is both a LRU and plain implementation. LRU
17       logic is applied to new items and subsequent updates. A fetch however,
18       involves LRU reorder only if the item is found in the lower section of
19       the cache. This equates to extra performance for the upper section as
20       fetch behaves similarly to accessing a plain cache.  Upon reaching its
21       size restriction, it prunes items from the bottom of the cache.
22
23       The 50% LRU-mode (bottom section), 50% plain-mode (upper-section)
24       applies to fetches only.
25

SYNOPSIS

27        # non-shared or local construction for use by a single process
28
29        use MCE::Shared::Cache;
30
31        my $ca;
32
33        $ca = MCE::Shared::Cache->new(); # max_keys => undef, max_age => undef
34        $ca = MCE::Shared::Cache->new( { max_keys => 500 }, @pairs );
35
36        $ca = MCE::Shared::Cache->new( max_keys => "unlimited", max_age => "never" );
37        $ca = MCE::Shared::Cache->new( max_keys => undef, max_age => undef ); # ditto
38        $ca = MCE::Shared::Cache->new( max_keys => 500, max_age => "1 hour" );
39        $ca = MCE::Shared::Cache->new( max_keys => "4 KiB" ); # 4*1024
40        $ca = MCE::Shared::Cache->new( max_keys => "1 MiB" ); # 1*1024*1024
41
42        $ca = MCE::Shared::Cache->new( max_age  => "43200 seconds" );
43        $ca = MCE::Shared::Cache->new( max_age  => 43200 );   # ditto
44        $ca = MCE::Shared::Cache->new( max_age  => "720 minutes" );
45        $ca = MCE::Shared::Cache->new( max_age  => "12 hours" );
46        $ca = MCE::Shared::Cache->new( max_age  => "0.5 days" );
47        $ca = MCE::Shared::Cache->new( max_age  => "1 week" );
48        $ca = MCE::Shared::Cache->new( max_age  => undef );   # no expiration
49        $ca = MCE::Shared::Cache->new( max_age  => 0 );       # now
50
51        # construction for sharing with other threads and processes
52
53        use MCE::Shared;
54
55        my $ca;
56
57        $ca = MCE::Shared->cache(); # max_keys => undef, max_age => undef
58        $ca = MCE::Shared->cache( { max_keys => 500 }, @pairs );
59
60        $ca = MCE::Shared->cache( max_keys => "unlimited", max_age => "never" );
61        $ca = MCE::Shared->cache( max_keys => undef, max_age => undef ); # ditto
62        $ca = MCE::Shared->cache( max_keys => 500, max_age => "1 hour" );
63        $ca = MCE::Shared->cache( max_keys => "4 KiB" ); # 4*1024
64        $ca = MCE::Shared->cache( max_keys => "1 MiB" ); # 1*1024*1024
65
66        $ca = MCE::Shared->cache( max_age  => "43200 seconds" );
67        $ca = MCE::Shared->cache( max_age  => 43200 );   # ditto
68        $ca = MCE::Shared->cache( max_age  => "720 minutes" );
69        $ca = MCE::Shared->cache( max_age  => "12 hours" );
70        $ca = MCE::Shared->cache( max_age  => "0.5 days" );
71        $ca = MCE::Shared->cache( max_age  => "1 week" );
72        $ca = MCE::Shared->cache( max_age  => undef );   # no expiration
73        $ca = MCE::Shared->cache( max_age  => 0 );       # now
74
75        # hash-like dereferencing
76
77        my $val = $ca->{$key};
78        $ca->{$key} = $val;
79
80        %{$ca} = ();
81
82        # OO interface
83
84        if ( !defined ( $val = $ca->get("some_key") ) ) {
85           $val = $ca->set( some_key => "some_value" );
86        }
87
88        $val   = $ca->set( $key, $val );
89        $val   = $ca->get( $key );
90        $val   = $ca->delete( $key );              # del is an alias for delete
91        $bool  = $ca->exists( $key );
92        void   = $ca->clear();
93        $len   = $ca->len();                       # scalar keys %{ $ca }
94        $len   = $ca->len( $key );                 # length $ca->{ $key }
95
96        $iter  = $ca->iterator( @keys );           # ($key, $val) = $iter->()
97        @keys  = $ca->keys( @keys );               # @keys is optional
98        %pairs = $ca->pairs( @keys );
99        @vals  = $ca->values( @keys );             # vals is an alias for values
100
101        $len   = $ca->assign( $key/$val pairs );   # equivalent to ->clear, ->mset
102        $cnt   = $ca->mdel( @keys );
103        @vals  = $ca->mget( @keys );
104        $bool  = $ca->mexists( @keys );            # true if all keys exists
105        $len   = $ca->mset( $key/$val pairs );     # merge is an alias for mset
106
107        # included, sugar methods without having to call set/get explicitly
108
109        $len   = $ca->append( $key, $string );     #   $val .= $string
110        $val   = $ca->decr( $key );                # --$val
111        $val   = $ca->decrby( $key, $number );     #   $val -= $number
112        $val   = $ca->getdecr( $key );             #   $val--
113        $val   = $ca->getincr( $key );             #   $val++
114        $val   = $ca->incr( $key );                # ++$val
115        $val   = $ca->incrby( $key, $number );     #   $val += $number
116        $old   = $ca->getset( $key, $new );        #   $o = $v, $v = $n, $o
117
118        # pipeline, provides atomicity for shared objects, MCE::Shared v1.09+
119
120        @vals  = $ca->pipeline(                    # ( "a_a", "b_b", "c_c" )
121           [ "set", foo => "a_a" ],
122           [ "set", bar => "b_b" ],
123           [ "set", baz => "c_c" ],
124           [ "mget", qw/ foo bar baz / ]
125        );
126
127       For normal hash behavior, the TIE interface is supported.
128
129        # non-shared or local construction for use by a single process
130
131        use MCE::Shared::Cache;
132
133        tie my %ca, "MCE::Shared::Cache", max_keys => undef, max_age => undef;
134        tie my %ca, "MCE::Shared::Cache", max_keys => 500, max_age => "1 hour";
135        tie my %ca, "MCE::Shared::Cache", { max_keys => 500 }, @pairs;
136
137        # construction for sharing with other threads and processes
138        # one option is needed minimally to know to use MCE::Shared::Cache
139
140        use MCE::Shared;
141
142        tie my %ca, "MCE::Shared", max_keys => undef, max_age => undef;
143        tie my %ca, "MCE::Shared", max_keys => 500, max_age => "1 hour";
144        tie my %ca, "MCE::Shared", { max_keys => 500 }, @pairs;
145
146        # usage
147
148        my $val;
149
150        if ( !defined ( $val = $ca{some_key} ) ) {
151           $val = $ca{some_key} = "some_value";
152        }
153
154        $ca{some_key} = 0;
155
156        tied(%ca)->incrby("some_key", 20);
157        tied(%ca)->incrby(some_key => 20);
158

SYNTAX for QUERY STRING

160       Several methods take a query string for an argument. The format of the
161       string is described below. In the context of sharing, the query
162       mechanism is beneficial for the shared-manager process. It is able to
163       perform the query where the data resides versus the client-process grep
164       locally involving lots of IPC.
165
166        o Basic demonstration
167
168          @keys = $ca->keys( "query string given here" );
169          @keys = $ca->keys( "val =~ /pattern/" );
170
171        o Supported operators: =~ !~ eq ne lt le gt ge == != < <= > >=
172        o Multiple expressions delimited by :AND or :OR, mixed case allowed
173
174          "key eq 'some key' :or (val > 5 :and val < 9)"
175          "key eq some key :or (val > 5 :and val < 9)"
176          "key =~ /pattern/i :And val =~ /pattern/i"
177          "val eq foo baz :OR key !~ /pattern/i"
178
179          * key matches on keys in the cache
180          * likewise, val matches on values
181
182        o Quoting is optional inside the string
183
184          "key =~ /pattern/i :AND val eq 'foo bar'"   # val eq "foo bar"
185          "key =~ /pattern/i :AND val eq foo bar"     # val eq "foo bar"
186
187       Examples.
188
189        # search capability key/val: =~ !~ eq ne lt le gt ge == != < <= > >=
190        # key/val means to match against actual key/val respectively
191
192        @keys  = $ca->keys( "key eq 'some key' :or (val > 5 :and val < 9)" );
193        @keys  = $ca->keys( "key eq some key :or (val > 5 :and val < 9)" );
194
195        @keys  = $ca->keys( "key =~ /$pattern/i" );
196        @keys  = $ca->keys( "key !~ /$pattern/i" );
197        @keys  = $ca->keys( "val =~ /$pattern/i" );
198        @keys  = $ca->keys( "val !~ /$pattern/i" );
199
200        %pairs = $ca->pairs( "key == $number" );
201        %pairs = $ca->pairs( "key != $number :and val > 100" );
202        %pairs = $ca->pairs( "key <  $number :or key > $number" );
203        %pairs = $ca->pairs( "val <= $number" );
204        %pairs = $ca->pairs( "val >  $number" );
205        %pairs = $ca->pairs( "val >= $number" );
206
207        @vals  = $ca->vals( "key eq $string" );
208        @vals  = $ca->vals( "key ne $string with space" );
209        @vals  = $ca->vals( "key lt $string :or val =~ /$pat1|$pat2/" );
210        @vals  = $ca->vals( "val le $string :and val eq 'foo bar'" );
211        @vals  = $ca->vals( "val le $string :and val eq foo bar" );
212        @vals  = $ca->vals( "val gt $string" );
213        @vals  = $ca->vals( "val ge $string" );
214

API DOCUMENTATION

216       This module involves TIE when accessing the object via hash-like
217       behavior.  Both non-shared and shared instances are impacted if doing
218       so. Although likely fast enough for many use cases, the OO interface is
219       recommended for best performance.
220
221       Accessing an item is likely to involve moving its key to the top of the
222       cache.  Various methods described below state with "Reorder: Yes" or
223       "Reorder: No" as an indication.
224
225       The methods "keys", "pairs", and "values" return the most frequently
226       accessed items from the upper section of the cache first before the
227       lower section. Returned values may not be ordered as expected. This
228       abnormally is normal for this hybrid LRU-plain implementation. It comes
229       from fetches not involving LRU movement on keys residing in the upper
230       section of the cache.
231
232       When "max_age" is set, accessing an item which has expired will behave
233       similarly to a non-existing item.
234
235   MCE::Shared::Cache->new ( { options }, key, value [, key, value, ... ] )
236   MCE::Shared->cache ( { options }, key, value [, key, value, ... ] )
237       Constructs a new object.
238
239        # non-shared or local construction for use by a single process
240
241        use MCE::Shared::Cache;
242
243        $ca = MCE::Shared::Cache->new(); # max_keys => undef, max_age => undef
244        $ca = MCE::Shared::Cache->new( { max_keys => 500 }, @pairs );
245
246        $ca = MCE::Shared::Cache->new( max_keys => "unlimited", max_age => "never" );
247        $ca = MCE::Shared::Cache->new( max_keys => undef, max_age => undef ); # ditto
248        $ca = MCE::Shared::Cache->new( max_keys => 500, max_age => "1 hour" );
249        $ca = MCE::Shared::Cache->new( max_keys => "4 KiB" ); # 4*1024
250        $ca = MCE::Shared::Cache->new( max_keys => "1 MiB" ); # 1*1024*1024
251
252        $ca = MCE::Shared::Cache->new( max_age  => "43200 seconds" );
253        $ca = MCE::Shared::Cache->new( max_age  => 43200 );   # ditto
254        $ca = MCE::Shared::Cache->new( max_age  => "720 minutes" );
255        $ca = MCE::Shared::Cache->new( max_age  => "12 hours" );
256        $ca = MCE::Shared::Cache->new( max_age  => "0.5 days" );
257        $ca = MCE::Shared::Cache->new( max_age  => "1 week" );
258        $ca = MCE::Shared::Cache->new( max_age  => undef );   # no expiration
259        $ca = MCE::Shared::Cache->new( max_age  => 0 );       # now
260
261        $ca->assign( @pairs );
262
263        # construction for sharing with other threads and processes
264
265        use MCE::Shared;
266
267        $ca = MCE::Shared->cache(); # max_keys => undef, max_age => undef
268        $ca = MCE::Shared->cache( { max_keys => 500 }, @pairs );
269
270        $ca = MCE::Shared->cache( max_keys => "unlimited", max_age => "never" );
271        $ca = MCE::Shared->cache( max_keys => undef, max_age => undef ); # ditto
272        $ca = MCE::Shared->cache( max_keys => 500, max_age => "1 hour" );
273        $ca = MCE::Shared->cache( max_keys => "4 KiB" ); # 4*1024
274        $ca = MCE::Shared->cache( max_keys => "1 MiB" ); # 1*1024*1024
275
276        $ca = MCE::Shared->cache( max_age  => "43200 seconds" );
277        $ca = MCE::Shared->cache( max_age  => 43200 );   # ditto
278        $ca = MCE::Shared->cache( max_age  => "720 minutes" );
279        $ca = MCE::Shared->cache( max_age  => "12 hours" );
280        $ca = MCE::Shared->cache( max_age  => "0.5 days" );
281        $ca = MCE::Shared->cache( max_age  => "1 week" );
282        $ca = MCE::Shared->cache( max_age  => undef );   # no expiration
283        $ca = MCE::Shared->cache( max_age  => 0 );       # now
284
285        $ca->assign( @pairs );
286
287       Reorder: Yes, when given key-value pairs contain duplicate keys
288
289   assign ( key, value [, key, value, ... ] )
290       Clears the cache, then sets multiple key-value pairs and returns the
291       number of keys stored in the cache. This is equivalent to "clear",
292       "mset".
293
294        $len = $ca->assign( "key1" => "val1", "key2" => "val2" );
295
296       Reorder: Yes, when given key-value pairs contain duplicate keys
297
298   clear
299       Removes all key-value pairs from the cache.
300
301        $ca->clear;
302        %{$ca} = ();
303
304   delete ( key )
305       Deletes and returns the value by given key or "undef" if the key does
306       not exists in the cache.
307
308        $val = $ca->delete( "some_key" );
309        $val = delete $ca->{ "some_key" };
310
311   del
312       "del" is an alias for "delete".
313
314   exists ( key )
315       Determines if a key exists in the cache.
316
317        if ( $ca->exists( "some_key" ) ) { ... }
318        if ( exists $ca->{ "some_key" } ) { ... }
319
320       Reorder: No
321
322   get ( key )
323       Gets the value of a cache key or "undef" if the key does not exists.
324       LRU reordering occurs only if the key is found in the lower section of
325       the cache. See "peek" to not promote the key internally to the top of
326       the list.
327
328        $val = $ca->get( "some_key" );
329        $val = $ca->{ "some_key" };
330
331       Reorder: Yes
332
333   iterator ( key [, key, ... ] )
334       When "max_age" is set, prunes any expired keys at the head of the list.
335
336       Returns a code reference for iterating a list of key-value pairs stored
337       in the cache when no arguments are given. Otherwise, returns a code
338       reference for iterating the given keys in the same order. Keys that do
339       not exist will have the "undef" value.
340
341       The list of keys to return is set when the closure is constructed.
342       Later keys added to the hash are not included. Subsequently, the
343       "undef" value is returned for deleted keys.
344
345        $iter = $ca->iterator;
346        $iter = $ca->iterator( "key1", "key2" );
347
348        while ( my ( $key, $val ) = $iter->() ) {
349           ...
350        }
351
352       Reorder: No
353
354   iterator ( "query string" )
355       When "max_age" is set, prunes any expired keys at the head of the list.
356
357       Returns a code reference for iterating a list of key-value pairs that
358       match the given criteria. It returns an empty list if the search found
359       nothing.  The syntax for the "query string" is described above.
360
361        $iter = $ca->iterator( "val eq some_value" );
362        $iter = $ca->iterator( "key eq some_key :AND val =~ /sun|moon|air|wind/" );
363        $iter = $ca->iterator( "val eq sun :OR val eq moon :OR val eq foo" );
364        $iter = $ca->iterator( "key =~ /$pattern/" );
365
366        while ( my ( $key, $val ) = $iter->() ) {
367           ...
368        }
369
370       Reorder: No
371
372   keys ( key [, key, ... ] )
373       When "max_age" is set, prunes any expired keys at the head of the list.
374
375       Returns all keys in the cache by most frequently accessed when no
376       arguments are given. Otherwise, returns the given keys in the same
377       order. Keys that do not exist will have the "undef" value. In scalar
378       context, returns the size of the cache.
379
380        @keys = $ca->keys;
381        @keys = $ca->keys( "key1", "key2" );
382        $len  = $ca->keys;
383
384       Reorder: No
385
386   keys ( "query string" )
387       When "max_age" is set, prunes any expired keys at the head of the list.
388
389       Returns only keys that match the given criteria. It returns an empty
390       list if the search found nothing. The syntax for the "query string" is
391       described above. In scalar context, returns the size of the resulting
392       list.
393
394        @keys = $ca->keys( "val eq some_value" );
395        @keys = $ca->keys( "key eq some_key :AND val =~ /sun|moon|air|wind/" );
396        @keys = $ca->keys( "val eq sun :OR val eq moon :OR val eq foo" );
397        $len  = $ca->keys( "key =~ /$pattern/" );
398
399       Reorder: No
400
401   len ( key )
402       When "max_age" is set, prunes any expired keys at the head of the list.
403
404       Returns the size of the cache when no arguments are given. For the
405       given key, returns the length of the value stored at key or the "undef"
406       value if the key does not exists.
407
408        $size = $ca->len;
409        $len  = $ca->len( "key1" );
410        $len  = length $ca->{ "key1" };
411
412       Reorder: Yes, only when key is given
413
414   max_age ( [ secs ] )
415       Returns the maximum age set on the cache or "never" if not defined
416       internally.  It sets the default expiry time when seconds is given.
417
418        $age = $ca->max_age;
419
420        $ca->max_age( "43200 seconds" );
421        $ca->max_age( 43200 );     # ditto
422        $ca->max_age( "720 minutes" );
423        $ca->max_age( "12 hours" );
424        $ca->max_age( "0.5 days" );
425        $ca->max_age( "1 week" );
426        $ca->max_age( undef );     # no expiration
427        $ca->max_age( 0 );         # now
428
429   max_keys ( [ size ] )
430       Returns the size limit set on the cache or "unlimited" if not defined
431       internally. When size is given, it adjusts the cache accordingly to the
432       new size by pruning the head of the list if necessary.
433
434        $size = $ca->max_keys;
435
436        $ca->max_keys( undef );    # unlimited
437        $ca->max_keys( "4 KiB" );  # 4*1024
438        $ca->max_keys( "1 MiB" );  # 1*1024*1024
439        $ca->max_keys( 500 );
440
441   mdel ( key [, key, ... ] )
442       Deletes one or more keys in the cache and returns the number of keys
443       deleted.  A given key which does not exist in the cache is not counted.
444
445        $cnt = $ca->mdel( "key1", "key2" );
446
447   mexists ( key [, key, ... ] )
448       Returns a true value if all given keys exists in the cache. A false
449       value is returned otherwise.
450
451        if ( $ca->mexists( "key1", "key2" ) ) { ... }
452
453       Reorder: No
454
455   mget ( key [, key, ... ] )
456       Gets the values of all given keys. It returns "undef" for keys which do
457       not exists in the cache.
458
459        ( $val1, $val2 ) = $ca->mget( "key1", "key2" );
460
461       Reorder: Yes
462
463   mset ( key, value [, key, value, ... ] )
464       Sets multiple key-value pairs in a cache and returns the number of keys
465       stored in the cache.
466
467        $len = $ca->mset( "key1" => "val1", "key2" => "val2" );
468
469       Reorder: Yes
470
471   merge
472       "merge" is an alias for "mset".
473
474   pairs ( key [, key, ... ] )
475       When "max_age" is set, prunes any expired keys at the head of the list.
476
477       Returns key-value pairs in the cache by most frequently accessed when
478       no arguments are given. Otherwise, returns key-value pairs for the
479       given keys in the same order. Keys that do not exist will have the
480       "undef" value.  In scalar context, returns the size of the cache.
481
482        @pairs = $ca->pairs;
483        @pairs = $ca->pairs( "key1", "key2" );
484        $len   = $ca->pairs;
485
486       Reorder: No
487
488   pairs ( "query string" )
489       When "max_age" is set, prunes any expired keys at the head of the list.
490
491       Returns only key-value pairs that match the given criteria. It returns
492       an empty list if the search found nothing. The syntax for the "query
493       string" is described above. In scalar context, returns the size of the
494       resulting list.
495
496        @pairs = $ca->pairs( "val eq some_value" );
497        @pairs = $ca->pairs( "key eq some_key :AND val =~ /sun|moon|air|wind/" );
498        @pairs = $ca->pairs( "val eq sun :OR val eq moon :OR val eq foo" );
499        $len   = $ca->pairs( "key =~ /$pattern/" );
500
501       Reorder: No
502
503   peek ( key )
504       Same as "get" without changing the order of the keys. Gets the value of
505       a cache key or "undef" if the key does not exists.
506
507        $val = $ca->get( "some_key" );
508        $val = $ca->{ "some_key" };
509
510       Reorder: No
511
512   pipeline ( [ func1, @args ], [ func2, @args ], ... )
513       Combines multiple commands for the object to be processed serially. For
514       shared objects, the call is made atomically due to single IPC to the
515       shared-manager process. The "pipeline" method is fully
516       "wantarray"-aware and receives a list of commands and their arguments.
517       In scalar or list context, it returns data from the last command in the
518       pipeline.
519
520        @vals = $ca->pipeline(                     # ( "a_a", "b_b", "c_c" )
521           [ "set", foo => "a_a" ],
522           [ "set", bar => "b_b" ],
523           [ "set", baz => "c_c" ],
524           [ "mget", qw/ foo bar baz / ]
525        );
526
527        $len = $ca->pipeline(                      # 3, same as $ca->len
528           [ "set", foo => "i_i" ],
529           [ "set", bar => "j_j" ],
530           [ "set", baz => "k_k" ],
531           [ "len" ]
532        );
533
534        $ca->pipeline(
535           [ "set", foo => "m_m" ],
536           [ "set", bar => "n_n" ],
537           [ "set", baz => "o_o" ]
538        );
539
540       Reorder: Very likely, see API on given method
541
542   pipeline_ex ( [ func1, @args ], [ func2, @args ], ... )
543       Same as "pipeline", but returns data for every command in the pipeline.
544
545        @vals = $ca->pipeline_ex(                  # ( "a_a", "b_b", "c_c" )
546           [ "set", foo => "a_a" ],
547           [ "set", bar => "b_b" ],
548           [ "set", baz => "c_c" ]
549        );
550
551       Reorder: Very likely, see API on given command
552
553   purge ( )
554       Remove all tombstones and expired data from the cache.
555
556        $ca->purge;
557
558   remove
559       "remove" is an alias for "delete".
560
561   set ( key, value [, expires_in ] )
562       Sets the value of the given cache key and returns its new value.
563       Optionally in v1.839 and later releases, give the number of seconds
564       before the key is expired.
565
566        $val = $ca->set( "key", "value" );
567        $val = $ca->{ "key" } = "value";
568
569        $val = $ca->set( "key", "value", 3600  );  # or "60 minutes"
570        $val = $ca->set( "key", "value", undef );  # or "never"
571        $val = $ca->set( "key", "value", 0     );  # or "now"
572
573        $val = $ca->set( "key", "value", "2 seconds" );  # or "2s"
574        $val = $ca->set( "key", "value", "2 minutes" );  # or "2m"
575        $val = $ca->set( "key", "value", "2 hours"   );  # or "2h"
576        $val = $ca->set( "key", "value", "2 days"    );  # or "2d"
577        $val = $ca->set( "key", "value", "2 weeks"   );  # or "2w"
578
579       Reorder: Yes
580
581   values ( key [, key, ... ] )
582       When "max_age" is set, prunes any expired keys at the head of the list.
583
584       Returns all values in the cache by most frequently accessed when no
585       arguments are given. Otherwise, returns values for the given keys in
586       the same order.  Keys that do not exist will have the "undef" value. In
587       scalar context, returns the size of the cache.
588
589        @vals = $ca->values;
590        @vals = $ca->values( "key1", "key2" );
591        $len  = $ca->values;
592
593       Reorder: No
594
595   values ( "query string" )
596       When "max_age" is set, prunes any expired keys at the head of the list.
597
598       Returns only values that match the given criteria. It returns an empty
599       list if the search found nothing. The syntax for the "query string" is
600       described above. In scalar context, returns the size of the resulting
601       list.
602
603        @vals = $ca->values( "val eq some_value" );
604        @vals = $ca->values( "key eq some_key :AND val =~ /sun|moon|air|wind/" );
605        @vals = $ca->values( "val eq sun :OR val eq moon :OR val eq foo" );
606        $len  = $ca->values( "key =~ /$pattern/" );
607
608       Reorder: No
609
610   vals
611       "vals" is an alias for "values".
612

SUGAR METHODS

614       This module is equipped with sugar methods to not have to call "set"
615       and "get" explicitly. In shared context, the benefit is atomicity and
616       reduction in inter-process communication.
617
618       The API resembles a subset of the Redis primitives
619       <http://redis.io/commands#strings> with key representing the cache key.
620
621       Optionally in v1.839 and later releases, give the number of seconds
622       before the key is expired, similarly to "set".
623
624   append ( key, string [, expires_in ] )
625       Appends a value to a key and returns its new length.
626
627        $len = $ca->append( $key, "foo" );
628
629       Reorder: Yes
630
631   decr ( key [, expires_in ] )
632       Decrements the value of a key by one and returns its new value.
633
634        $num = $ca->decr( $key );
635
636       Reorder: Yes
637
638   decrby ( key, number [, expires_in ] )
639       Decrements the value of a key by the given number and returns its new
640       value.
641
642        $num = $ca->decrby( $key, 2 );
643
644       Reorder: Yes
645
646   getdecr ( key [, expires_in ] )
647       Decrements the value of a key by one and returns its old value.
648
649        $old = $ca->getdecr( $key );
650
651       Reorder: Yes
652
653   getincr ( key [, expires_in ] )
654       Increments the value of a key by one and returns its old value.
655
656        $old = $ca->getincr( $key );
657
658       Reorder: Yes
659
660   getset ( key, value [, expires_in ] )
661       Sets the value of a key and returns its old value.
662
663        $old = $ca->getset( $key, "baz" );
664
665       Reorder: Yes
666
667   incr ( key [, expires_in ] )
668       Increments the value of a key by one and returns its new value.
669
670        $num = $ca->incr( $key );
671
672       Reorder: Yes
673
674   incrby ( key, number [, expires_in ] )
675       Increments the value of a key by the given number and returns its new
676       value.
677
678        $num = $ca->incrby( $key, 2 );
679
680       Reorder: Yes
681

INTERNAL METHODS

683       Internal "Sereal" freeze-thaw hooks for exporting shared-cache object.
684
685       FREEZE
686       THAW
687
688       Internal "Storable" freeze-thaw hooks for exporting shared-cache
689       object.
690
691       STORABLE_freeze
692       STORABLE_thaw
693

PERFORMANCE TESTING

695       One might want to benchmark this module. If yes, remember to use the
696       non-shared construction for running on a single core.
697
698        use MCE::Shared::Cache;
699
700        my $cache = MCE::Shared::Cache->new( max_keys => 500_000 );
701
702       Otherwise, the following is a parallel version for a benchmark script
703       <https://blog.celogeek.com/201401/426/perl-benchmark-cache-with-
704       expires-and-max-size> found on the web. The serial version was created
705       by Celogeek for benchmarking various caching modules.
706
707       The MCE "progress" option makes it possible to track progress while
708       running parallel. This script involves IPC to and from the shared-
709       manager process, where the data resides. In regards to IPC, fetches may
710       take longer on Linux versus Darwin or FreeBSD.
711
712        #!/usr/bin/perl
713
714        use strict;
715        use warnings;
716        use feature qw( say );
717
718        use Digest::MD5 qw( md5_base64 );
719        use Time::HiRes qw( time );
720        use MCE 1.814;
721        use MCE::Shared;
722
723        $| = 1; srand(0);
724
725        # construct shared variables
726        # serialization is handled automatically
727
728        my $c     = MCE::Shared->cache( max_keys => 500_000 );
729        my $found = MCE::Shared->scalar( 0 );
730
731        # construct and spawn MCE workers
732        # workers increment a local variable $f
733
734        my $mce = MCE->new(
735            chunk_size  => 4000,
736            max_workers => 4,
737            user_func   => sub {
738                my ($mce, $chunk_ref, $chunk_id) = @_;
739                if ( $mce->user_args()->[0] eq 'setter' ) {
740                    for ( @{ $chunk_ref } ) { $c->set($_, {md5 => $_})  }
741                }
742                else {
743                    my $f = 0;
744                    for ( @{ $chunk_ref } ) { $f++ if ref $c->get($_) eq 'HASH' }
745                    $found->incrby($f);
746                }
747            }
748        )->spawn();
749
750        say "Mapping";
751        my @todo = map { md5_base64($_) } ( 1 .. 600_000 );
752
753        say "Starting";
754        my ( $read, $write );
755
756        {
757            my $s = time;
758            $mce->process({
759                progress  => sub { print "Write: $_[0]\r" },
760                user_args => [ 'setter' ],
761            }, \@todo);
762            $write = time - $s;
763        }
764
765        say "Write: ", sprintf("%0.3f", scalar(@todo) / $write);
766
767        {
768            my $s = time;
769            $found->set(0);
770            $mce->process({
771                progress  => sub { print "Read $_[0]\r" },
772                user_args => [ 'getter' ],
773            }, \@todo);
774            $read = time - $s;
775        }
776
777        $mce->shutdown();
778
779        say "Read : ", sprintf("%0.3f", scalar(@todo) / $read);
780        say "Found: ", $found->get();
781
782       The "progress" option is further described on Metacpan. Several
783       examples are provided, accommodating all input data-types in MCE.
784
785       Progress Demonstrations <https://metacpan.org/pod/MCE::Core#PROGRESS-
786       DEMONSTRATIONS>
787

SEE ALSO

789       ·  CHI
790
791       ·  Cache::FastMmap
792
793       ·  Cache::LRU
794
795       ·  Cache::Ref
796
797       ·  Tie::Cache::LRU
798
799       ·  Tie::Cache::LRU::Expires
800

INDEX

802       MCE, MCE::Hobo, MCE::Shared
803

AUTHOR

805       Mario E. Roy, <marioeroy AT gmail DOT com>
806
807
808
809perl v5.30.1                      2020-01-30             MCE::Shared::Cache(3)
Impressum