Skip to content

Commit

Permalink
Throw an exception if Apc Extension is not enabled rather than return…
Browse files Browse the repository at this point in the history
… false
  • Loading branch information
Tom Burgess committed Apr 22, 2015
1 parent 046cfaa commit d3346ae
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 46 deletions.
52 changes: 6 additions & 46 deletions src/OhCache/Adapters/AdapterApc.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,25 @@
namespace OhCache\Adapters;

use OhCache\Adapters\AdapterAbstract;

use OhCache\Exception\ApcExtensionNotLoaded;
/**
* Interact with APC
*
* AdapterApc
*/
class AdapterApc extends AdapterAbstract
{
/**
* Whether PHP has the apc extension loaded.
*
* @var boolean
*/
private $hasApc = false;

/**
* Construct, and check the presence of the APC extension
*
* @param array $config
*/
public function __construct(array $config = array())
{
$this->checkExtension();
if (array_key_exists('prefix', $config)) {
$this->prefix = $config['prefix'];
}
$this->checkExtension();
}

/**
Expand All @@ -66,12 +59,6 @@ public function __construct(array $config = array())
*/
public function get($key)
{
if (!$this->hasApc) {
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
}

$record = apc_fetch(
$this->getKeyString($key),
$found
Expand All @@ -93,12 +80,6 @@ public function get($key)
*/
public function set($key, $value, $ttl = self::DEFAULT_TTL)
{
if (!$this->hasApc) {
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
}

return apc_store(
$this->getKeyString($key),
$value,
Expand All @@ -117,12 +98,6 @@ public function set($key, $value, $ttl = self::DEFAULT_TTL)
*/
public function setIfNotExists($key, $value, $ttl = self::DEFAULT_TTL)
{
if (!$this->hasApc) {
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
}

return apc_add(
$this->getKeyString($key),
$value,
Expand Down Expand Up @@ -157,12 +132,6 @@ public function has($key)
*/
public function renew($key, $ttl = self::DEFAULT_TTL)
{
if (!$this->hasApc) {
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
}

$key = $this->getKeyString($key);
$val = apc_fetch($key, $fetched);

Expand All @@ -181,12 +150,6 @@ public function renew($key, $ttl = self::DEFAULT_TTL)
*/
public function remove($key)
{
if (!$this->hasApc) {
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
}

return apc_delete($this->getKeyString($key));
}

Expand All @@ -197,20 +160,17 @@ public function remove($key)
*/
public function flush()
{
if (!$this->hasApc) {
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
}

return apc_clear_cache();
}

/**
* Check if apc is enabled
* @throws ApcExtensionNotLoaded
*/
private function checkExtension()
{
$this->hasApc = (extension_loaded('apc'));
if (false === extension_loaded('apc')) {
throw new ApcExtensionNotLoaded('Apc Extension Not Loaded');
}
}
}
37 changes: 37 additions & 0 deletions src/OhCache/Exception/ApcExtensionNotLoaded.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* This file is part of the OhCache library
*
* @author Tom Burgess <tom.burgess@tburgess.net>
* @copyright 2014 Roger Thomas <rogere84@gmail.com>
* @package OhCache
*
* MIT LICENSE
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace OhCache\Exception;

/**
* ApcExtensionNotLoaded
*/
class ApcExtensionNotLoaded extends \Exception
{
}

0 comments on commit d3346ae

Please sign in to comment.