Cache layers plugin
# In your app
$app->plugin(XCached => [ ... caches config ... ] );
# In your controller
sub action {
my $c = shift;
my $user = $c->xcache('user');
if (!$user) {
$user = $user_model->load_user( id => $c->param('user_id') );
$c->xcache( user => $user, { expire_in => 3600 } );
}
$c->render( user => $user );
}
# ... or like this
sub action {
my $c = shift;
my $user = $c->xcache(
user => $user_model => load_user =>
[ id => $c->param('user_id') ],
(
cache => { expire_in => 3600 }
)
);
$c->render( user => $user );
}
# ... or even like this, if driver is non-blocking
sub action {
my $c = shift;
$c->render_later;
$c->xcache(
user => $user_model => load_user =>
[ id => $c->param('user_id') ],
(
cache => { expire_in => 3600 }
),
sub {
my ( $xcache, $user ) = @_;
$c->render( user => $user );
}
);
}
# In your template
<%= xcinclude(
'common/user/info',
user => $current_user,
xcached => [
cache => { expire_in => 60 }
],
xcache_key => 'user:' . $current_user->id
) %>
# In @common/user/info.html.ep
% for $object ( $user->related_objects ) {
%= include( 'common/user/related_object' => $object );
% }
Mojolicious::Plugin::XCached allows to add layers of caches to your app.
Caches are implemented via MojoX::Cached.
Plugin adds two helpers:
- "xcache"
-
To use caches inside application and/or templates
- "xcaches"
-
Returns count of cache layers
- "xcinclude"
-
To cache rendered templates includes
Simple cache which supports data caching, sub/method call results caching
use MojoX::Cached;
use MojoX::Cached::Driver::SomeDriver;
# Initialize
my $driver = MojoX::Cached::Driver::SomeDriver->new( ... driver options ... );
my $cacher = MojoX::Cached->new( driver => $driver );
# Simple using
#
# Set
$cacher->set( user => { name => 'Dmitry', email => '[email protected]', ... } );
# Get
my $user = $cacher->get('user');
# Cache method results for 1 hour (3600 seconds)
#
# Perform (in scalar context):
# $template->render( ... some template data ... )
# and cache result
my $rendered = $cacher->cached(
'heavy_template' => $temlate => 'render' =>
[ ... some template data ... ],
expire_in => 3600
);
# The same, but in list context
# Results will be stored separately (means, that method will be called again
# in list context)
($rendered) = $cacher->cached(
'heavy_template' => $temlate => 'render' =>
[ ... some template data ... ],
expire_in => 3600
);
# Later when on call method with same arguments, result will be fetched from
# cache (if not expired) respecting call context
Simple cache, which supports data caching, sub/method call results caching. You have treat this like Memoize with expiration and different backends.
It supports non-blocking interface, but non-blocking feature depends on driver implementation. Optional callback \&cb
could be passed as last argument for most of methods.
Dmitry "dim0xff" Latin <[email protected]>
This software is copyright (c) 2018 by Dmitry Latin.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.