| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # $Id: /mirror/perl/GunghoX-FollowLinks/trunk/lib/GunghoX/FollowLinks/Rule/Fresh.pm 40501 2008-01-24T04:48:27.359921Z daisuke  $ | 
| 2 |  |  |  |  |  |  | # | 
| 3 |  |  |  |  |  |  | # Copyright (c) 2007 Daisuke Maki <daisuke@endeworks.jp> | 
| 4 |  |  |  |  |  |  | # All rights reserved. | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | package GunghoX::FollowLinks::Rule::Fresh; | 
| 7 | 1 |  |  | 1 |  | 1497 | use strict; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 30 |  | 
| 8 | 1 |  |  | 1 |  | 3 | use warnings; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 34 |  | 
| 9 | 1 |  |  | 1 |  | 359 | use Gungho::Util; | 
|  | 1 |  |  |  |  | 4586 |  | 
|  | 1 |  |  |  |  | 10 |  | 
| 10 | 1 |  |  | 1 |  | 30 | use GunghoX::FollowLinks::Rule qw(FOLLOW_ALLOW FOLLOW_DENY); | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 7 |  | 
| 11 | 1 |  |  | 1 |  | 293 | use base qw(GunghoX::FollowLinks::Rule); | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 227 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | __PACKAGE__->mk_accessors($_) for qw(storage); | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | sub new | 
| 16 |  |  |  |  |  |  | { | 
| 17 | 1 |  |  | 1 | 1 | 91 | my $class   = shift; | 
| 18 | 1 |  |  |  |  | 3 | my %args    = @_; | 
| 19 |  |  |  |  |  |  |  | 
| 20 | 1 |  |  |  |  | 3 | my $storage_config = delete $args{storage}; | 
| 21 |  |  |  |  |  |  | my $storage_module = Gungho::Util::load_module( | 
| 22 | 1 |  | 50 |  |  | 7 | $storage_config->{module} || 'Memory', | 
| 23 |  |  |  |  |  |  | 'GunghoX::FollowLinks::Rule::Fresh' | 
| 24 |  |  |  |  |  |  | ); | 
| 25 | 1 | 50 |  |  |  | 9 | my $storage = $storage_module->new( %{ $storage_config->{config} || {} } ); | 
|  | 1 |  |  |  |  | 12 |  | 
| 26 |  |  |  |  |  |  |  | 
| 27 | 1 |  |  |  |  | 9 | $class->next::method(storage => $storage); | 
| 28 |  |  |  |  |  |  | } | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | sub apply | 
| 31 |  |  |  |  |  |  | { | 
| 32 | 3 |  |  | 3 | 1 | 6912 | my ($self, $c, $response, $url, $attrs) = @_; | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 3 |  |  |  |  | 11 | my $storage = $self->storage; | 
| 35 | 3 | 100 |  |  |  | 19 | if ($storage->get($url->as_string)) { | 
| 36 | 2 |  |  |  |  | 7 | return FOLLOW_DENY; | 
| 37 |  |  |  |  |  |  | } else { | 
| 38 | 1 |  |  |  |  | 2 | $storage->put($url->as_string); | 
| 39 | 1 |  |  |  |  | 7 | return FOLLOW_ALLOW; | 
| 40 |  |  |  |  |  |  | } | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | 1; | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | __END__ | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | =head1 NAME | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | GunghoX::FollowLinks::Rule::Fresh - Only Follow Fresh Links | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | use GunghoX::FollowLinks::Rule::Fresh; | 
| 54 |  |  |  |  |  |  | my $rule = GunghoX::FollowLinks::Rule::Fresh->new( | 
| 55 |  |  |  |  |  |  | storage => { | 
| 56 |  |  |  |  |  |  | module => "Memory", | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  | ); | 
| 59 |  |  |  |  |  |  | $rule->apply( $c, $response, $url, $attrs ); | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | This rule allows you to follow links thatyou haven't seen yet. The list of | 
| 64 |  |  |  |  |  |  | URLs that have been fetched are stored in a storage module of your choise. | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | If you want to put it in a memcached instance, for example, you can specify | 
| 67 |  |  |  |  |  |  | it like this: | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | my $rule = GunghoX::FollowLinks::Rule::Fresh->new( | 
| 70 |  |  |  |  |  |  | storage => { | 
| 71 |  |  |  |  |  |  | module => "Cache", | 
| 72 |  |  |  |  |  |  | config => { | 
| 73 |  |  |  |  |  |  | cache => { | 
| 74 |  |  |  |  |  |  | module => "Cache::Memcached", | 
| 75 |  |  |  |  |  |  | config => { | 
| 76 |  |  |  |  |  |  | servers => "127.0.0.1:11211", | 
| 77 |  |  |  |  |  |  | compress_threshold => 10_000, | 
| 78 |  |  |  |  |  |  | } | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  | ); | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | =head1 METHODS | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | =head2 new | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | Creates a new rule instance. You must specify the storage backend. | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | =head2 apply | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | Applies the rule. | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | =cut |