Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements #13078 #13182

Merged
merged 1 commit into from
Nov 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG-3.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions phalcon/mvc/model/metadata/apc.zep
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ use Phalcon\Mvc\Model\Exception;
* ]
* );
*</code>
*
* @deprecated Deprecated since 3.3.0, will be removed in 4.0.0
* @see Phalcon\Mvc\Model\Metadata\Apcu
*/
class Apc extends MetaData
{
Expand Down
92 changes: 92 additions & 0 deletions phalcon/mvc/model/metadata/apcu.zep
Original file line number Diff line number Diff line change
@@ -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 <andres@phalconphp.com> |
| Eduar Carvajal <eduar@phalconphp.com> |
+------------------------------------------------------------------------+
*/

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')
*
*<code>
* $metaData = new \Phalcon\Mvc\Model\Metadata\Apcu(
* [
* "prefix" => "my-app-id",
* "lifetime" => 86400,
* ]
* );
*</code>
*/
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);
}
}
77 changes: 77 additions & 0 deletions tests/unit/Mvc/Model/MetaData/ApcuCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Phalcon\Test\Unit\Mvc\Model\MetaData;

use UnitTester;
use Phalcon\Test\Models\Robots;
use Phalcon\Mvc\Model\Metadata\Apcu;

/**
* \Phalcon\Test\Unit\Mvc\Model\Metadata\ApcuCest
* Tests the \Phalcon\Mvc\Model\Metadata\Apcu component
*
* @copyright (c) 2011-2017 Phalcon Team
* @link http://www.phalconphp.com
* @author Andres Gutierrez <andres@phalconphp.com>
* @author Serghei Iakovlev <serghei@phalconphp.com>
* @author Wojciech Ślawski <jurigag@gmail.com>
* @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());
}
}