__construct ( $name, $location, $expires, $gzip )

Constructs a new instance of this class.

Tested with MySQL 5.0.x, PostgreSQL, and SQLite 3.x. SQLite 2.x is assumed to work. No other PDO-supported databases have been tested (e.g. Oracle, Microsoft SQL Server, IBM DB2, ODBC, Sybase, Firebird). Feel free to send patches for additional database support.

See http://php.net/pdo for more information.

Access

public

Parameters

Parameter

Type

Required

Description

$name

string

Required

A name to uniquely identify the cache object.

$location

string

Optional

The location to store the cache object in. This may vary by cache method. The default value is NULL.

$expires

integer

Optional

The number of seconds until a cache object is considered stale. The default value is 0.

$gzip

boolean

Optional

Whether data should be gzipped before being stored. The default value is true.

Returns

Type

Description

object

Reference to the cache object.

Source

Method defined in lib/cachecore/cachepdo.class.php | Toggle source view (46 lines) | View on GitHub

public function __construct($name, $location = null, $expires = 0, $gzip = true)
{
    // Make sure the name is no longer than 40 characters.
    $name = sha1($name);

    // Call parent constructor and set id.
    parent::__construct($name, $location, $expires, $gzip);
    $this->id = $this->name;
    $options = array();

    // Check if the location contains :// (e.g. mysql://user:pass@hostname:port/table)
    if (stripos($location, '://') === false)
    {
        // No? Just pass it through.
        $this->dsn = parse_url($location);
        $this->dsn_string = $location;
    }
    else
    {
        // Yes? Parse and set the DSN
        $this->dsn = parse_url($location);
        $this->dsn_string = $this->dsn['scheme'] . ':host=' . $this->dsn['host'] . ((isset($this->dsn['port'])) ? ';port=' . $this->dsn['port'] : '') . ';dbname=' . substr($this->dsn['path'], 1);
    }

    // Make sure that user/pass are defined.
    $user = isset($this->dsn['user']) ? $this->dsn['user'] : null;
    $pass = isset($this->dsn['pass']) ? $this->dsn['pass'] : null;

    // Set persistence for databases that support it.
    switch ($this->dsn['scheme'])
    {
        case 'mysql': // MySQL
        case 'pgsql': // PostgreSQL
            $options[PDO::ATTR_PERSISTENT] = true;
            break;
    }

    // Instantiate a new PDO object with a persistent connection.
    $this->pdo = new PDO($this->dsn_string, $user, $pass, $options);

    // Define prepared statements for improved performance.
    $this->create = $this->pdo->prepare("INSERT INTO cache (id, expires, data) VALUES (:id, :expires, :data)");
    $this->read = $this->pdo->prepare("SELECT id, expires, data FROM cache WHERE id = :id");
    $this->reset = $this->pdo->prepare("UPDATE cache SET expires = :expires WHERE id = :id");
    $this->delete = $this->pdo->prepare("DELETE FROM cache WHERE id = :id");
}

Copyright © 2010–2013 Amazon Web Services, LLC


Feedback