The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Egg::Plugin::Cache::UA - The result of the WEB request is cached.

SYNOPSIS

  package MyApp;
  use Egg qw/Cache::UA/;
  .......
  .....

  package MyApp::Dispatch;
  .........
  
  MyApp->dispatch_map(
    ...........
    cache=> {
      google => sub {
        my($e)= @_;
        $e->cache_ua->output('http://xxx.googlesyndication.com/pagead/show_ads.js');
        },
      brainer=> sub {
        my($e)= @_;
        $e->cache_ua->output('http://xxx.brainer.jp/ad.js');
        },
      },
    );

DESCRIPTION

This module caches and recycles the request result of Egg::Plugin::LWP.

Especially, I think that it is effective in the contents match system advertisement etc. of the type that returns the JAVA script. It becomes difficult to receive the influence of the response speed of advertisement ASP server by the action of cashe.

It is necessary to setup Egg::Model::Cache to use it, and to set the label name to acquire the model to 'cache_name' of the configuration.

This module has succeeded to Egg::Plugin::LWP.

CONFIGURATION

The configuration is set by 'plugin_cache_ua'.

  package MyApp;
  
  __PACKAGE__->startup(
    plugin_cache_ua => {
      cache_name         => 'cache_model_name',
      allow_hosts        => [qw/ mydomain.name /],
      content_type       => 'text/html',
      content_type_error => 'text/html',
      cache_expires      => 60* 60,
      expires            => '+1d',
      last_modified      => '+1d',
      },
    );

allow_hosts

The host name that permits the use of cashe is set with ARRAY.

It is necessary to set this.

  allow_hosts => [qw/ www.domain.com domain.com domain.net /],

When the regular expression is set, the access is not accepted because each value is put on quotemeta.

When it is not possible to acquire it, processing is continued disregarding this setting though it checks with HTTP_REFERER of the environment variable because the thing that cannot be acquired under the influences of the proxy and the security software, etc. can break out, too.

content_type

Default of sent contents type.

This setting is substitution when the contents type is not obtained because of the WEB request.

'text/html' is used if it unsets it.

  content_type=> 'text/javascript',

content_type_error

Contents type used when data is not obtained by some errors' occurring by WEB request.

Default is 'text/html'.

cache_name

Model name of cashe used.

The model name to acquire the cashe object set up with Egg::Model::Cache is set.

There is no default. Please set it.

cache_expires

It is a value passed to the third argument of 'set' method of cashe.

This is a setting that assumes the use of Cache::Memcached.

  cache_expires=> 60* 60,  # It is effective for one hour.

It is not necessary to set it usually.

Validity term is done depending on the cashe model used.

expires or last_modified

The response header to press the cashe of the browser side is set.

It specifies it by the form used by CGI module.

  expires       => '+1d',
  last_modified => '+1d',

request_method

It is request a method when WEB is requested.

Default is 'GET'.

I think that you should specify this when you are putting necessary in 'get' method as undefined usually.

  $e->cache_ua->get( 'http://.....' => { request_method=> 'POST' } );

NAME

cache_ua

The Egg::Plugin::Cache::UA::handler object is returned.

 my $cache_ua= $e->cache_ua;

HADLER METHODS

get ( [URL], [OPTION] )

The request is sent to URL.

The content is returned if becoming a hit to cashe.

OPTION overwrites the default.

The HASH reference returns to the return value without fail.

  my $res= $e->cache_ua->get('http://domainname/');
  
  if ($res->{is_success}) {
    $e->stash->{request_content}= \$res->{content};
  } else {
    $e->finished($res->{status} || 500);
  }

The content of content is set in $e->response->body. When content is not obtained by the error's occurring by the request, the content of error is set.

Because $e->response->body is defined, the processing of view comes to be passed by the operation of Egg.

The content of the returned HASH reference is as follows.

is_success

Succeeding in the request is true.

status

There is a status line obtained because of the response.

content_type

There is a contents type obtained because of the response.

Instead, the default of the setting enters when the contents type is not obtained.

content

There is a content of contents obtained because of the response.

error

One the respondent error message enters when is_success is false.

no_hit

When not becoming a hit to cashe, it becomes true.

output ( [URL], [OPTION] )

Content is set directly to Egg::Response based on information obtained by the get method.

The response header set here is as follows.

X-CACHE-UA

When no_hit is only false, it is set. In a word, the thing that becomes a hit to cashe is shown.

expires or last_modified

It is set based on the setting.

status

The obtained status line is set.

content_type

The obtained contents type is set.

delete ( [URL] )

The data of URL is deleted from cashe.

  $e->delete('http://domainname/');
  • Alias = remove.

cache ([LABEL_NAME])

The cashe object set to 'cache_name' is returned usually.

When LABEL_NAME is specified, an arbitrary model object is returned.

 my $cache= $e->cache_ua->cache('cache_label');

SEE ALSO

Egg::Release, Egg::Response, Egg::Plugin::LWP, Egg::Model::Cache,

AUTHOR

Masatoshi Mizuno <lushe@cpan.org>

COPYRIGHT AND LICENSE

Copyright (C) 2008 by Bee Flag, Corp. <http://egg.bomcity.com/>, All Rights Reserved.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available.