Cache
[ class tree: Cache ] [ index: Cache ] [ all elements ]

Class: Cache_HTTP_Request

Source Location: /Cache-1.5.7/Cache/HTTP/Request.php

Class Overview

PEAR
   |
   --Cache
      |
      --Cache_HTTP_Request

HTTP_Request Cache


Author(s):

Version:

  • $Id: Request.php 315102 2011-08-17 19:38:20Z cweiske $

Methods


Inherited Variables

Inherited Methods

Class: Cache

Cache::Cache()
this Constructor set the Container Property as a Cache_Container|Cache_Error Object.
Cache::extSave()
Stores a dataset with additional userdefined data.
Cache::flush()
Flushes the cache - removes all data from it
Cache::garbageCollection()
Calls the garbage collector of the storage object with a certain probability
Cache::generateID()
Generates a "unique" ID for the given value
Cache::get()
Returns the requested dataset it if exists and is not expired
Cache::getCaching()
Returns the current caching state.
Cache::getUserdata()
Returns the userdata field of a cached data set.
Cache::isCached()
Checks if a dataset exists.
Cache::isExpired()
Checks if a dataset is expired
Cache::load()
Loads the given ID from the cache.
Cache::remove()
Removes the specified dataset from the cache.
Cache::save()
Stores the given data in the cache.
Cache::setCaching()
Enables or disables caching.
Cache::_Cache()

Class Details

[line 63]
HTTP_Request Cache

The classical example is :

You want to get news from another site through RSS remote files. But you don't want to access to to the remote site at every time you display its news on your site. Because, if the remote site is down or slow... So you you need a class which makes a local cache copy of the remote file. Every x hours, the cache is updated. But if the remote site is down, the local cache copy is keeped (you can also get error messages if you want).

So you need this class!

Cache_HTTP_Request inherits from Cache and use HTTP_Request to access to the remote file.

Usage example :

<?php require_once('Cache/HTTP_Request.php');

$cache = &new Cache_HTTP_Request('http://www.php.net', null, 'file', null, 3600); $cache->sendRequest(); $remoteFileBody = $cache->getResponseBody();

(...) ?>

  • Author: Fabien MARTY <fabien.marty@free.fr>
  • Version: $Id: Request.php 315102 2011-08-17 19:38:20Z cweiske $


[ Top ]


Method Detail

Cache_HTTP_Request (Constructor)   [line 131]

Cache_HTTP_Request Cache_HTTP_Request( $url $url, [$params $params = null], [string $container = 'file'], [array $containerOptions = null], [int $expires = 3600], [int $mode = CACHE_HTTP_REQUEST_KEEP_LOCAL_COPY])

Constructor

Parameters:

string   $container     Name of container class
array   $containerOptions     Array with container class options
int   $mode     What to do when the remote server is down : CACHE_HTTP_REQUEST_KEEP_LOCAL_COPY or CACHE_HTTP_REQUEST_RETURN_FALSE or CACHE_HTTP_REQUEST_RETURN_PEAR_ERROR
int   $expires     lifetime of the cached data in seconds - 0 for endless
$url   $url     The url to access
$params   $params     Associative array of parameters which can be: method - Method to use, GET, POST etc http - HTTP Version to use, 1.0 or 1.1 user - Basic Auth username pass - Basic Auth password proxy_host - Proxy server host proxy_port - Proxy server port proxy_user - Proxy auth username proxy_pass - Proxy auth password

[ Top ]

_Cache_HTTP_Request (Destructor)   [line 158]

void _Cache_HTTP_Request( )

Deconstructor
  • Access: public

[ Top ]

getResponseBody   [line 170]

mixed getResponseBody( )

Get and return the response body (null if no data available)

[ Top ]

getResponseCode   [line 182]

mixed getResponseCode( )

Get and return the response code (null if no data available)

[ Top ]

getResponseHeader   [line 194]

mixed getResponseHeader( )

Get and return the response header (null if no data available)

[ Top ]

sendRequest   [line 235]

mixed sendRequest( )

Send the HTTP request or use the cache system

If there is a cache file for this HTTP request, the request is not re-sent. Cached response is used. Yet, if the cache is expired, the HTTP request is re-sent. Then, if the remote server is down, this method will return : (depending on the selected mode)

  • false or
  • a PEAR_Error or (better)
  • true and the local copy of the latest valid response will be used.
(technical) For the last choice, there is a technical tips. Indeed, there are two cache files. The first one (id key) is a classical one with the given lifetime. But it can be removed by automatic garbage collection for example. So to be able to use the latest valid response (when the remote server is dead), we make a second cache file with no lifetime (id2 key).

  • Return: true or false or a PEAR_ERROR
  • Access: public

[ Top ]

setMode   [line 209]

void setMode( int $newMode)

Set a new mode when the server is down
  • Access: public

Parameters:

int   $newMode     What to do when the remote server is down : CACHE_HTTP_REQUEST_KEEP_LOCAL_COPY or CACHE_HTTP_REQUEST_RETURN_FALSE or CACHE_HTTP_REQUEST_RETURN_PEAR_ERROR

[ Top ]


Documentation generated on Thu, 15 Sep 2011 07:00:07 +0000 by phpDocumentor 1.4.3. PEAR Logo Copyright © PHP Group 2004.