File: Curried.pm

package info (click to toggle)
libcatalyst-plugin-cache-perl 0.12-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 264 kB
  • sloc: perl: 1,855; makefile: 2
file content (113 lines) | stat: -rw-r--r-- 2,135 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/perl

package Catalyst::Plugin::Cache::Curried;

use strict;
use warnings;

use base qw/Class::Accessor::Fast/;

use Scalar::Util ();

__PACKAGE__->mk_accessors(qw/c meta/);

sub new {
    my ( $class, $c, @meta ) = @_;

    my $self = $class->SUPER::new({
        c    => $c,
        meta => \@meta,
    });

    Scalar::Util::weaken( $self->{c} )
        if ref( $self->{c} );

    return $self;
}

sub backend {
    my ( $self, @meta ) = @_;
    $self->c->choose_cache_backend( @{ $self->meta }, @meta )
}

sub set {
    my ( $self, $key, $value, @meta ) = @_;
    @meta = ( expires => $meta[0] ) if @meta == 1;
    $self->c->cache_set( $key, $value, @{ $self->meta }, @meta );
}

sub get {
    my ( $self, $key ) = @_;
    $self->c->cache_get( $key, @{ $self->meta } );
}

sub remove {
    my ( $self, $key ) = @_;
    $self->c->cache_remove( $key, @{ $self->meta } );
}

sub compute {
    my ($self, $key, $code, @meta) = @_;
    @meta = ( expires => $meta[0] ) if @meta == 1;
    $self->c->cache_compute( $key, $code, @{ $self->meta }, @meta );
}

__PACKAGE__;

__END__

=pod

=head1 NAME

Catalyst::Plugin::Cache::Curried - Curried versions of C<cache_set>,
C<cache_get> and C<cache_remove> that look more like a backend.

=head1 SYNOPSIS

    my $curried = $c->cache( %meta );

    $curried->get( $key, $value ); # no need to specify %meta

=head1 DESCRIPTION

See L<Catalyst::Plugin::Cache/META DATA> for details.

=head1 METHODS

=over 4

=item new %meta

Create a new curried cache, that captures C<%meta>.

=item backend %additional_meta

This calls C<choose_cache_backend> on the $c object with the captured meta and
the additional meta.

=item set $key, $value, %additional_meta

=item get $key, %additional_meta

=item remove $key, %additional_meta

=item compute $key, $code, %additional_meta

Dellegate to the C<c> object's C<cache_set>, C<cache_get>, C<cache_remove>
or C<cache_compute> with the arguments, then the captured meta from C<meta>,
and then the additional meta.

=item meta

Returns the array ref that captured %meta from C<new>.

=item c

The captured $c object to delegate to.

=back

=cut