track mtime of data files
authorDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 26 May 2010 12:17:18 +0000 (14:17 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 26 May 2010 12:17:18 +0000 (14:17 +0200)
This allows reload of data files if they changed on disk

lib/MojoFacets/Data.pm

index a5ae43f..8587d9f 100644 (file)
@@ -47,11 +47,15 @@ sub index {
 sub _load_path {
        my ( $self, $path ) = @_;
 
 sub _load_path {
        my ( $self, $path ) = @_;
 
-       return if defined $loaded->{$path}->{data};
-
        my $full_path = $self->app->home->rel_file( 'data/' . $path );
        die "$full_path $!" unless -r $full_path;
 
        my $full_path = $self->app->home->rel_file( 'data/' . $path );
        die "$full_path $!" unless -r $full_path;
 
+       if ( defined $loaded->{$path}->{data} ) {
+               my $mtime = (stat($full_path))[9];
+               return if $loaded->{$path}->{mtime} == $mtime;
+               warn "reload $full_path, modified ", time() - $mtime, " seconds ago\n";
+       }
+
        # we could use Mojo::JSON here, but it's too slow
 #      $data = from_json read_file $path;
        my $data = read_file $full_path;
        # we could use Mojo::JSON here, but it's too slow
 #      $data = from_json read_file $path;
        my $data = read_file $full_path;
@@ -149,6 +153,7 @@ sub _load_path {
                stats  => $stats,
                full_path => $full_path,
                size => -s $full_path,
                stats  => $stats,
                full_path => $full_path,
                size => -s $full_path,
+               mtime => (stat($full_path))[9],
                data => $data,
        };
 
                data => $data,
        };