| 1 | package Mason::Plugin::Cache; | ||
| 2 | 2 | ▶ | BEGIN { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 2 | $Mason::Plugin::Cache::VERSION = '0.05'; | |
| 4 | } | ||
| 5 | 2 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | with 'Mason::Plugin'; | |
| 7 | |||
| 8 | 2 | __PACKAGE__->meta->make_immutable(); | |
| 9 | |||
| 10 | 2 | 1; | |
| 11 | |||
| 12 | |||
| 13 | |||
| 14 | =pod | ||
| 15 | |||
| 16 | =head1 NAME | ||
| 17 | |||
| 18 | Mason::Plugin::Cache - Provide component cache object and filter | ||
| 19 | |||
| 20 | =head1 VERSION | ||
| 21 | |||
| 22 | version 0.05 | ||
| 23 | |||
| 24 | =head1 SYNOPSIS | ||
| 25 | |||
| 26 | my $result = $.cache->get('key'); | ||
| 27 | if (!defined($result)) { | ||
| 28 | ... compute $result ... | ||
| 29 | $.cache->set('key', $result, '5 minutes'); | ||
| 30 | } | ||
| 31 | |||
| 32 | ... | ||
| 33 | |||
| 34 | % $.Cache('key2', '1 hour') {{ | ||
| 35 | <!-- this will be cached for an hour --> | ||
| 36 | % }} | ||
| 37 | |||
| 38 | =head1 DESCRIPTION | ||
| 39 | |||
| 40 | Adds a C<cache> method and C<Cache> filter to access a cache (L<CHI|CHI>) | ||
| 41 | object with a namespace unique to the component. | ||
| 42 | |||
| 43 | =head1 INTERP PARAMETERS | ||
| 44 | |||
| 45 | =over | ||
| 46 | |||
| 47 | =item cache_defaults | ||
| 48 | |||
| 49 | Hash of parameters passed to cache constructor. Defaults to | ||
| 50 | |||
| 51 | driver=>'File', root_dir => 'DATA_DIR/cache' | ||
| 52 | |||
| 53 | which will create a basic file cache under Mason's L<data directory|data_dir>. | ||
| 54 | |||
| 55 | =item cache_root_class | ||
| 56 | |||
| 57 | Class used to create a cache. Defaults to L<CHI|CHI>. | ||
| 58 | |||
| 59 | =back | ||
| 60 | |||
| 61 | =head1 COMPONENT CLASS METHODS | ||
| 62 | |||
| 63 | =over | ||
| 64 | |||
| 65 | =item cache | ||
| 66 | |||
| 67 | Returns a new cache object with the namespace set to the component's path. | ||
| 68 | Parameters to this method, if any, are combined with L<cache_defaults> and | ||
| 69 | passed to the L<cache_root_class> constructor. The cache object is memoized | ||
| 70 | when no parameters are passed. | ||
| 71 | |||
| 72 | my $result = $.cache->get('key'); | ||
| 73 | |||
| 74 | =back | ||
| 75 | |||
| 76 | =head1 REQUEST METHODS | ||
| 77 | |||
| 78 | =over | ||
| 79 | |||
| 80 | =item cache | ||
| 81 | |||
| 82 | Same as calling C<cache> on the current component class. This usage will be | ||
| 83 | familiar to Mason 1 users. | ||
| 84 | |||
| 85 | my $result = $m->cache->get('key'); | ||
| 86 | |||
| 87 | =back | ||
| 88 | |||
| 89 | =head1 FILTERS | ||
| 90 | |||
| 91 | =over | ||
| 92 | |||
| 93 | =item Cache ($key, $options, [%cache_params]) | ||
| 94 | |||
| 95 | Caches the content using C<< $self->cache >> and the supplied cache I<$key>. | ||
| 96 | |||
| 97 | I<$options> is a scalar or hash reference. If a scalar, it is treated as the | ||
| 98 | C<expires_in> duration and passed as the third argument to C<set>. If it is a | ||
| 99 | hash reference, it may contain name/value pairs for both C<get> and C<set>. | ||
| 100 | |||
| 101 | I<%cache_params>, if any, are passed to C<< $self->cache >>. | ||
| 102 | |||
| 103 | % $.Cache($my_key, '1 hour') {{ | ||
| 104 | <!-- this will be cached for an hour --> | ||
| 105 | % }} | ||
| 106 | |||
| 107 | % $.Cache($my_key, { expire_if => sub { $.refresh } }, driver => 'RawMemory') {{ | ||
| 108 | <!-- this will be cached until $.refresh is true --> | ||
| 109 | % }} | ||
| 110 | |||
| 111 | If neither I<$key> nor I<$options> are passed, the key is set to 'Default' and | ||
| 112 | the cache never expires. | ||
| 113 | |||
| 114 | % $.Cache() {{ | ||
| 115 | <!-- cache this forever, or until explicitly removed --> | ||
| 116 | % }} | ||
| 117 | |||
| 118 | =back | ||
| 119 | |||
| 120 | =head1 SUPPORT | ||
| 121 | |||
| 122 | The mailing list for Mason and Mason plugins is | ||
| 123 | L<mason-users@lists.sourceforge.net>. You must be subscribed to send a message. | ||
| 124 | To subscribe, visit | ||
| 125 | L<https://lists.sourceforge.net/lists/listinfo/mason-users>. | ||
| 126 | |||
| 127 | You can also visit us at C<#mason> on L<irc://irc.perl.org/#mason>. | ||
| 128 | |||
| 129 | Bugs and feature requests will be tracked at RT: | ||
| 130 | |||
| 131 | http://rt.cpan.org/NoAuth/Bugs.html?Dist=Mason-Plugin-Cache | ||
| 132 | bug-mason-plugin-cache@rt.cpan.org | ||
| 133 | |||
| 134 | The latest source code can be browsed and fetched at: | ||
| 135 | |||
| 136 | http://github.com/jonswar/perl-mason-plugin-cache | ||
| 137 | git clone git://github.com/jonswar/perl-mason-plugin-cache.git | ||
| 138 | |||
| 139 | =head1 SEE ALSO | ||
| 140 | |||
| 141 | L<Mason|Mason> | ||
| 142 | |||
| 143 | =head1 AUTHOR | ||
| 144 | |||
| 145 | Jonathan Swartz <swartz@pobox.com> | ||
| 146 | |||
| 147 | =head1 COPYRIGHT AND LICENSE | ||
| 148 | |||
| 149 | This software is copyright (c) 2011 by Jonathan Swartz. | ||
| 150 | |||
| 151 | This is free software; you can redistribute it and/or modify it under | ||
| 152 | the same terms as the Perl 5 programming language system itself. | ||
| 153 | |||
| 154 | =cut | ||
| 155 | |||
| 156 | |||
| 157 | __END__ | ||