cache ( $expires )

Specifies that the resulting CFResponse object should be cached according to the settings from set_cache_config().

Access

public

Parameters

Parameter

Type

Required

Description

$expires

string
integer

Required

The time the cache is to expire. Accepts a number of seconds as an integer, or an amount of time, as a string, that is understood by strtotime() (e.g. “1 hour”).

Returns

Type

Description

$this

A reference to the current instance.

Examples

Fire a single request, and then cache the response to APC.

Note: This method is inherited by all service-specific classes.

// Instantiate
$sdb = new AmazonSDB();
$sdb->set_cache_config('apc');

// First time pulls live data
$response = $sdb->cache(10)->list_domains();
var_dump($response->isOK());

// Second time pulls from cache
$response = $sdb->cache(10)->list_domains();
var_dump($response->isOK());
Result:
bool(true)
bool(true)

Fire a single request, and then cache the response to the file system.

Note: This method is inherited by all service-specific classes.

// Instantiate
$sdb = new AmazonSDB();
$sdb->set_cache_config('./cache');

// First time pulls live data
$response = $sdb->cache(10)->list_domains();
var_dump($response->isOK());

// Second time pulls from cache
$response = $sdb->cache(10)->list_domains();
var_dump($response->isOK());
Result:
bool(true)
bool(true)

Fire a single request, and then cache the response to Memcache.

Note: This method is inherited by all service-specific classes.

// Instantiate
$sdb = new AmazonSDB();
$sdb->set_cache_config(array(
	array('host' => '127.0.0.1')
));

// First time pulls live data
$response = $sdb->cache(10)->list_domains();
var_dump($response->isOK());

// Second time pulls from cache
$response = $sdb->cache(10)->list_domains();
var_dump($response->isOK());
Result:
bool(true)
bool(true)

Batch several requests together, and then cache the responses to APC.

Note: This method is inherited by all service-specific classes.

// Instantiate
$sdb = new AmazonSDB();
$sdb->set_cache_config('apc');

// Prepare for parallel requests
$sdb->batch()->list_domains();
$sdb->batch()->list_domains();

// First time pulls live data
$response = $sdb->batch()->cache('1 minute')->send(false);
var_dump($response[0]->isOK());
var_dump($response[1]->isOK());

// Second time pulls from cache
$response = $sdb->batch()->cache('1 minute')->send(false);
var_dump($response[0]->isOK());
var_dump($response[1]->isOK());
Result:
bool(true)
bool(true)
bool(true)
bool(true)

Batch several requests together, and then cache the responses to the file system.

Note: This method is inherited by all service-specific classes.

// Instantiate
$sdb = new AmazonSDB();
$sdb->set_cache_config('./cache');

// Prepare for parallel requests
$sdb->batch()->list_domains();
$sdb->batch()->list_domains();

// First time pulls live data
$response = $sdb->batch()->cache('1 minute')->send(false);
var_dump($response[0]->isOK());
var_dump($response[1]->isOK());

// Second time pulls from cache
$response = $sdb->batch()->cache('1 minute')->send(false);
var_dump($response[0]->isOK());
var_dump($response[1]->isOK());
Result:
bool(true)
bool(true)
bool(true)
bool(true)

Source

Method defined in sdk.class.php | Toggle source view (22 lines) | View on GitHub

public function cache($expires)
{
    // Die if they haven't used set_cache_config().
    if (!$this->cache_class)
    {
        throw new CFRuntime_Exception('Must call set_cache_config() before using cache()');
    }

    if (is_string($expires))
    {
        $expires = strtotime($expires);
        $this->cache_expires = $expires - time();
    }
    elseif (is_int($expires))
    {
        $this->cache_expires = $expires;
    }

    $this->use_cache_flow = true;

    return $this;
}

Copyright © 2010–2013 Amazon Web Services, LLC


Feedback