Set the caching configuration to use for response caching.
Access
Parameters
Parameter |
Type |
Required |
Description |
$location
|
string
|
Required
|
The location to store the cache object in. This may vary by cache method. - File - The local file system paths such as
./cache (relative) or /tmp/cache/ (absolute). The location must be server-writable. - APC - Pass in
apc to use this lightweight cache. You must have the APC extension installed. - XCache - Pass in
xcache to use this lightweight cache. You must have the XCache extension installed. - Memcached - Pass in an indexed array of associative arrays. Each associative array should have a
host and a port value representing a Memcached server to connect to. - PDO - A URL-style string (e.g.
pdo.mysql://user:pass@localhost/cache ) or a standard DSN-style string (e.g. pdo.sqlite:/sqlite/cache.db ). MUST be prefixed with pdo. . See CachePDO and PDO for more details. |
$gzip
|
boolean
|
Optional
|
Whether or not data should be gzipped before being stored. A value of true will compress the contents before caching them. A value of false will leave the contents uncompressed. Defaults to true . |
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)
Source
Method defined in sdk.class.php | Toggle source view (44 lines) | View on GitHub
public function set_cache_config($location, $gzip = true)
{
// If location is empty, don't do anything.
if (empty($location))
{
return $this;
}
// If we have an array, we're probably passing in Memcached servers and ports.
if (is_array($location))
{
$this->cache_class = 'CacheMC';
}
else
{
// I would expect locations like `/tmp/cache`, `pdo.mysql://user:pass@hostname:port`, `pdo.sqlite:memory:`, and `apc`.
$type = strtolower(substr($location, 0, 3));
switch ($type)
{
case 'apc':
$this->cache_class = 'CacheAPC';
break;
case 'xca': // First three letters of `xcache`
$this->cache_class = 'CacheXCache';
break;
case 'pdo':
$this->cache_class = 'CachePDO';
$location = substr($location, 4);
break;
default:
$this->cache_class = 'CacheFile';
break;
}
}
// Set the remaining cache information.
$this->cache_location = $location;
$this->cache_compress = $gzip;
return $this;
}