Skip to content

Commit

Permalink
add caching (untested)
Browse files Browse the repository at this point in the history
The master plan is to use the file cache for running test scripts
  • Loading branch information
moritz committed Jun 21, 2011
1 parent ee59758 commit 1942fef
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 8 deletions.
5 changes: 5 additions & 0 deletions Changes
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,10 @@
Revision history for WebService-Libris Revision history for WebService-Libris


0.03
Documentation for ::Author class
Accessor for books that an author wrote
Caching

0.02 2011-06-19 0.02 2011-06-19
Improved documentation Improved documentation
Accessors are a bit more robust Accessors are a bit more robust
Expand Down
26 changes: 23 additions & 3 deletions lib/WebService/Libris.pm
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ my %default_typemap = (
has 'id'; has 'id';
has 'type'; has 'type';
has '_dom'; has '_dom';
has 'cache';


has 'type_map'; has 'type_map';


Expand Down Expand Up @@ -251,11 +252,28 @@ sub rdf_url {
sub dom { sub dom {
my $self = shift; my $self = shift;


# TODO: add caching options unless ($self->_dom) {
$self->_dom(Mojo::UserAgent->new()->get($self->rdf_url)->res->dom) unless $self->_dom; if ($self->cache) {
my $key = join '/', $self->fragments;
if (my $r = $self->cache->get($key)) {
$self->_dom($r);
} else {
my $dom = $self->_request_dom;
$self->cache->set($key, $dom);
$self->_dom($dom);
}
} else {
$self->_dom($self->_request_dom);
}
}
$self->_dom; $self->_dom;
} }


sub _request_dom {
my $self = shift;
Mojo::UserAgent->new()->get($self->rdf_url)->res->dom;
}

sub direct_search { sub direct_search {
my ($self, %opts) = @_; my ($self, %opts) = @_;
my $terms = $opts{term} // die "Search term missing"; my $terms = $opts{term} // die "Search term missing";
Expand All @@ -281,6 +299,7 @@ sub search {
WebService::Libris::Collection->new( WebService::Libris::Collection->new(
type => 'bib', type => 'bib',
ids => \@ids, ids => \@ids,
cache => $self->cache,
); );
} }


Expand All @@ -291,7 +310,7 @@ sub search_for_isbn {
my $url = $res->res->headers->location; my $url = $res->res->headers->location;
return unless $url; return unless $url;
my ($type, $libris_id) = (split '/', $url)[-2, -1]; my ($type, $libris_id) = (split '/', $url)[-2, -1];
$self->new(type => $type, id => $libris_id); $self->new(type => $type, id => $libris_id, cache => $self->cache);
} }


sub fragments { sub fragments {
Expand Down Expand Up @@ -322,6 +341,7 @@ sub collection_from_dom {
WebService::Libris::Collection->new( WebService::Libris::Collection->new(
type => $key, type => $key,
ids => \@ids, ids => \@ids,
cache => $self->cache,
); );
} }


Expand Down
13 changes: 8 additions & 5 deletions lib/WebService/Libris/Collection.pm
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require WebService::Libris;


has 'type'; has 'type';
has 'ids'; has 'ids';
has 'cache';


sub new { sub new {
my ($class, %attrs) = @_; my ($class, %attrs) = @_;
Expand All @@ -13,25 +14,27 @@ sub new {


sub all { sub all {
my $self = shift; my $self = shift;
map WebService::Libris->new(type => $self->type, id => $_), map WebService::Libris->new(type => $self->type, id => $_, cache => $self->cache),
@{ $self->ids }; @{ $self->ids };
} }


sub first { sub first {
my $self = shift; my $self = shift;
return unless @{ $self->ids }; return unless @{ $self->ids };
WebService::Libris->new( WebService::Libris->new(
type => $self->type, type => $self->type,
id => $self->ids->[0], id => $self->ids->[0],
cache => $self->cache,
); );
} }


sub next { sub next {
my $self = shift; my $self = shift;
if (@{ $self->ids }) { if (@{ $self->ids }) {
return WebService::Libris->new( return WebService::Libris->new(
type => $self->type, type => $self->type,
id => shift @{ $self->ids }, id => shift @{ $self->ids },
cache => $self->cache,
) )
} else { } else {
return; return;
Expand Down
37 changes: 37 additions & 0 deletions lib/WebService/Libris/FileCache.pm
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,37 @@
use 5.010;
package WebService::Libris::FileCache;
use Mojo::Base -base;
use Mojo::DOM;

has 'directory';

sub new {
my $class = shift;
bless {@_}, $class;
}

sub _filename {
my ($self, $key) = @_;
$key =~ s{/}{_}g;
$self->directory . $key . '.xml'
}

sub get {
my ($self, $key) = @_;
my $filename = $self->_filename($key);
open my $h, '<', $filename;
return unless $h;
Mojo::DOM->new(xml => 1, charset => 'UTF-8')->parse(do { local $/; <$h> });
}

sub set {
my ($self, $key, $value) = @_;
my $filename = $self->_filename($key);
open my $h, '>', $filename or die "Can't open file '$filename' for writing: $!";
print { $h } $value->to_xml;
close $h;
$value;
}


1;

0 comments on commit 1942fef

Please sign in to comment.