Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP]Support Handling Template Loaders #100

Open
wants to merge 40 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
3d8a573
Preliminary version of Provider.
lestrrat Nov 19, 2013
bdfebb3
Add a simple custom provider test
lestrrat Nov 19, 2013
97f4021
Forget Provider, use Loader.
lestrrat Nov 20, 2013
9eaf25e
Revive slurp_template
lestrrat Nov 20, 2013
61ef6db
Use File::Spec->rel2abs instead of Cwd::abs_path
lestrrat Nov 20, 2013
2cf9385
use the correct cache prefix
lestrrat Nov 20, 2013
c07530d
Change default cache loc
lestrrat Nov 20, 2013
6b292bb
fix cache handling
lestrrat Nov 20, 2013
60c349c
Allow reading vpaths
lestrrat Nov 20, 2013
943ad34
revert this test back as we no longer has a provider (still fails)
lestrrat Nov 20, 2013
ecf013a
change hooks
lestrrat Nov 20, 2013
38a4e79
what I have so far (still broken)
lestrrat Nov 21, 2013
8392443
move meta in the beginning
lestrrat Nov 21, 2013
2cc887e
Clean up
lestrrat Nov 21, 2013
f6c2ddd
Separate out ::Assembler from ::Engine
lestrrat Nov 21, 2013
dc80243
I shall declare SAVE_SRC deprecated
lestrrat Nov 21, 2013
6b70a24
Remove my notes in POD which was causing POD tests to fail
lestrrat Nov 21, 2013
f27551f
Super silly, crude example of a loader using SQLite and memcached
lestrrat Nov 22, 2013
ab708c0
Add include example
lestrrat Nov 22, 2013
a2ea9f2
Create a base Loader class so that stuff like initialization can be h…
lestrrat Nov 22, 2013
ed424aa
Move error handling to MakeError
lestrrat Nov 22, 2013
19f98d7
Cleanup the goto stuff
lestrrat Nov 22, 2013
175bbe7
Change cache dir strategy
lestrrat Nov 22, 2013
69ed49d
Refactor constants to ::Constants
lestrrat Nov 22, 2013
fc1dbd0
Remove pure perl implementation.
tokuhirom Nov 22, 2013
cd75a6a
minil migrate
tokuhirom Nov 22, 2013
ccf31cc
builder/MyBuilder.pm uses OO-ish EU::ParseXS interface.
tokuhirom Nov 23, 2013
66f3572
Declare variable as unsued
lestrrat Nov 25, 2013
26047e2
Merge branch 'massive-refactor' into massive-refactor-minilized
lestrrat Nov 25, 2013
a11fab8
update META.json
lestrrat Nov 25, 2013
2c21fc5
Silence author tests
lestrrat Nov 25, 2013
22549a3
one more file to silence
lestrrat Nov 25, 2013
a95f034
This change allows MakeError consumers to change the log_prefix
lestrrat Nov 25, 2013
27531ca
Add back Catalyst::View::Xslate
lestrrat Nov 25, 2013
be2016e
Change bytecode_version to be an integer
lestrrat Nov 25, 2013
f4c5bd6
Just a little bit of documentation
lestrrat Nov 25, 2013
1df7e78
Update Changes
lestrrat Nov 25, 2013
c0a7336
oops. fixed usage about c-source.
tokuhirom Nov 25, 2013
a566811
Enable travis for 5.1[246]
tokuhirom Nov 25, 2013
cd0589a
tweaks
gfx Nov 27, 2013
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Super silly, crude example of a loader using SQLite and memcached
  • Loading branch information
lestrrat committed Nov 22, 2013
commit f27551f1087a775aa00a83d1a0f4a65049abdd12
128 changes: 128 additions & 0 deletions example/loader.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package
Example::Xslate::Loader::SQLiteMemcached;
use Mouse;
use Cache::Memcached;
use DBI;
use DBD::SQLite; # Just making it explicit, because this is an example
use File::Spec;
use File::Temp ();

has tempdir => (
is => 'ro',
lazy => 1,
builder => 'build_tempdir'
);

has db => (
is => 'ro',
lazy => 1,
builder => 'build_db'
);

has memd => (
is => 'ro',
lazy => 1,
builder => 'build_memd'
);

has engine => (
is => 'ro',
required => 1,
);

has assembler => (
is => 'ro',
required => 1,
);

sub build {
my ($class, $engine) = @_;

$class->new(
engine => $engine,
assembler => $engine->_assembler,
);
}

sub build_tempdir { File::Temp->newdir() }
sub build_db {
my $self = shift;
my $tempfile = File::Spec->catfile($self->tempdir, "templates.db");
my $dbh = DBI->connect("dbi:SQLite:dbname=$tempfile", undef, undef, {
RaiseError => 1,
AutoCommit => 1,
});

$dbh->do(<<EOSQL);
CREATE TABLE template (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
body TEXT NOT NULL,
updated_on INTEGER NOT NULL,
UNIQUE (name)
)
EOSQL

my %templates = (
hello => 'Hello, <: $lang :> world!',
hello_foo => 'Hello, <: $foo :> world!',
);
foreach my $name (keys %templates) {
$dbh->do(<<EOSQL, undef, $name, $templates{$name}, time());
INSERT INTO template (name, body, updated_on) VALUES (?, ?, ?)
EOSQL
}
return $dbh;
}
sub build_memd {
my $self = shift;
Cache::Memcached->new({
servers => [ '127.0.0.1:11211' ],
namespace => join(".", "templates", $self->engine->bytecode_version, ""),
});
}

sub compile { shift->engine->compile(@_) }
sub assemble { shift->assembler->assemble(@_) }

sub load {
my ($self, $name) = @_;

my ($body, $asm, $updated_on);
my $cached = $self->memd->get($name);
if ($cached) {
$asm = $cached->[0];
$updated_on = $cached->[1];
goto ASSEMBLE;
}

my $sth = $self->db->prepare_cached(<<EOSQL);
SELECT body, updated_on FROM template WHERE name = ?
EOSQL
if (! $sth->execute($name)) {
return;
}

($body, $updated_on) = $sth->fetchrow_array();
$asm = $self->compile($body);
$self->memd->set($name, [$asm, $updated_on]);

ASSEMBLE:
$self->assemble($asm, $name, $name, undef, int($updated_on));
return $asm;
}

package main;
use strict;
use Text::Xslate;

my $xslate = Text::Xslate->new();
my $loader = Example::Xslate::Loader::SQLiteMemcached->new(
engine => $xslate,
assembler => $xslate->_assembler,
);
$xslate->{loader} = $loader;

foreach (1..10) {
print $xslate->render('hello' => { lang => "Xslate" }), "\n";
}