diff --git a/CHANGELOG-3.3.md b/CHANGELOG-3.3.md index 605d3640aad..68f511ff183 100644 --- a/CHANGELOG-3.3.md +++ b/CHANGELOG-3.3.md @@ -8,6 +8,7 @@ - Added `Phalcon\Mvc\Application::sendHeadersOnHandleRequest` to enable or disable sending headers by each request handling [#13101](https://github.com/phalcon/cphalcon/issues/13101) - Added `Phalcon\Mvc\Application::sendCookiesOnHandleRequest` to enable or disable sending cookies by each request handling [#13101](https://github.com/phalcon/cphalcon/issues/13101) - Added ability to use PDO option aliases on database connect [#13010](https://github.com/phalcon/cphalcon/issues/13010) +- Added `Phalcon\Mvc\Model\MetaData\Apcu` [#13078](https://github.com/phalcon/cphalcon/issues/13078) - Fixed `Phalcon\Mvc\Model\Query\Builder::getPhql` to correct generate PHQL in argument's array when using order DESC or ASC [#11827](https://github.com/phalcon/cphalcon/issues/11827) - Fixed `Phalcon\Db\Dialect\Postgresql::createTable` to produce valid SQL for table definition with `BOOLEAN` types [#13132](https://github.com/phalcon/cphalcon/issues/13132) - Fixed `Phalcon\Db\Dialect\Postgresql::_castDefault` to return correct value for `BOOLEAN` type [#13132](https://github.com/phalcon/cphalcon/issues/13132), [phalcon/phalcon-devtools#1118](https://github.com/phalcon/phalcon-devtools/issues/1118) diff --git a/phalcon/mvc/model/metadata/apc.zep b/phalcon/mvc/model/metadata/apc.zep index f96a5c47e78..7e1a3dde2e7 100644 --- a/phalcon/mvc/model/metadata/apc.zep +++ b/phalcon/mvc/model/metadata/apc.zep @@ -39,6 +39,9 @@ use Phalcon\Mvc\Model\Exception; * ] * ); * + * + * @deprecated Deprecated since 3.3.0, will be removed in 4.0.0 + * @see Phalcon\Mvc\Model\Metadata\Apcu */ class Apc extends MetaData { diff --git a/phalcon/mvc/model/metadata/apcu.zep b/phalcon/mvc/model/metadata/apcu.zep new file mode 100644 index 00000000000..3c8b65158ca --- /dev/null +++ b/phalcon/mvc/model/metadata/apcu.zep @@ -0,0 +1,92 @@ + +/* + +------------------------------------------------------------------------+ + | Phalcon Framework | + +------------------------------------------------------------------------+ + | Copyright (c) 2011-2017 Phalcon Team (https://phalconphp.com) | + +------------------------------------------------------------------------+ + | This source file is subject to the New BSD License that is bundled | + | with this package in the file LICENSE.txt. | + | | + | If you did not receive a copy of the license and are unable to | + | obtain it through the world-wide-web, please send an email | + | to license@phalconphp.com so we can send you a copy immediately. | + +------------------------------------------------------------------------+ + | Authors: Andres Gutierrez | + | Eduar Carvajal | + +------------------------------------------------------------------------+ + */ + +namespace Phalcon\Mvc\Model\MetaData; + +use Phalcon\Mvc\Model\MetaData; +use Phalcon\Mvc\Model\Exception; + +/** + * Phalcon\Mvc\Model\MetaData\Apcu + * + * Stores model meta-data in the APCu cache. Data will erased if the web server is restarted + * + * By default meta-data is stored for 48 hours (172800 seconds) + * + * You can query the meta-data by printing apcu_fetch('$PMM$') or apcu_fetch('$PMM$my-app-id') + * + * + * $metaData = new \Phalcon\Mvc\Model\Metadata\Apcu( + * [ + * "prefix" => "my-app-id", + * "lifetime" => 86400, + * ] + * ); + * + */ +class Apcu extends MetaData +{ + + protected _prefix = ""; + + protected _ttl = 172800; + + protected _metaData = []; + + /** + * Phalcon\Mvc\Model\MetaData\Apcu constructor + * + * @param array options + */ + public function __construct(options = null) + { + var prefix, ttl; + + if typeof options == "array" { + if fetch prefix, options["prefix"] { + let this->_prefix = prefix; + } + if fetch ttl, options["lifetime"] { + let this->_ttl = ttl; + } + } + } + + /** + * Reads meta-data from APCu + */ + public function read(string! key) -> array | null + { + var data; + + let data = apcu_fetch("$PMM$" . this->_prefix . key); + if typeof data == "array" { + return data; + } + return null; + } + + /** + * Writes the meta-data to APCu + */ + public function write(string! key, var data) -> void + { + apcu_store("$PMM$" . this->_prefix . key, data, this->_ttl); + } +} diff --git a/tests/unit/Mvc/Model/MetaData/ApcuCest.php b/tests/unit/Mvc/Model/MetaData/ApcuCest.php new file mode 100644 index 00000000000..e461517e46a --- /dev/null +++ b/tests/unit/Mvc/Model/MetaData/ApcuCest.php @@ -0,0 +1,77 @@ + + * @author Serghei Iakovlev + * @author Wojciech Ĺšlawski + * @package Phalcon\Test\Unit\Mvc\Model\Metadata + * + * The contents of this file are subject to the New BSD License that is + * bundled with this package in the file LICENSE.txt + * + * If you did not receive a copy of the license and are unable to obtain it + * through the world-wide-web, please send an email to license@phalconphp.com + * so that we can send you a copy immediately. + */ +class ApcuCest +{ + private $data; + + public function _before(UnitTester $I) + { + if (!function_exists('apc_fetch')) { + throw new \PHPUnit_Framework_SkippedTestError( + 'Warning: apc extension is not loaded' + ); + } + + $I->haveServiceInDi('modelsMetadata', function () { + return new Apcu([ + 'prefix' => 'app\\', + 'lifetime' => 60 + ]); + }, true); + + $this->data = require PATH_FIXTURES . 'metadata/robots.php'; + apcu_clear_cache(); + } + + public function apc(UnitTester $I) + { + $I->wantTo('fetch metadata from apc cache'); + + /** @var \Phalcon\Mvc\Model\MetaDataInterface $md */ + $md = $I->grabServiceFromDi('modelsMetadata'); + + $md->reset(); + $I->assertTrue($md->isEmpty()); + + Robots::findFirst(); + + $I->assertEquals( + $this->data['meta-robots-robots'], + apcu_fetch('$PMM$app\meta-phalcon\test\models\robots-robots') + ); + + $I->assertEquals( + $this->data['map-robots'], + apcu_fetch('$PMM$app\map-phalcon\test\models\robots') + ); + + $I->assertFalse($md->isEmpty()); + + $md->reset(); + $I->assertTrue($md->isEmpty()); + } +}