-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5dc9ae8
commit 4fbd352
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package Slick::Annotation; | ||
|
||
use 5.036; | ||
|
||
use Exporter qw(import); | ||
use Carp qw(carp); | ||
use JSON::Tiny qw(decode_json encode_json); | ||
use Scalar::Util qw(blessed); | ||
|
||
our @EXPORT_OK = qw(cacheable); | ||
|
||
sub cacheable { | ||
my $cache = shift; | ||
my $code = shift; | ||
my $timeout = shift // 300; # Default cache is 5 minutes | ||
|
||
return sub { | ||
my ( $app, $context ) = @_; | ||
|
||
state $cache_obj = $app->cache($cache); | ||
|
||
if ($cache_obj) { | ||
if ( $cache_obj->get( $context->request->uri ) ) { | ||
my $response = | ||
decode_json( $cache_obj->get( $context->request->uri ) ); | ||
|
||
$context->from_psgi($response); | ||
} | ||
else { | ||
$code->( $app, $context ); | ||
|
||
my $json = encode_json $context->to_psgi; | ||
|
||
if ( blessed( $cache_obj->{_executor} ) =~ /Memcached/x ) { | ||
$cache_obj->set( | ||
$context->request->uri => $json => $timeout ); | ||
} | ||
else { | ||
$cache_obj->set( | ||
$context->request->uri => $json => EX => $timeout ); | ||
} | ||
} | ||
} | ||
else { | ||
carp | ||
qq{Attempted to use cache $cache to cache route but cache does not exist.}; | ||
$code->( $app, $context ); | ||
} | ||
} | ||
} | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters