Skip to content

Commit

Permalink
Merge pull request #363 from phalcon/2.0.x
Browse files Browse the repository at this point in the history
2.0.2
  • Loading branch information
sergeyklay committed May 26, 2015
2 parents b56b223 + 6523974 commit a1ded35
Show file tree
Hide file tree
Showing 33 changed files with 692 additions and 554 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ composer.phar
/codeception/acceptance/WebGuy.php
/codeception/functional/TestGuy.php
/codeception/unit/CodeGuy.php
/build
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ language: php

env:
- PHALCON_VERSION="2.0.x"
- PHALCON_VERSION="2.0.0"
- PHALCON_VERSION="phalcon-v2.0.2"
- PHALCON_VERSION="phalcon-v2.0.1"
- PHALCON_VERSION="phalcon-v2.0.0"

install:
- tests/memcached.sh
Expand Down
142 changes: 74 additions & 68 deletions Library/Phalcon/Cache/Backend/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
namespace Phalcon\Cache\Backend;

use Phalcon\Cache\Exception;
use Phalcon\Cache\FrontendInterface;
use Phalcon\Db;
use Phalcon\Db\AdapterInterface as DbAdapterInterface;

/**
* Phalcon\Cache\Backend\Database
Expand All @@ -29,23 +31,40 @@
class Database extends Prefixable
{

/**
* @var \Phalcon\Db\AdapterInterface
*/
protected $db = null;

/**
* @var string
*/
protected $table = null;

/**
* Class constructor.
*
* @param \Phalcon\Cache\FrontendInterface $frontend
* @param array $options
* @throws \Phalcon\Cache\Exception
*/
public function __construct($frontend, $options = array())
public function __construct(FrontendInterface $frontend, $options = array())
{
if (!isset($options['db'])) {
throw new Exception("Parameter 'db' is required");
}

if (!($options['db'] instanceof DbAdapterInterface)) {
throw new Exception("Parameter 'db' must implement Phalcon\\Db\\AdapterInterface");
}

if (!isset($options['table'])) {
throw new Exception("Parameter 'table' is required");
}

$this->db = $options['db'];
$this->table = $options['table'];

parent::__construct($frontend, $options);
}

Expand All @@ -58,30 +77,25 @@ public function __construct($frontend, $options = array())
*/
public function get($keyName, $lifetime = null)
{
$prefixedKey = $this->getPrefixedIdentifier($keyName);
$options = $this->getOptions();
$sql = "SELECT data, lifetime FROM " . $options['table'] . " WHERE key_name = ?";
$cache = $options['db']->fetchOne($sql, Db::FETCH_ASSOC, array($prefixedKey));
$prefixedKey = $this->getPrefixedIdentifier($keyName);
$sql = "SELECT data, lifetime FROM " . $this->table . " WHERE key_name = ?";
$cache = $this->db->fetchOne($sql, Db::FETCH_ASSOC, array($prefixedKey));
$this->_lastKey = $prefixedKey;

if (!$cache) {
return null;
}

/** @var \Phalcon\Cache\FrontendInterface $frontend */
$frontend = $this->getFrontend();

if ($lifetime === null) {
$lifetime = $frontend->getLifetime();
}

//Remove the cache if expired
if ($cache['lifetime'] < (time() - $lifetime)) {
$options['db']->execute("DELETE FROM " . $options['table'] . " WHERE key_name = ?", array($prefixedKey));
// Remove the cache if expired
if ($cache['lifetime'] < time()) {
$this->db->execute("DELETE FROM " . $this->table . " WHERE key_name = ?", array($prefixedKey));

return null;
}

$this->setLastKey($keyName);

return $frontend->afterRetrieve($cache['data']);
}

Expand All @@ -97,50 +111,55 @@ public function get($keyName, $lifetime = null)
public function save($keyName = null, $content = null, $lifetime = null, $stopBuffer = true)
{
if ($keyName === null) {
$lastKey = $this->_lastKey;
$prefixedKey = $this->_lastKey;
} else {
$lastKey = $keyName;
$prefixedKey = $this->getPrefixedIdentifier($keyName);
}

if (!$lastKey) {
if (!$prefixedKey) {
throw new Exception('The cache must be started first');
}

$options = $this->getOptions();
/** @var \Phalcon\Cache\FrontendInterface $frontend */
$frontend = $this->getFrontend();

if ($content === null) {
$content = $frontend->getContent();
$cachedContent = $frontend->getContent();
} else {
$cachedContent = $content;
}

if (null === $lifetime) {
$lifetime = $frontend->getLifetime();
}

$lifetime = time() + $lifetime;

// Check if the cache already exist
$prefixedKey = $this->getPrefixedIdentifier($keyName);
$sql = "SELECT data, lifetime FROM " . $options['table'] . " WHERE key_name = ?";
$cache = $options['db']->fetchOne($sql, Db::FETCH_ASSOC, array($prefixedKey));
$sql = "SELECT data, lifetime FROM " . $this->table . " WHERE key_name = ?";
$cache = $this->db->fetchOne($sql, Db::FETCH_ASSOC, array($prefixedKey));

if (!$cache) {
$options['db']->execute("INSERT INTO " . $options['table'] . " VALUES (?, ?, ?)", array(
$this->db->execute("INSERT INTO " . $this->table . " VALUES (?, ?, ?)", array(
$prefixedKey,
$frontend->beforeStore($content),
time()
$frontend->beforeStore($cachedContent),
$lifetime
));
} else {
$options['db']->execute(
"UPDATE " . $options['table'] . " SET data = ?, lifetime = ? WHERE key_name = ?",
$this->db->execute(
"UPDATE " . $this->table . " SET data = ?, lifetime = ? WHERE key_name = ?",
array(
$frontend->beforeStore($content),
time(),
$frontend->beforeStore($cachedContent),
$lifetime,
$prefixedKey
)
);
}

// Stop the buffer, this only applies for Phalcon\Cache\Frontend\Output
if ($stopBuffer) {
$frontend->stop();
}

// Print the buffer, this only applies for Phalcon\Cache\Frontend\Output
if ($frontend->isBuffering()) {
echo $content;
}
Expand All @@ -157,15 +176,14 @@ public function save($keyName = null, $content = null, $lifetime = null, $stopBu
public function delete($keyName)
{
$prefixedKey = $this->getPrefixedIdentifier($keyName);
$options = $this->getOptions();
$sql = "SELECT COUNT(*) AS rowcount FROM " . $options['table'] . " WHERE key_name = ?";
$row = $options['db']->fetchOne($sql, Db::FETCH_ASSOC, array($prefixedKey));
$sql = "SELECT COUNT(*) AS rowcount FROM " . $this->table . " WHERE key_name = ?";
$row = $this->db->fetchOne($sql, Db::FETCH_ASSOC, array($prefixedKey));

if (!$row['rowcount']) {
return false;
}

return $options['db']->execute("DELETE FROM " . $options['table'] . " WHERE key_name = ?", array($prefixedKey));
return $this->db->execute("DELETE FROM " . $this->table . " WHERE key_name = ?", array($prefixedKey));
}

/**
Expand All @@ -176,32 +194,26 @@ public function delete($keyName)
*/
public function queryKeys($prefix = null)
{
$options = $this->getOptions();
$optionsPrefix = !empty($options['prefix'])
? $options['prefix']
: '';

if ($prefix != null || !empty($optionsPrefix)) {
if ($prefix == null) {
$prefix = $this->getPrefixedIdentifier('');
} else {
$prefix = $this->getPrefixedIdentifier($prefix);
}

$sql = "SELECT key_name FROM " . $options['table'] . " WHERE key_name LIKE ? ORDER BY lifetime";
$caches = $options['db']->query($sql, array($prefix));
if (!$prefix) {
$prefix = $this->_prefix;
} else {
$sql = "SELECT key_name FROM " . $options['table'] . " ORDER BY lifetime";
$caches = $options['db']->query($sql);
$prefix = $this->getPrefixedIdentifier($prefix);
}

$caches->setFetchMode(Db::FETCH_ASSOC);
if (!empty($prefix)) {
$sql = "SELECT key_name FROM " . $this->table . " WHERE key_name LIKE ? ORDER BY lifetime";
$rs = $this->db->query($sql, array($prefix . '%'));
} else {
$sql = "SELECT key_name FROM " . $this->table . " ORDER BY lifetime";
$rs = $this->db->query($sql);
}

$rs->setFetchMode(Db::FETCH_ASSOC);

$keys = array();
while ($row = $caches->fetch()) {
$keys[] = !empty($optionsPrefix)
? str_replace($optionsPrefix, '', $row['key_name'])
: $row['key_name'];

while ($row = $rs->fetch()) {
$keys[] = !empty($prefix) ? str_replace($prefix, '', $row['key_name']) : $row['key_name'];
}

return $keys;
Expand All @@ -217,21 +229,16 @@ public function queryKeys($prefix = null)
public function exists($keyName = null, $lifetime = null)
{
$prefixedKey = $this->getPrefixedIdentifier($keyName);
$options = $this->getOptions();
$sql = "SELECT lifetime FROM " . $options['table'] . " WHERE key_name = ?";
$cache = $options['db']->fetchOne($sql, Db::FETCH_ASSOC, array($prefixedKey));
$sql = "SELECT lifetime FROM " . $this->table . " WHERE key_name = ?";
$cache = $this->db->fetchOne($sql, Db::FETCH_ASSOC, array($prefixedKey));

if (!$cache) {
return false;
}

if ($lifetime === null) {
$lifetime = $this->getFrontend()->getLifetime();
}

//Remove the cache if expired
if ($cache['lifetime'] < (time() - $lifetime)) {
$options['db']->execute("DELETE FROM " . $options['table'] . " WHERE key_name = ?", array($prefixedKey));
// Remove the cache if expired
if ($cache['lifetime'] < time()) {
$this->db->execute("DELETE FROM " . $this->table . " WHERE key_name = ?", array($prefixedKey));

return false;
}
Expand All @@ -246,8 +253,7 @@ public function exists($keyName = null, $lifetime = null)
*/
public function flush()
{
$options = $this->getOptions();
$options['db']->execute('DELETE FROM ' . $options['table']);
$this->db->execute('DELETE FROM ' . $this->table);

return true;
}
Expand Down
8 changes: 1 addition & 7 deletions Library/Phalcon/Cache/Backend/Prefixable.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ abstract class Prefixable extends Backend implements BackendInterface
*/
protected function getPrefixedIdentifier($id)
{
$options = $this->getOptions();

if (!empty($options['prefix'])) {
return $options['prefix'] . $id;
}

return $id;
return $this->_prefix . $id;
}
}
53 changes: 3 additions & 50 deletions Library/Phalcon/Cache/Backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,53 +61,6 @@ echo $time;

```

Redis
-----
This adapter uses a [Redis](http://redis.io) backend to store the cached content and [phpredis](https://github.com/nicolasff/phpredis) extension:

```php

$di->set('cache', function() {

//Connect to redis
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

//Create a Data frontend and set a default lifetime to 1 hour
$frontend = new Phalcon\Cache\Frontend\Data(array(
'lifetime' => 3600
));

//Create the cache passing the connection
$cache = new Phalcon\Cache\Backend\Redis($frontend, array(
'redis' => $redis
));

return $cache;
});

```

Memcached
-----
This adapter uses a Memcache backend to store the cached content:

```php

$di->set('cache', function() {

//Create a Data frontend and set a default lifetime to 1 hour
$frontend = new Phalcon\Cache\Frontend\Data(array(
'lifetime' => 3600
));

// Set up Memcached and use tracking to be able to clean it later.
// You should not use tracking if you're going to store a lot of keys!
$cache = new Memcached($frontend, array(
'tracking' => true
));

return $cache;
});

```
Wincache
--------
This adapter uses [windows cache extension](http://pecl.php.net/package/wincache) for PHP
Loading

0 comments on commit a1ded35

Please sign in to comment.