CHI-Cascade

 view release on metacpan or  search on metacpan

lib/CHI/Cascade.pm  view on Meta::CPAN

        code    => sub {
            my ($rule, $target_name, $values_of_depends) = @_;

            # $values_of_depends == {
            #     unique_name_other1 => $value_1,
            #     unique_name_other2 => $value_2
            # }
            # $rule->target     eq      $target_name
            # $rule->depends    ===     ['unique_name_other1', 'unique_name_other2']
            # $rule->dep_values ==      $values_of_depends
            # $rule->params     ==      { a => 1, b => 2 }

            # Now we can calcualte $value
            return $value;
        },
        params  => { a => 1, b => 2 }
    );

    $cascade->rule(
        target  => 'unique_name_other1',
        depends => 'unique_name_other3',
        code    => sub {
            my ($rule, $target_name, $values_of_depends) = @_;

            # $values_of_depends == {
            #     unique_name_other3 => $value_3
            # }

            # computing here
            return $value;
        }
    );

    $value_of_this_target = $cascade->run('unique_name');

=head1 DESCRIPTION

This module is the attempt to use a benefits of caching and 'make' concept.
If we have many an expensive tasks (a I<computations> or sometimes here used
term as a I<recomputing>) and want to cache it we can split its to small
expsnsive tasks and to describe dependencies for cache items.

This module is experimental yet. I plan to improve it near time but some things
already work. You can take a look for t/* tests as examples.

=head1 CONSTRUCTOR

$cascade = CHI::Cascade->new( %options )

This method constructs a new C<CHI::Cascade> object and returns it.
Key/value pair arguments may be provided to set up the initial state.
Options are:

=over

=item chi

B<Required>. Instance of L<CHI> object. The L<CHI::Cascade> doesn't construct this
object for you. Please create instance of C<CHI> yourself.

=item busy_lock

B<Optional>. Default is I<never>. I<This is not C<busy_lock> option of CHI!>
This is amount of time (to see L<CHI/"DURATION EXPRESSIONS">) until all target
locks expire. When a target is to being computing it is locked. If process which
is to be computing target and it will die or OS will be hangs up we can dead
locks and locked target will never recomputed again. This option helps to avoid
it. You can set up a special busy_lock for rules too.

=item target_chi

B<Optional>. This is CHI cache for target markers. Default value is value of
L</chi> option. It can be useful if you use a L<CHI/l1_cache> option. So you can
separate data of targets from target markers - data will be kept in a file cache
and a marker in memory cache for example.

=back

=head1 METHODS

=over

=item rule( %options )

To add new rule to C<CHI::Cascade> object. All rules should be added before
first L</run> method

The keys of %options are (options are passed directly in L<CHI::Cascade::Rule> constructor):

=over

=item target

B<Required>. A target for L</run> and for searching of L</depends>. It can be as
scalar text or C<Regexp> object created through C<qr//>

=item depends

B<Optional>. The B<scalar>, B<arrayref> or B<coderef> values of dependencies.
This is the definition of target(s) from which this current rule is dependent.
If I<depends> is:

=over

=item scalar

It should be plain text of single dependence of this target.

=item arrayref

An each item of list can be scalar value (exactly matched target) or code
reference. If item is coderef it will be executed once as $coderef->( $rule,
L<< $rule->qr_params|CHI::Cascade::Rule/qr_params >> ) and should return a
scalar value as current dependence for this target at runtime (the API for
coderef parameters was changed since v0.16)

=item coderef

This subroutine will be executed once inside I<run> method if necessary with
parameters as: $coderef->( $rule, L<<$rule->qr_params|CHI::Cascade::Rule/qr_params >> )
(API was changed since v0.16). It should return B<scalar> or B<arrayref>. The
returned value is I<scalar> it will be considered as single dependence of this
target and the behavior will be exactly as described for I<scalar> in this
paragraph. If the returned value is I<arrayref> it will be considered as list of
dependencies for this target and the behavior will be exactly as described for
I<arrayref> in this paragraph.

=back

lib/CHI/Cascade.pm  view on Meta::CPAN


=back

Please notice that there no way to continue a L</code> of current rule if any
dependence throws an exception!. It because that the main concept of execution
code of rules is to have all valid values (cached or recomputed) of all
dependencies before execution of dependent code.

=item code

B<Required>. The code reference for computing a value of this target (a
I<computational code>). Will be executed if no value in cache for this target or
any dependence or dependences of dependences and so on will be recomputed. Will
be executed as C<< $code->( $rule, $target, $hashref_to_value_of_dependencies )
>> I<(The API of running this code was changed since v0.10)>

If you want to terminate a code and to return immediately from L</run> method and
don't want to save a value in cache you can throw an exception from L</code> of
type L<CHI::Cascade::Value>. Your instance of L<CHI::Cascade::Value> can have a
value or cannot (a valid value can be even C<undef>!). A L</run> method returns
either a value is set by you (through L<CHI::Cascade::Value/value> method) or
value from cache or C<undef> in other cases. Please to see
L<CHI::Cascade::Value>

If L</run> method will have a L</defer> option as B<true> this code will not be
executed and you will get a set bit B<CASCADE_DEFERRED> in L</state> bit mask
variable. This may useful when you want to control a target execution.

=over

=item $rule

An instance of L<CHI::Cascade::Rule> object. You can use it object as accessor
for some current executed target data (plain text of target, for getting of
parameters and so on). Please to see L<CHI::Cascade::Rule>

=item $target

The current executed target as plain text for this L</code>

=item $hashref_to_value_of_dependencies

A hash reference of values (values are cleaned values not L<CHI::Cascade::Value>
objects!) of all dependencies for current target. Keys in this hash are flat
strings of dependecies and values are computed or cached ones.

This module should guarantee that values of dependencies will be valid values
even if value is C<undef>. This code can return C<undef> value as a valid code
return but author doesn't recommend it. If C<CHI::Cascade> could not get a valid
values of all dependencies of current target before execution of this code the
last will not be executed (The C<run> will return C<undef>).

=back

=item params

B<Optional>. You can pass in your code any additional parameters by this option.
These parameters are accessed in your rule's code through
L<CHI::Cascade::Rule/params> method of L<CHI::Cascade::Rule> instance object.

=item busy_lock

B<Optional>. Default is L</busy_lock> of constructor or I<never> if first is not
defined. I<This is not C<busy_lock> option of CHI!> This is amount of time (to
see L<CHI/"DURATION EXPRESSIONS">) until target lock expires. When a target is
to being computed it is locked. If process which to be recomputing a target and
it will die or OS will be hangs up we can dead locks and locked target will
never recomputed again. This option helps to avoid it.

=item recomputed

B<Optional>. This is a computational callback (coderef). If target of this rule
was recomputed this callback will be executed right away after a recomputed
value has been saved in cache. The callback will be executed as $coderef->(
$rule, $target, $value ) where passed parameters are:

=over

=item $rule

An instance of L<CHI::Cascade::Rule> class. This instance is recreated for every
target searching and recomputing if need.

=item $target

A current target as string

=item $value

The instance of L<CHI::Cascade::Value> class. You can use a computed value as
$value->value

=back

For example you can use this callback for notifying of other sites that your
target's value has been changed and is already in cache.

=item value_expires

B<Optional>.
Sets an L<CHI>'s cache expire value for all future target markers are created by
this rule in notation described in L<CHI/"DURATION EXPRESSIONS">. The B<default>
is 'never'. It can be B<coderef> or B<string scalar> format as L<CHI/"DURATION
EXPRESSIONS">. A B<coderef> should return value in same format.

=item ttl

B<Optional>.
An arrayref for min & max intervals of TTL. Example: C<[ 60, 3600 ]> - where the
minimum ttl is seconds and the maximum is 3600 seconds. Targets of this rule
will be recomputed during from 60 up to 3600 seconds from touched time of any
dependence this rule. Please read L<CHI::Cascade::Value/CASCADE_TTL_INVOLVED>
too.

=back

=item run( $target, %options )

This method makes a cascade computation if need and returns value (value is
cleaned value not L<CHI::Cascade::Value> object!) for this target If any
dependence of this target of any dependencies of dependencies were
(re)computed this target will be (re)computed too.

The run method of instance of cascade can be called from other run method of



( run in 0.893 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )