--- lib/Strix.pm 2008/06/20 17:04:20 212 +++ lib/Strix.pm 2008/06/20 20:44:18 213 @@ -11,6 +11,10 @@ use Carp qw/confess/; use Jifty; +use File::Slurp; +use JSON::XS; +use Carp qw/confess/; + our $debug = 0; =head1 NAME @@ -220,6 +224,12 @@ $uid ||= 1; # anonymous # $uid ||= 2; # admin + my $cache_format = 'site-%d-navigation-for-uid-%d.js'; + if ( my $data = $self->read_cache( $cache_format, $site_id, $uid ) ) { + return $data; + } + + my $sth = $self->dbh->prepare( "SELECT kategorija.*, ((length(prikaz)+length(coalesce(ordstr,'')))/3)-1 as depth FROM kategorija JOIN navigacija ON (kategorija.id = kategorija_id), site WHERE site_id = ? AND site.id = site_id AND userCanDoOnObject(?, 1, 'kats', kategorija.id) ORDER BY prikaz"); $sth->execute( $site_id, $uid ); @@ -304,8 +314,58 @@ } + $self->write_cache( $navigation, $cache_format, $site_id, $uid ); + return $navigation; } +=head2 cache_path + + my $path = $strix->cache_path( 'format-%d', $var, ... ); + +=cut + +sub cache_path { + my $self = shift; + + warn "# cache_path",dump( @_ ); + + my $path = Jifty::Util->absolute_path( 'var/strix' ); + + if ( ! -e $path ) { + mkdir $path || die "can't create $path: $!"; + } + + $path .= '/' . sprintf( shift, @_ ); # XXX shift is important here! + return $path; +} + +=head2 write_cache + + write_cache( $data, 'format-%d', $var, ... ); + +=cut + +sub write_cache { + my $self = shift; + my $data = shift || confess "no data?"; + my $path = $self->cache_path( @_ ); + write_file( $path, encode_json( $data )) || die "can't save into $path: $!"; +} + +=head2 read_cache + + my $data = read_cache( 'format-%d', $var ... ); + +=cut + +sub read_cache { + my $self = shift; + my $path = $self->cache_path( @_ ); + return unless -e $path; + warn "# read_cache( $path )"; + return decode_json( read_file( $path ) ) || die "can't read $path: $!"; +} + 1;