| 1 | package Elastic::Model::Results::Cached; | ||
| 2 | $Elastic::Model::Results::Cached::VERSION = '0.52'; | ||
| 3 | use Carp; | ||
| 4 | use Moose; | ||
| 5 | with 'Elastic::Model::Role::Results'; | ||
| 6 | use MooseX::Types::Moose qw(Str Num Object HashRef); | ||
| 7 | |||
| 8 | use namespace::autoclean; | ||
| 9 | |||
| 10 | #=================================== | ||
| 11 | has 'took' => ( | ||
| 12 | #=================================== | ||
| 13 | isa => Num, | ||
| 14 | is => 'ro', | ||
| 15 | writer => '_set_took', | ||
| 16 | ); | ||
| 17 | |||
| 18 | #=================================== | ||
| 19 | has 'cache' => ( | ||
| 20 | #=================================== | ||
| 21 | is => 'ro', | ||
| 22 | isa => Object, | ||
| 23 | required => 1, | ||
| 24 | ); | ||
| 25 | |||
| 26 | #=================================== | ||
| 27 | has 'cache_opts' => ( | ||
| 28 | #=================================== | ||
| 29 | is => 'ro', | ||
| 30 | isa => HashRef, | ||
| 31 | ); | ||
| 32 | |||
| 33 | #=================================== | ||
| 34 | has 'cache_key' => ( | ||
| 35 | #=================================== | ||
| 36 | is => 'ro', | ||
| 37 | isa => Str, | ||
| 38 | builder => '_build_cache_key', | ||
| 39 | lazy => 1, | ||
| 40 | |||
| 41 | ); | ||
| 42 | |||
| 43 | no Moose; | ||
| 44 | |||
| 45 | #=================================== | ||
| 46 | sub _build_cache_key { | ||
| 47 | #=================================== | ||
| 48 | my $self = shift; | ||
| 49 | return $self->model->json->encode( $self->search ); | ||
| 50 | } | ||
| 51 | |||
| 52 | #=================================== | ||
| 53 | sub BUILD { | ||
| 54 | #=================================== | ||
| 55 | my $self = shift; | ||
| 56 | |||
| 57 | my $cache = $self->cache; | ||
| 58 | my $cache_opts = $self->cache_opts || {}; | ||
| 59 | my $cache_key = $self->cache_key; | ||
| 60 | |||
| 61 | my $cached; | ||
| 62 | my $result = $cache->get( $cache_key, %$cache_opts ) | ||
| 63 | unless delete $cache_opts->{force_set}; | ||
| 64 | |||
| 65 | if ( defined $result ) { | ||
| 66 | $cached++; | ||
| 67 | } | ||
| 68 | else { | ||
| 69 | $result = $self->model->search( $self->search ); | ||
| 70 | } | ||
| 71 | |||
| 72 | my $hits = $result->{hits}; | ||
| 73 | $self->_set_total( $hits->{total} ); | ||
| 74 | $self->_set_elements( $hits->{hits} ); | ||
| 75 | $self->_set_max_score( $hits->{max_score} || 0 ); | ||
| 76 | |||
| 77 | $self->_set_took( $result->{took} || 0 ); | ||
| 78 | $self->_set_facets( $result->{facets} || {} ); | ||
| 79 | $self->_set_aggs($result->{aggregations} || {}); | ||
| 80 | |||
| 81 | $cache->set( $cache_key, $result, $cache_opts ) | ||
| 82 | unless $cached; | ||
| 83 | } | ||
| 84 | |||
| 85 | 1; | ||
| 86 | |||
| 87 | =pod | ||
| 88 | |||
| 89 | =encoding UTF-8 | ||
| 90 | |||
| 91 | =head1 NAME | ||
| 92 | |||
| 93 | Elastic::Model::Results::Cached - A cacheable iterator over bounded/finite search results | ||
| 94 | |||
| 95 | =head1 VERSION | ||
| 96 | |||
| 97 | version 0.52 | ||
| 98 | |||
| 99 | =head1 SYNOPSIS | ||
| 100 | |||
| 101 | $cache = CHI->new(...); | ||
| 102 | $view = $model->view | ||
| 103 | ->cache( $cache ) | ||
| 104 | ->cache_opts( expires_in => '30 sec' ); | ||
| 105 | |||
| 106 | $results = $view->cached_search; | ||
| 107 | $results = $view->cached_search( expires_in => '2 sec', force_set => 1 ); | ||
| 108 | |||
| 109 | =head1 DESCRIPTION | ||
| 110 | |||
| 111 | An L<Elastic::Model::Results::Cached> object is returned when you call | ||
| 112 | L<Elastic::Model::View/cached_search()>, and behaves exactly the same | ||
| 113 | as L<Elastic::Model::Results> except that it will try to retrieve the | ||
| 114 | results from the cache, before hitting Elasticsearch. | ||
| 115 | |||
| 116 | =head1 ADDITIONAL ATTRIBUTES | ||
| 117 | |||
| 118 | =head2 cache | ||
| 119 | |||
| 120 | The L<CHI>-compatible cache object from L<Elastic::Model::View/cache>. | ||
| 121 | |||
| 122 | =head2 cache_opts | ||
| 123 | |||
| 124 | The combination of the default L<Elastic::Model::View/cache_opts> plus | ||
| 125 | any additional options passed in to L<Elastic::Model::View/cached_search()>. | ||
| 126 | These options are passed to | ||
| 127 | L<CHI's get() or set()|'https://metacpan.org/module/CHI#Getting-and-setting> | ||
| 128 | methods. | ||
| 129 | |||
| 130 | =head2 cache_key | ||
| 131 | |||
| 132 | The cache_key is a canonical JSON string representation of the full | ||
| 133 | L<Elastic::Model::Role::Results/search> parameter. | ||
| 134 | |||
| 135 | =head1 AUTHOR | ||
| 136 | |||
| 137 | Clinton Gormley <drtech@cpan.org> | ||
| 138 | |||
| 139 | =head1 COPYRIGHT AND LICENSE | ||
| 140 | |||
| 141 | This software is copyright (c) 2015 by Clinton Gormley. | ||
| 142 | |||
| 143 | This is free software; you can redistribute it and/or modify it under | ||
| 144 | the same terms as the Perl 5 programming language system itself. | ||
| 145 | |||
| 146 | =cut | ||
| 147 | |||
| 148 | __END__ |