| 1 | package LWP::UserAgent::SemWebCache; | ||
| 2 | |||
| 3 | 1 | ▶ | use 5.006000; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | our $AUTHORITY = 'cpan:KJETILK'; | ||
| 6 | our $VERSION = '0.002'; | ||
| 7 | |||
| 8 | 1 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 0 | ▶ | use Digest::MD4 qw(md4_base64); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | use Types::Standard qw(Str); | ||
| 11 | |||
| 12 | extends 'LWP::UserAgent'; | ||
| 13 | |||
| 14 | has key => ( | ||
| 15 | is => 'rw', | ||
| 16 | isa => Str, | ||
| 17 | lazy => 1, | ||
| 18 | clearer => 1, | ||
| 19 | builder => '_build_key' | ||
| 20 | ); | ||
| 21 | |||
| 22 | sub _build_key { return md4_base64(shift->request_uri->canonical->as_string) } | ||
| 23 | |||
| 24 | |||
| 25 | with 'LWP::UserAgent::Role::CHICaching', | ||
| 26 | 'LWP::UserAgent::Role::CHICaching::VaryNotAsterisk', | ||
| 27 | 'LWP::UserAgent::Role::CHICaching::SimpleMungeResponse'; | ||
| 28 | |||
| 29 | 1; | ||
| 30 | |||
| 31 | |||
| 32 | =pod | ||
| 33 | |||
| 34 | =encoding utf-8 | ||
| 35 | |||
| 36 | =head1 NAME | ||
| 37 | |||
| 38 | LWP::UserAgent::SemWebCache - LWP::UserAgent for caching SPARQL Queries | ||
| 39 | |||
| 40 | =head1 SYNOPSIS | ||
| 41 | |||
| 42 | This is a slight modification of L<LWP::UserAgent::CHICaching>, and | ||
| 43 | can be used much like it: | ||
| 44 | |||
| 45 | my $cache = CHI->new( driver => 'Memory', global => 1 ); | ||
| 46 | my $ua = LWP::UserAgent::SemWebCache->new(cache => $cache); | ||
| 47 | my $res1 = $ua->get("http://localhost:3000/?query=DAHUT"); | ||
| 48 | |||
| 49 | =head1 DESCRIPTION | ||
| 50 | |||
| 51 | This class composes the two roles L<LWP::UserAgent::Role::CHICaching> | ||
| 52 | and L<LWP::UserAgent::Role::CHICaching::VaryNotAsterisk> and | ||
| 53 | reimplements the C<key> attribute. | ||
| 54 | |||
| 55 | For now, it makes a relatively uncertain assumption that could in some | ||
| 56 | cases violate L<Section 4.1 of | ||
| 57 | RFC7234|http://tools.ietf.org/html/rfc7234#section-4.1> and cause | ||
| 58 | unpredictable results: Since SPARQL results come in different | ||
| 59 | serializations, the C<Vary> header will be present in most cases, and | ||
| 60 | therefore, different caches would usually have been required. However, | ||
| 61 | if we assume that no variations that are semantically significant | ||
| 62 | could occur, then we should be OK. Unless, of course, the server | ||
| 63 | declared that anything goes, which amount to setting C<Vary: *>, in | ||
| 64 | that case, we don't cache. | ||
| 65 | |||
| 66 | Additionally, since the URI resulting from a SPARQL protocol query | ||
| 67 | might be long, and long keys are often difficult for backend caches, | ||
| 68 | so the reimplementation of C<key> will create a digest. | ||
| 69 | |||
| 70 | =head2 Attributes and Methods | ||
| 71 | |||
| 72 | =over | ||
| 73 | |||
| 74 | =item C<< key >>, C<< clear_key >> | ||
| 75 | |||
| 76 | The key to use for a response. This role will return the canonical URI of the | ||
| 77 | request as a string, which is a reasonable default. | ||
| 78 | |||
| 79 | =back | ||
| 80 | |||
| 81 | =head1 AUTHOR | ||
| 82 | |||
| 83 | Kjetil Kjernsmo E<lt>kjetilk@cpan.orgE<gt>. | ||
| 84 | |||
| 85 | =head1 COPYRIGHT AND LICENCE | ||
| 86 | |||
| 87 | This software is copyright (c) 2015, 2016 by Kjetil Kjernsmo. | ||
| 88 | |||
| 89 | This is free software; you can redistribute it and/or modify it under | ||
| 90 | the same terms as the Perl 5 programming language system itself. | ||