Show
Ignore:
Timestamp:
04/01/08 21:02:22 (9 months ago)
Author:
yusukebe
Message:

add CACHING

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/WebService-Simple/trunk/lib/WebService/Simple.pm

    r8554 r8608  
    88use URI::Escape; 
    99use WebService::Simple::Response; 
     10use Data::Dumper; 
    1011 
    11 our $VERSION = '0.01'; 
     12our $VERSION = '0.02'; 
    1213 
    1314sub new { 
    14     my ($class, $base_url, $param) = @_; 
    15     croak "paramater base_url is required" unless $base_url; 
     15    my ($class, %opt) = @_; 
     16    croak "paramater base_url is required" unless $opt{base_url}; 
    1617    my $self = bless { 
    17                       base_url => $base_url, 
    18                       param => $param, 
     18                      ua => LWP::UserAgent->new, 
     19                      %opt, 
    1920                  }, $class; 
    2021    $self; 
     
    2425    my ($self, $request_param) = @_; 
    2526    my $url = $self->_make_url($request_param); 
    26     my $ua = LWP::UserAgent->new; 
    27     my $response = $ua->get($url); 
     27    my $response = $self->_fetch_url($url); 
     28    return $response; 
     29} 
     30 
     31sub _fetch_url{ 
     32    my ($self,$url) = @_; 
     33    my $response; 
     34    if(exists $self->{cache}){ 
     35        $response = $self->{cache}->thaw($url); 
     36        if(defined $response){ 
     37            return $response; 
     38        } 
     39    } 
     40    $response = $self->{ua}->get($url); 
    2841    croak "can't get the request" unless $response->is_success; 
     42    if(exists $self->{cache}) { 
     43        $self->{cache}->freeze($url, $response); 
     44    } 
    2945    return $response; 
    3046} 
     
    6076 
    6177    my $flickr = WebService::Simple->new( 
    62         "http://api.flickr.com/services/rest/", 
    63         { api_key => "your_api_key", } 
     78        base_url => "http://api.flickr.com/services/rest/", 
     79        param    => { api_key => "your_api_key", } 
    6480    ); 
    6581    my $response = 
     
    7995 
    8096    my $flickr = WebService::Simple->new( 
    81         "http://api.flickr.com/services/rest/", 
    82         { api_key => "your_api_key", } 
     97        base_url => "http://api.flickr.com/services/rest/", 
     98        param    => { api_key => "your_api_key", } 
    8399    ); 
    84100 
     
    95111=back 
    96112 
     113=head1 CACHING 
     114 
     115Cache the response of Web Service APIs by using Cache object. 
     116Here's an example. 
     117 
     118    my $cache = Cache::File->new( 
     119        cache_root      => '/tmp/mycache', 
     120        default_expires => '30 min', 
     121    ); 
     122 
     123    my $flickr = WebService::Simple->new( 
     124        base_url => "http://api.flickr.com/services/rest/", 
     125        cache    => $cache, 
     126        param    => { api_key => $api_key, } 
     127    ); 
     128 
     129 
    97130=head1 AUTHOR 
    98131