mirror of
https://github.com/Mibew/handlebars.php.git
synced 2025-04-15 10:57:24 +03:00
Merge pull request #114 from szicsu/apc_improvement
APC cache improvement
This commit is contained in:
commit
8652b01b84
@ -20,7 +20,7 @@ namespace Handlebars\Cache;
|
||||
use Handlebars\Cache;
|
||||
|
||||
/**
|
||||
* A dummy array cache
|
||||
* A APC cache
|
||||
*
|
||||
* @category Xamin
|
||||
* @package Handlebars
|
||||
@ -34,6 +34,21 @@ use Handlebars\Cache;
|
||||
class APC implements Cache
|
||||
{
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $_prefix;
|
||||
|
||||
/**
|
||||
* Construct the APC cache.
|
||||
*
|
||||
* @param string|null $prefix optional key prefix, defaults to null
|
||||
*/
|
||||
public function __construct( $prefix = null )
|
||||
{
|
||||
$this->_prefix = (string)$prefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cache for $name if exist.
|
||||
*
|
||||
@ -43,10 +58,11 @@ class APC implements Cache
|
||||
*/
|
||||
public function get($name)
|
||||
{
|
||||
if (apc_exists($name)) {
|
||||
return apc_fetch($name);
|
||||
}
|
||||
return false;
|
||||
$success = null;
|
||||
$result = apc_fetch($this->_getKey($name), $success);
|
||||
|
||||
return $success ? $result : false;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -59,7 +75,7 @@ class APC implements Cache
|
||||
*/
|
||||
public function set($name, $value)
|
||||
{
|
||||
apc_store($name, $value);
|
||||
apc_store($this->_getKey($name), $value);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -71,7 +87,19 @@ class APC implements Cache
|
||||
*/
|
||||
public function remove($name)
|
||||
{
|
||||
apc_delete($name);
|
||||
apc_delete($this->_getKey($name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the full cache key for a given cache item's
|
||||
*
|
||||
* @param string $name Name of the cache item
|
||||
*
|
||||
* @return string full cache key of cached item
|
||||
*/
|
||||
private function _getKey($name)
|
||||
{
|
||||
return $this->_prefix . ':' . $name;
|
||||
}
|
||||
|
||||
}
|
||||
|
100
tests/Xamin/Cache/APCTest.php
Normal file
100
tests/Xamin/Cache/APCTest.php
Normal file
@ -0,0 +1,100 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Handlebars-php
|
||||
* Base on mustache-php https://github.com/bobthecow/mustache.php
|
||||
*
|
||||
* PHP version 5.3
|
||||
*
|
||||
* @category Xamin
|
||||
* @package Handlebars
|
||||
* @author fzerorubigd <fzerorubigd@gmail.com>
|
||||
* @author Dmitriy Simushev <simushevds@gmail.com>
|
||||
* @copyright 2013 (c) f0ruD A
|
||||
* @license MIT <http://opensource.org/licenses/MIT>
|
||||
* @version GIT: $Id$
|
||||
* @link http://xamin.ir
|
||||
*/
|
||||
|
||||
/**
|
||||
* Test of APC cache driver
|
||||
*
|
||||
* Run without sikp:
|
||||
* php -d apc.enable_cli=1 ./vendor/bin/phpunit
|
||||
*
|
||||
* @category Xamin
|
||||
* @package Handlebars
|
||||
* @subpackage Test
|
||||
* @author Tamás Szijártó <szijarto.tamas.developer@gmail.com>
|
||||
* @license MIT <http://opensource.org/licenses/MIT>
|
||||
* @version Release: @package_version@
|
||||
* @link http://xamin.ir
|
||||
*/
|
||||
class APCTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
if ( ! extension_loaded('apc') || false === @apc_cache_info()) {
|
||||
$this->markTestSkipped('The ' . __CLASS__ .' requires the use of APC');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the new driver
|
||||
*
|
||||
* @param null|string $prefix optional key prefix, defaults to null
|
||||
*
|
||||
* @return \Handlebars\Cache\APC
|
||||
*/
|
||||
private function _getCacheDriver( $prefix = null )
|
||||
{
|
||||
return new \Handlebars\Cache\APC($prefix);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test with cache prefix
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testWithPrefix()
|
||||
{
|
||||
$prefix = __CLASS__;
|
||||
$driver = $this->_getCacheDriver($prefix);
|
||||
|
||||
$this->assertEquals(false, $driver->get('foo'));
|
||||
|
||||
$driver->set('foo', 10);
|
||||
$this->assertEquals(10, $driver->get('foo'));
|
||||
|
||||
$driver->set('foo', array(12));
|
||||
$this->assertEquals(array(12), $driver->get('foo'));
|
||||
|
||||
$driver->remove('foo');
|
||||
$this->assertEquals(false, $driver->get('foo'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test without cache prefix
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testWithoutPrefix()
|
||||
{
|
||||
$driver = $this->_getCacheDriver();
|
||||
|
||||
$this->assertEquals(false, $driver->get('foo'));
|
||||
|
||||
$driver->set('foo', 20);
|
||||
$this->assertEquals(20, $driver->get('foo'));
|
||||
|
||||
$driver->set('foo', array(22));
|
||||
$this->assertEquals(array(22), $driver->get('foo'));
|
||||
|
||||
$driver->remove('foo');
|
||||
$this->assertEquals(false, $driver->get('foo'));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user