rhymix/common/framework/drivers/cache/apc.php
Kijin Sung ea5d955072 Use apcu_* functions for APC cache
PHP 7에서는 apcu 확장모듈이 apc의 기능을 제공합니다.
이 때 apcu_* 함수는 존재하지만, apc_* 함수가 존재한다는 보장은 없습니다.
(리눅스 배포판에 따라서는 apcu-bc 패키지를 추가로 설치해야 합니다.)

라이믹스는 PHP 7만을 타겟으로 하므로, apcu_* 함수를 직접 사용하도록
변경하여 apc_* 함수가 존재하지 않는 경우 오류가 발생하지 않도록 합니다.
2018-12-19 16:52:36 +09:00

179 lines
3.3 KiB
PHP

<?php
namespace Rhymix\Framework\Drivers\Cache;
/**
* The APC cache driver.
*/
class APC implements \Rhymix\Framework\Drivers\CacheInterface
{
/**
* Set this flag to false to disable cache prefixes.
*/
public $prefix = true;
/**
* The singleton instance is stored here.
*/
protected static $_instance = null;
/**
* Direct invocation of the constructor is not permitted.
*/
protected function __construct()
{
}
/**
* Create a new instance of the current cache driver, using the given settings.
*
* @param array $config
* @return void
*/
public static function getInstance(array $config)
{
if (self::$_instance === null)
{
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Check if the current cache driver is supported on this server.
*
* This method returns true on success and false on failure.
*
* @return bool
*/
public static function isSupported()
{
return function_exists('apcu_exists');
}
/**
* Validate cache settings.
*
* This method returns true on success and false on failure.
*
* @param mixed $config
* @return bool
*/
public static function validateSettings($config)
{
return true;
}
/**
* Get the value of a key.
*
* This method returns null if the key was not found.
*
* @param string $key
* @return mixed
*/
public function get($key)
{
$value = apcu_fetch($key);
return $value === false ? null : $value;
}
/**
* Set the value to a key.
*
* This method returns true on success and false on failure.
* $ttl is measured in seconds. If it is zero, the key should not expire.
*
* @param string $key
* @param mixed $value
* @param int $ttl
* @param bool $force
* @return bool
*/
public function set($key, $value, $ttl = 0, $force = false)
{
return apcu_store($key, $value, $ttl);
}
/**
* Delete a key.
*
* This method returns true on success and false on failure.
* If the key does not exist, it should return false.
*
* @param string $key
* @return bool
*/
public function delete($key)
{
return apcu_delete($key);
}
/**
* Check if a key exists.
*
* This method returns true on success and false on failure.
*
* @param string $key
* @return bool
*/
public function exists($key)
{
return apcu_exists($key);
}
/**
* Increase the value of a key by $amount.
*
* If the key does not exist, this method assumes that the current value is zero.
* This method returns the new value.
*
* @param string $key
* @param int $amount
* @return int
*/
public function incr($key, $amount)
{
$result = apcu_inc($key, $amount);
if ($result === false)
{
apcu_store($key, $amount);
$result = $amount;
}
return $result;
}
/**
* Decrease the value of a key by $amount.
*
* If the key does not exist, this method assumes that the current value is zero.
* This method returns the new value.
*
* @param string $key
* @param int $amount
* @return int
*/
public function decr($key, $amount)
{
$result = apcu_dec($key, $amount);
if ($result === false)
{
apcu_store($key, 0 - $amount);
$result = 0 - $amount;
}
return $result;
}
/**
* Clear all keys from the cache.
*
* This method returns true on success and false on failure.
*
* @return bool
*/
public function clear()
{
return apcu_clear_cache();
}
}