Changeset 57 in bookmarks


Ignore:
Timestamp:
08/16/13 00:09:23 (11 years ago)
Author:
peter
Message:
  • include a Last-Modified header in the HTML list and individual bookmark view
  • added a get_last_modified_time() method to Bookmarks that return the time of the most recently modified bookmark
  • for the individual bookmark view, check the Is-Modified-Since header and return a 304 response if the bookmark is unchanged
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/BookmarkApp.pm

    r53 r57  
    77 
    88use Encode; 
    9 use HTTP::Date qw{time2isoz time2iso}; 
     9use HTTP::Date qw{time2isoz time2iso time2str str2time}; 
    1010use JSON; 
    1111use Bookmarks; 
     
    135135 
    136136    # list all the bookmarks  
     137    my $mtime = $self->_bookmarks->get_last_modified_time; 
     138 
    137139    my $format = $q->param('format') || 'html'; 
    138140 
     
    239241            -type    => 'text/html', 
    240242            -charset => 'UTF-8', 
     243            -Last_Modified => time2str($mtime), 
    241244        ); 
    242245 
     
    335338    my $bookmark = $self->_bookmarks->get_bookmark({ id => $id }); 
    336339    if ($bookmark) { 
     340        # check If-Modified-Since header to return cache response 
     341        if ($self->query->env->{HTTP_IF_MODIFIED_SINCE}) { 
     342            my $cache_time = str2time($self->query->env->{HTTP_IF_MODIFIED_SINCE}); 
     343            if ($bookmark->mtime <= $cache_time) { 
     344                $self->header_type('redirect'); 
     345                $self->header_props( 
     346                    -status => 304, 
     347                ); 
     348                return; 
     349            } 
     350        } 
     351         
    337352        if ($format eq 'json') { 
    338353            $self->header_props( 
     
    349364                -type    => 'text/html', 
    350365                -charset => 'UTF-8', 
     366                -Last_Modified => time2str($bookmark->mtime), 
    351367            ); 
    352368            return $self->tt_process( 
  • trunk/Bookmarks.pm

    r52 r57  
    169169    $sth->execute(@bind); 
    170170    return @{ $sth->fetchall_arrayref({}) }; 
     171} 
     172 
     173sub get_last_modified_time { 
     174    my $self = shift; 
     175    my $sth = $self->dbh->prepare('select mtime from bookmarks order by mtime desc limit 1'); 
     176    $sth->execute; 
     177    my ($mtime) = $sth->fetchrow_array; 
     178    return $mtime; 
    171179} 
    172180 
Note: See TracChangeset for help on using the changeset viewer.