Skip to content

Instantly share code, notes, and snippets.

@nihen
Created October 15, 2012 01:42
Show Gist options
  • Save nihen/3890412 to your computer and use it in GitHub Desktop.
Save nihen/3890412 to your computer and use it in GitHub Desktop.
xslate and fork and cache
#!perl
use strict;
use warnings;
use Fatal qw(open close);
use Test::More;
use File::Temp qw(tempdir);
use Text::Xslate;
{
package MyXslate;
use parent qw(Text::Xslate);
sub _load_source {
my ($self, $fi) = @_;
my $fullpath = $fi->{fullpath};
$self->{_load_source_count}{$fullpath}++;
$self->SUPER::_load_source($fi);
}
}
my $content_main = <<'T';
: cascade base
T
my $content_base = <<'T';
I am base
T
{
my $service = tempdir(CLEANUP => 1);
write_file("$service/main.tx", $content_main);
write_file("$service/base.tx", $content_base);
my $tx = MyXslate->new(
cache_dir => "$service/cache",
path => [$service],
);
my $tx2 = MyXslate->new(
cache_dir => "$service/cache",
path => [$service],
);
is $tx->render("main.tx"), $content_base;
is $tx2->render("main.tx"), $content_base;
is $tx->{_load_source_count}{"$service/main.tx"} => 1;
is $tx2->{_load_source_count}{"$service/main.tx"} => undef;
sleep 1; # time goes
$content_base .= '2';
write_file("$service/base.tx", $content_base);
is $tx->render("main.tx"), $content_base;
is $tx->{_load_source_count}{"$service/main.tx"} => 2;
# ここでできれば_load_sourceではなく_load_compiledにとどめてほしい
is $tx2->render("main.tx"), $content_base;
# とどまってたらundefのままだけど結果は1
is $tx2->{_load_source_count}{"$service/main.tx"} => undef;
}
done_testing;
exit;
sub write_file {
my($file, $content) = @_;
open my($fh), ">", $file;
print $fh $content;
close $fh;
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment