Changeset 2528


Ignore:
Timestamp:
02/22/10 22:43:48 (2 years ago)
Author:
karpet
Message:

more facet support, with CHI

Location:
Search-OpenSearch/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • Search-OpenSearch/trunk/Makefile.PL

    r2477 r2528  
    2020        'Data::Pageset'      => 0, 
    2121        'Search::Tools'      => 0, 
     22        'CHI'                => 0, 
    2223    }, 
    2324    dist                => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', }, 
  • Search-OpenSearch/trunk/lib/Search/OpenSearch/Engine.pm

    r2527 r2528  
    88use Search::OpenSearch::Response::XML; 
    99use Search::OpenSearch::Response::JSON; 
    10  
    11 __PACKAGE__->mk_accessors(qw( index facets fields link )); 
     10use CHI; 
     11 
     12__PACKAGE__->mk_accessors(qw( index facets fields link cache cache_ttl )); 
    1213 
    1314our $VERSION = '0.05'; 
     
    2324            Search::OpenSearch::Facets->new( %{ $self->facets } ) ); 
    2425    } 
     26    $self->{cache} ||= CHI->new( 
     27        driver           => 'File', 
     28        dir_create_mode  => 0770, 
     29        file_create_mode => 0660, 
     30        root_dir         => "/tmp/opensearch_cache", 
     31    ); 
     32    $self->{cache_ttl} = 60 * 60 * 1;    # 1 hour 
     33 
    2534    return $self; 
    2635} 
     
    7382        : $response_class->new( 
    7483        results   => $results, 
    75         facets    => $self->get_facets($results), 
     84        facets    => $self->get_facets( $query, $results ), 
    7685        fields    => $self->fields, 
    7786        offset    => $offset, 
     
    92101 
    93102sub get_facets { 
    94     croak "Engine " . ref( $_[0] ) . " must implement get_facets"; 
     103    my $self      = shift; 
     104    my $query     = shift; 
     105    my $results   = shift; 
     106    my $cache_key = ref($self) . $query; 
     107    my $cache     = $self->cache or return; 
     108 
     109    my %facets; 
     110    if ( $cache->get($cache_key) ) { 
     111        %facets = %{ $cache->get($cache_key) }; 
     112    } 
     113    else { 
     114        my $facets = $self->build_facets( $query, $results ); 
     115        $cache->set( $cache_key, $facest, $self->cache_ttl ); 
     116        %facets = %$facets; 
     117    } 
     118    return \%facets; 
     119} 
     120 
     121sub build_facets { 
     122    croak ref(shift) . " must implement build_facets()"; 
    95123} 
    96124 
     
    186214The base URI for Responses. Passed to Response->link. 
    187215 
     216=head2 get_facets( I<query>, I<results> ) 
     217 
     218Checks the cache for facets related to I<query> and, if found, 
     219returns them. If not found, calls build_facets(), which must 
     220be implemented by each Engine subclass. 
     221 
     222=head2 cache 
     223 
     224Get/set the internal CHI object. Defaults to the File driver. 
     225 
     226=head2 cache_ttl 
     227 
     228Get/set the cache key time-to-live. Default is 1 hour. 
     229 
     230=cut 
     231 
    188232=head1 AUTHOR 
    189233 
  • Search-OpenSearch/trunk/lib/Search/OpenSearch/Response/XML.pm

    r2527 r2528  
    88use URI::Encode qw( uri_encode ); 
    99use POSIX qw( strftime ); 
     10use Data::UUID; 
    1011 
    1112our $VERSION = '0.05'; 
     
    2021 
    2122sub stringify { 
    22     my $self  = shift; 
    23     my $pager = $self->fetch_pager(); 
    24  
    25     my @entries = $self->_build_entries; 
     23    my $self       = shift; 
     24    my $pager      = $self->fetch_pager(); 
     25    my $UUID_maker = Data::UUID->new; 
     26    my @entries    = $self->_build_entries; 
    2627 
    2728    my $now = strftime '%Y-%m-%dT%H:%M:%SZ', gmtime; 
     
    3435            'opensearch:startIndex'   => $self->offset, 
    3536            'opensearch:itemsPerPage' => $self->page_size, 
     37            'id'                      => $UUID_maker->create(), 
    3638        }, 
    3739        'feed', 
     
    4951    ); 
    5052 
    51     # TODO generate uuid? cache per query? 
    52  
    5353    # main link 
    5454    my $link = $XMLer->singleton( 'link', { href => $self->link } ); 
     
    128128    my $results = $self->fetch_results(); 
    129129    my @entries; 
     130 
     131    #my $UUID_maker = Data::UUID->new; 
     132 
    130133    for my $result (@$results) { 
    131134        my $entry = $XMLer->perl_to_xml( 
    132135            {   title   => $result->{title}, 
    133136                content => $result->{summary}, 
    134                 id      => $result->{uri}, 
     137                id      => $result->{uri},       # or uuid? 
    135138            }, 
    136             'entry', 0, 1 
     139            'entry', 
     140            0, 
     141            1 
    137142        ); 
    138143        my $link = $XMLer->singleton( 'link', { href => $result->{uri} } ); 
Note: See TracChangeset for help on using the changeset viewer.