forked from kevinowino869/mitrobill
update PEAR
This commit is contained in:
@ -1,18 +1,19 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* ~~summary~~
|
||||
*
|
||||
* ~~description~~
|
||||
*
|
||||
* Wrapper for shared memory and locking functionality across different extensions.
|
||||
|
||||
*
|
||||
* Allows you to share data across requests as long as the PHP process is running. One of APC or WinCache is required to accomplish this, with other extensions being potentially pluggable as adapters.
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
*
|
||||
* @category Caching
|
||||
* @package PEAR2_Cache_SHM
|
||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||
* @copyright 2011 Vasil Rangelov
|
||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||
* @version 0.1.3
|
||||
* @version 0.2.0
|
||||
* @link http://pear2.php.net/PEAR2_Cache_SHM
|
||||
*/
|
||||
|
||||
@ -38,9 +39,9 @@ use PEAR2\Cache\SHM\InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Main class for this package.
|
||||
*
|
||||
*
|
||||
* Automatically chooses an adapter based on the available extensions.
|
||||
*
|
||||
*
|
||||
* @category Caching
|
||||
* @package PEAR2_Cache_SHM
|
||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||
@ -50,18 +51,20 @@ use PEAR2\Cache\SHM\InvalidArgumentException;
|
||||
abstract class SHM implements IteratorAggregate
|
||||
{
|
||||
/**
|
||||
* @var array An array of adapter names that meet their requirements.
|
||||
* An array of adapter names that meet their requirements.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private static $_adapters = array();
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new shared memory storage.
|
||||
*
|
||||
* Estabilishes a separate persistent storage. Adapter is automatically
|
||||
*
|
||||
* Establishes a separate persistent storage. Adapter is automatically
|
||||
* chosen based on the available extensions.
|
||||
*
|
||||
*
|
||||
* @param string $persistentId The ID for the storage.
|
||||
*
|
||||
*
|
||||
* @return static|SHM A new instance of an SHM adapter (child of this
|
||||
* class).
|
||||
*/
|
||||
@ -79,28 +82,28 @@ abstract class SHM implements IteratorAggregate
|
||||
1
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks if the adapter meets its requirements.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public static function isMeetingRequirements()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Registers an adapter.
|
||||
*
|
||||
*
|
||||
* Registers an SHM adapter, allowing you to call it with {@link factory()}.
|
||||
*
|
||||
*
|
||||
* @param string $adapter FQCN of adapter. A valid adapter is one that
|
||||
* extends this class. The class will be autoloaded if not already
|
||||
* present.
|
||||
* @param bool $prepend Whether to prepend this adapter into the list of
|
||||
* possible adapters, instead of appending to it.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
final public static function registerAdapter($adapter, $prepend = false)
|
||||
@ -121,244 +124,244 @@ abstract class SHM implements IteratorAggregate
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Adds a value to the shared memory storage.
|
||||
*
|
||||
*
|
||||
* Adds a value to the storage if it doesn't exist, or fails if it does.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of key to associate the value with.
|
||||
* @param mixed $value Value for the specified key.
|
||||
* @param int $ttl Seconds to store the value. If set to 0 indicates no
|
||||
* time limit.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function __invoke($key, $value, $ttl = 0)
|
||||
{
|
||||
return $this->add($key, $value, $ttl);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets a value from the shared memory storage.
|
||||
*
|
||||
*
|
||||
* This is a magic method, thanks to which any property you attempt to get
|
||||
* the value of will be fetched from the adapter, treating the property name
|
||||
* as the key of the value to get.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of key to get.
|
||||
*
|
||||
*
|
||||
* @return mixed The current value of the specified key.
|
||||
*/
|
||||
public function __get($key)
|
||||
{
|
||||
return $this->get($key);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets a value in the shared memory storage.
|
||||
*
|
||||
*
|
||||
* This is a magic method, thanks to which any property you attempt to set
|
||||
* the value of will be set by the adapter, treating the property name as
|
||||
* the key of the value to set. The value is set without a TTL.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of key to associate the value with.
|
||||
* @param mixed $value Value for the specified key.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function __set($key, $value)
|
||||
{
|
||||
return $this->set($key, $value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks if a specified key is in the storage.
|
||||
*
|
||||
*
|
||||
* This is a magic method, thanks to which any property you call isset() on
|
||||
* will be checked by the adapter, treating the property name as the key
|
||||
* of the value to check.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of key to check.
|
||||
*
|
||||
* @return bool TRUE if the key is in the storage, FALSE otherwise.
|
||||
*
|
||||
* @return bool TRUE if the key is in the storage, FALSE otherwise.
|
||||
*/
|
||||
public function __isset($key)
|
||||
{
|
||||
return $this->exists($key);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Deletes a value from the shared memory storage.
|
||||
*
|
||||
*
|
||||
* This is a magic method, thanks to which any property you attempt to unset
|
||||
* the value of will be unset by the adapter, treating the property name as
|
||||
* the key of the value to delete.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of key to delete.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function __unset($key)
|
||||
{
|
||||
return $this->delete($key);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new shared memory storage.
|
||||
*
|
||||
* Estabilishes a separate persistent storage.
|
||||
*
|
||||
*
|
||||
* Establishes a separate persistent storage.
|
||||
*
|
||||
* @param string $persistentId The ID for the storage. The storage will be
|
||||
* reused if it exists, or created if it doesn't exist. Data and locks
|
||||
* are namespaced by this ID.
|
||||
*/
|
||||
abstract public function __construct($persistentId);
|
||||
|
||||
|
||||
/**
|
||||
* Obtains a named lock.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of the key to obtain. Note that $key may
|
||||
* repeat for each distinct $persistentId.
|
||||
* @param double $timeout If the lock can't be immediatly obtained, the
|
||||
* @param double $timeout If the lock can't be immediately obtained, the
|
||||
* script will block for at most the specified amount of seconds.
|
||||
* Setting this to 0 makes lock obtaining non blocking, and setting it
|
||||
* to NULL makes it block without a time limit.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
abstract public function lock($key, $timeout = null);
|
||||
|
||||
|
||||
/**
|
||||
* Releases a named lock.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of the key to release. Note that $key may
|
||||
* repeat for each distinct $persistentId.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
abstract public function unlock($key);
|
||||
|
||||
|
||||
/**
|
||||
* Checks if a specified key is in the storage.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of key to check.
|
||||
*
|
||||
* @return bool TRUE if the key is in the storage, FALSE otherwise.
|
||||
*
|
||||
* @return bool TRUE if the key is in the storage, FALSE otherwise.
|
||||
*/
|
||||
abstract public function exists($key);
|
||||
|
||||
|
||||
/**
|
||||
* Adds a value to the shared memory storage.
|
||||
*
|
||||
*
|
||||
* Adds a value to the storage if it doesn't exist, or fails if it does.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of key to associate the value with.
|
||||
* @param mixed $value Value for the specified key.
|
||||
* @param int $ttl Seconds to store the value. If set to 0 indicates no
|
||||
* time limit.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
abstract public function add($key, $value, $ttl = 0);
|
||||
|
||||
|
||||
/**
|
||||
* Sets a value in the shared memory storage.
|
||||
*
|
||||
*
|
||||
* Adds a value to the storage if it doesn't exist, overwrites it otherwise.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of key to associate the value with.
|
||||
* @param mixed $value Value for the specified key.
|
||||
* @param int $ttl Seconds to store the value. If set to 0 indicates no
|
||||
* time limit.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
abstract public function set($key, $value, $ttl = 0);
|
||||
|
||||
|
||||
/**
|
||||
* Gets a value from the shared memory storage.
|
||||
*
|
||||
*
|
||||
* Gets the current value, or throws an exception if it's not stored.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of key to get the value of.
|
||||
*
|
||||
*
|
||||
* @return mixed The current value of the specified key.
|
||||
*/
|
||||
abstract public function get($key);
|
||||
|
||||
|
||||
/**
|
||||
* Deletes a value from the shared memory storage.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of key to delete.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
abstract public function delete($key);
|
||||
|
||||
|
||||
/**
|
||||
* Increases a value from the shared memory storage.
|
||||
*
|
||||
*
|
||||
* Increases a value from the shared memory storage. Unlike a plain
|
||||
* set($key, get($key)+$step) combination, this function also implicitly
|
||||
* performs locking.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of key to increase.
|
||||
* @param int $step Value to increase the key by.
|
||||
*
|
||||
*
|
||||
* @return int The new value.
|
||||
*/
|
||||
abstract public function inc($key, $step = 1);
|
||||
|
||||
|
||||
/**
|
||||
* Decreases a value from the shared memory storage.
|
||||
*
|
||||
*
|
||||
* Decreases a value from the shared memory storage. Unlike a plain
|
||||
* set($key, get($key)-$step) combination, this function also implicitly
|
||||
* performs locking.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of key to decrease.
|
||||
* @param int $step Value to decrease the key by.
|
||||
*
|
||||
*
|
||||
* @return int The new value.
|
||||
*/
|
||||
abstract public function dec($key, $step = 1);
|
||||
|
||||
/**
|
||||
* Sets a new value if a key has a certain value.
|
||||
*
|
||||
*
|
||||
* Sets a new value if a key has a certain value. This function only works
|
||||
* when $old and $new are longs.
|
||||
*
|
||||
*
|
||||
* @param string $key Key of the value to compare and set.
|
||||
* @param int $old The value to compare the key against.
|
||||
* @param int $new The value to set the key to.
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
abstract public function cas($key, $old, $new);
|
||||
|
||||
|
||||
/**
|
||||
* Clears the persistent storage.
|
||||
*
|
||||
*
|
||||
* Clears the persistent storage, i.e. removes all keys. Locks are left
|
||||
* intact.
|
||||
*
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract public function clear();
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve an external iterator
|
||||
*
|
||||
*
|
||||
* Returns an external iterator.
|
||||
*
|
||||
*
|
||||
* @param string|null $filter A PCRE regular expression.
|
||||
* Only matching keys will be iterated over.
|
||||
* Setting this to NULL matches all keys of this instance.
|
||||
* @param bool $keysOnly Whether to return only the keys,
|
||||
* or return both the keys and values.
|
||||
*
|
||||
*
|
||||
* @return \Traversable An array with all matching keys as array keys,
|
||||
* and values as array values. If $keysOnly is TRUE, the array keys are
|
||||
* numeric, and the array values are key names.
|
||||
@ -368,4 +371,5 @@ abstract class SHM implements IteratorAggregate
|
||||
|
||||
SHM::registerAdapter('\\' . __NAMESPACE__ . '\SHM\Adapter\Placebo');
|
||||
SHM::registerAdapter('\\' . __NAMESPACE__ . '\SHM\Adapter\Wincache');
|
||||
SHM::registerAdapter('\\' . __NAMESPACE__ . '\SHM\Adapter\APCu');
|
||||
SHM::registerAdapter('\\' . __NAMESPACE__ . '\SHM\Adapter\APC');
|
||||
|
@ -1,18 +1,19 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* ~~summary~~
|
||||
*
|
||||
* ~~description~~
|
||||
*
|
||||
* Wrapper for shared memory and locking functionality across different extensions.
|
||||
|
||||
*
|
||||
* Allows you to share data across requests as long as the PHP process is running. One of APC or WinCache is required to accomplish this, with other extensions being potentially pluggable as adapters.
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
*
|
||||
* @category Caching
|
||||
* @package PEAR2_Cache_SHM
|
||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||
* @copyright 2011 Vasil Rangelov
|
||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||
* @version 0.1.3
|
||||
* @version 0.2.0
|
||||
* @link http://pear2.php.net/PEAR2_Cache_SHM
|
||||
*/
|
||||
/**
|
||||
@ -32,7 +33,7 @@ use ArrayObject;
|
||||
|
||||
/**
|
||||
* Shared memory adapter for the APC extension.
|
||||
*
|
||||
*
|
||||
* @category Caching
|
||||
* @package PEAR2_Cache_SHM
|
||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||
@ -42,32 +43,39 @@ use ArrayObject;
|
||||
class APC extends SHM
|
||||
{
|
||||
/**
|
||||
* @var string ID of the current storage.
|
||||
* ID of the current storage.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $persistentId;
|
||||
|
||||
|
||||
/**
|
||||
* List of persistent IDs.
|
||||
*
|
||||
*
|
||||
* A list of persistent IDs within the current request (as keys) with an int
|
||||
* (as a value) specifying the number of instances in the current request.
|
||||
* Used as an attempt to ensure implicit lock releases even on errors in the
|
||||
* critical sections, since APC doesn't have an actual locking function.
|
||||
* @var array
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $requestInstances = array();
|
||||
|
||||
|
||||
/**
|
||||
* @var array Array of lock names (as values) for each persistent ID (as
|
||||
* key) obtained during the current request.
|
||||
* Array of lock names for each persistent ID.
|
||||
*
|
||||
* Array of lock names (as values) for each persistent ID (as key) obtained
|
||||
* during the current request.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $locksBackup = array();
|
||||
|
||||
/**
|
||||
* Creates a new shared memory storage.
|
||||
*
|
||||
* Estabilishes a separate persistent storage.
|
||||
*
|
||||
*
|
||||
* Establishes a separate persistent storage.
|
||||
*
|
||||
* @param string $persistentId The ID for the storage. The storage will be
|
||||
* reused if it exists, or created if it doesn't exist. Data and locks
|
||||
* are namespaced by this ID.
|
||||
@ -87,35 +95,36 @@ class APC extends SHM
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks if the adapter meets its requirements.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public static function isMeetingRequirements()
|
||||
{
|
||||
return extension_loaded('apc')
|
||||
&& version_compare(phpversion('apc'), '3.0.13', '>=')
|
||||
&& version_compare(phpversion('apc'), '3.1.1', '>=')
|
||||
&& ini_get('apc.enabled')
|
||||
&& ('cli' !== PHP_SAPI || ini_get('apc.enable_cli'));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Releases all locks in a storage.
|
||||
*
|
||||
*
|
||||
* This function is not meant to be used directly. It is implicitly called
|
||||
* by the the destructor and as a shutdown function when the request ends.
|
||||
* One of these calls ends up releasing any unreleased locks obtained
|
||||
* during the request. A lock is also implicitly released as soon as there
|
||||
* are no objects left in the current request using the same persistent ID.
|
||||
*
|
||||
*
|
||||
* @param string $internalPersistentId The internal persistent ID, the locks
|
||||
* of which are being released.
|
||||
* @param bool $isAtShutdown Whether the function was executed at
|
||||
* shutdown.
|
||||
*
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public static function releaseLocks($internalPersistentId, $isAtShutdown)
|
||||
@ -127,7 +136,7 @@ class APC extends SHM
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Releases any locks obtained by this instance as soon as there are no more
|
||||
* references to the object's persistent ID.
|
||||
@ -137,18 +146,18 @@ class APC extends SHM
|
||||
static::$requestInstances[$this->persistentId]--;
|
||||
static::releaseLocks($this->persistentId, false);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Obtains a named lock.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of the key to obtain. Note that $key may
|
||||
* repeat for each distinct $persistentId.
|
||||
* @param double $timeout If the lock can't be immediatly obtained, the
|
||||
* @param double $timeout If the lock can't be immediately obtained, the
|
||||
* script will block for at most the specified amount of seconds.
|
||||
* Setting this to 0 makes lock obtaining non blocking, and setting it
|
||||
* to NULL makes it block without a time limit.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function lock($key, $timeout = null)
|
||||
@ -164,13 +173,13 @@ class APC extends SHM
|
||||
static::$locksBackup[$this->persistentId] = $key;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Releases a named lock.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of the key to release. Note that $key may
|
||||
* repeat for each distinct $persistentId.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function unlock($key)
|
||||
@ -178,69 +187,71 @@ class APC extends SHM
|
||||
$lock = $this->persistentId . 'l ' . $key;
|
||||
$success = apc_delete($lock);
|
||||
if ($success) {
|
||||
unset(static::$locksBackup[$this->persistentId][array_search(
|
||||
$key,
|
||||
static::$locksBackup[$this->persistentId],
|
||||
true
|
||||
)]);
|
||||
unset(
|
||||
static::$locksBackup[$this->persistentId][array_search(
|
||||
$key,
|
||||
static::$locksBackup[$this->persistentId],
|
||||
true
|
||||
)]
|
||||
);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks if a specified key is in the storage.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of key to check.
|
||||
*
|
||||
* @return bool TRUE if the key is in the storage, FALSE otherwise.
|
||||
*
|
||||
* @return bool TRUE if the key is in the storage, FALSE otherwise.
|
||||
*/
|
||||
public function exists($key)
|
||||
{
|
||||
return apc_exists($this->persistentId . 'd ' . $key);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Adds a value to the shared memory storage.
|
||||
*
|
||||
*
|
||||
* Adds a value to the storage if it doesn't exist, or fails if it does.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of key to associate the value with.
|
||||
* @param mixed $value Value for the specified key.
|
||||
* @param int $ttl Seconds to store the value. If set to 0 indicates no
|
||||
* time limit.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function add($key, $value, $ttl = 0)
|
||||
{
|
||||
return apc_add($this->persistentId . 'd ' . $key, $value, $ttl);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets a value in the shared memory storage.
|
||||
*
|
||||
*
|
||||
* Adds a value to the storage if it doesn't exist, overwrites it otherwise.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of key to associate the value with.
|
||||
* @param mixed $value Value for the specified key.
|
||||
* @param int $ttl Seconds to store the value. If set to 0 indicates no
|
||||
* time limit.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function set($key, $value, $ttl = 0)
|
||||
{
|
||||
return apc_store($this->persistentId . 'd ' . $key, $value, $ttl);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets a value from the shared memory storage.
|
||||
*
|
||||
*
|
||||
* Gets the current value, or throws an exception if it's not stored.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of key to get the value of.
|
||||
*
|
||||
*
|
||||
* @return mixed The current value of the specified key.
|
||||
*/
|
||||
public function get($key)
|
||||
@ -260,29 +271,29 @@ class APC extends SHM
|
||||
}
|
||||
throw new SHM\InvalidArgumentException('No such key in cache', 101);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Deletes a value from the shared memory storage.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of key to delete.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function delete($key)
|
||||
{
|
||||
return apc_delete($this->persistentId . 'd ' . $key);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Increases a value from the shared memory storage.
|
||||
*
|
||||
*
|
||||
* Increases a value from the shared memory storage. Unlike a plain
|
||||
* set($key, get($key)+$step) combination, this function also implicitly
|
||||
* performs locking.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of key to increase.
|
||||
* @param int $step Value to increase the key by.
|
||||
*
|
||||
*
|
||||
* @return int The new value.
|
||||
*/
|
||||
public function inc($key, $step = 1)
|
||||
@ -300,17 +311,17 @@ class APC extends SHM
|
||||
}
|
||||
return $newValue;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Decreases a value from the shared memory storage.
|
||||
*
|
||||
*
|
||||
* Decreases a value from the shared memory storage. Unlike a plain
|
||||
* set($key, get($key)-$step) combination, this function also implicitly
|
||||
* performs locking.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of key to decrease.
|
||||
* @param int $step Value to decrease the key by.
|
||||
*
|
||||
*
|
||||
* @return int The new value.
|
||||
*/
|
||||
public function dec($key, $step = 1)
|
||||
@ -331,27 +342,27 @@ class APC extends SHM
|
||||
|
||||
/**
|
||||
* Sets a new value if a key has a certain value.
|
||||
*
|
||||
*
|
||||
* Sets a new value if a key has a certain value. This function only works
|
||||
* when $old and $new are longs.
|
||||
*
|
||||
*
|
||||
* @param string $key Key of the value to compare and set.
|
||||
* @param int $old The value to compare the key against.
|
||||
* @param int $new The value to set the key to.
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function cas($key, $old, $new)
|
||||
{
|
||||
return apc_cas($this->persistentId . 'd ' . $key, $old, $new);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Clears the persistent storage.
|
||||
*
|
||||
*
|
||||
* Clears the persistent storage, i.e. removes all keys. Locks are left
|
||||
* intact.
|
||||
*
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function clear()
|
||||
@ -366,18 +377,18 @@ class APC extends SHM
|
||||
apc_delete($key);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve an external iterator
|
||||
*
|
||||
*
|
||||
* Returns an external iterator.
|
||||
*
|
||||
*
|
||||
* @param string|null $filter A PCRE regular expression.
|
||||
* Only matching keys will be iterated over.
|
||||
* Setting this to NULL matches all keys of this instance.
|
||||
* @param bool $keysOnly Whether to return only the keys,
|
||||
* or return both the keys and values.
|
||||
*
|
||||
*
|
||||
* @return ArrayObject An array with all matching keys as array keys,
|
||||
* and values as array values. If $keysOnly is TRUE, the array keys are
|
||||
* numeric, and the array values are key names.
|
||||
|
416
system/autoload/PEAR2/Cache/SHM/Adapter/APCu.php
Normal file
416
system/autoload/PEAR2/Cache/SHM/Adapter/APCu.php
Normal file
@ -0,0 +1,416 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Wrapper for shared memory and locking functionality across different extensions.
|
||||
|
||||
*
|
||||
* Allows you to share data across requests as long as the PHP process is running. One of APC or WinCache is required to accomplish this, with other extensions being potentially pluggable as adapters.
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* @category Caching
|
||||
* @package PEAR2_Cache_SHM
|
||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||
* @copyright 2011 Vasil Rangelov
|
||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||
* @version 0.2.0
|
||||
* @link http://pear2.php.net/PEAR2_Cache_SHM
|
||||
*/
|
||||
/**
|
||||
* The namespace declaration.
|
||||
*/
|
||||
namespace PEAR2\Cache\SHM\Adapter;
|
||||
|
||||
/**
|
||||
* Throws exceptions from this namespace, and extends from this class.
|
||||
*/
|
||||
use PEAR2\Cache\SHM;
|
||||
|
||||
/**
|
||||
* {@link APC::getIterator()} returns this object.
|
||||
*/
|
||||
use ArrayObject;
|
||||
|
||||
/**
|
||||
* Shared memory adapter for the APC extension.
|
||||
*
|
||||
* @category Caching
|
||||
* @package PEAR2_Cache_SHM
|
||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||
* @link http://pear2.php.net/PEAR2_Cache_SHM
|
||||
*/
|
||||
class APCu extends SHM
|
||||
{
|
||||
/**
|
||||
* ID of the current storage.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $persistentId;
|
||||
|
||||
/**
|
||||
* List of persistent IDs.
|
||||
*
|
||||
* A list of persistent IDs within the current request (as keys) with an int
|
||||
* (as a value) specifying the number of instances in the current request.
|
||||
* Used as an attempt to ensure implicit lock releases even on errors in the
|
||||
* critical sections, since APC doesn't have an actual locking function.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $requestInstances = array();
|
||||
|
||||
/**
|
||||
* Array of lock names for each persistent ID.
|
||||
*
|
||||
* Array of lock names (as values) for each persistent ID (as key) obtained
|
||||
* during the current request.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $locksBackup = array();
|
||||
|
||||
/**
|
||||
* Creates a new shared memory storage.
|
||||
*
|
||||
* Establishes a separate persistent storage.
|
||||
*
|
||||
* @param string $persistentId The ID for the storage. The storage will be
|
||||
* reused if it exists, or created if it doesn't exist. Data and locks
|
||||
* are namespaced by this ID.
|
||||
*/
|
||||
public function __construct($persistentId)
|
||||
{
|
||||
$this->persistentId = __CLASS__ . ' ' . $persistentId;
|
||||
if (isset(static::$requestInstances[$this->persistentId])) {
|
||||
static::$requestInstances[$this->persistentId]++;
|
||||
} else {
|
||||
static::$requestInstances[$this->persistentId] = 1;
|
||||
static::$locksBackup[$this->persistentId] = array();
|
||||
}
|
||||
register_shutdown_function(
|
||||
get_called_class() . '::releaseLocks',
|
||||
$this->persistentId,
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the adapter meets its requirements.
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public static function isMeetingRequirements()
|
||||
{
|
||||
return extension_loaded('apcu')
|
||||
&& version_compare(phpversion('apcu'), '5.0.0', '>=')
|
||||
&& ini_get('apc.enabled')
|
||||
&& ('cli' !== PHP_SAPI || ini_get('apc.enable_cli'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Releases all locks in a storage.
|
||||
*
|
||||
* This function is not meant to be used directly. It is implicitly called
|
||||
* by the the destructor and as a shutdown function when the request ends.
|
||||
* One of these calls ends up releasing any unreleased locks obtained
|
||||
* during the request. A lock is also implicitly released as soon as there
|
||||
* are no objects left in the current request using the same persistent ID.
|
||||
*
|
||||
* @param string $internalPersistentId The internal persistent ID, the locks
|
||||
* of which are being released.
|
||||
* @param bool $isAtShutdown Whether the function was executed at
|
||||
* shutdown.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public static function releaseLocks($internalPersistentId, $isAtShutdown)
|
||||
{
|
||||
$hasInstances = 0 !== static::$requestInstances[$internalPersistentId];
|
||||
if ($isAtShutdown === $hasInstances) {
|
||||
foreach (static::$locksBackup[$internalPersistentId] as $key) {
|
||||
apcu_delete($internalPersistentId . 'l ' . $key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Releases any locks obtained by this instance as soon as there are no more
|
||||
* references to the object's persistent ID.
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
static::$requestInstances[$this->persistentId]--;
|
||||
static::releaseLocks($this->persistentId, false);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Obtains a named lock.
|
||||
*
|
||||
* @param string $key Name of the key to obtain. Note that $key may
|
||||
* repeat for each distinct $persistentId.
|
||||
* @param double $timeout If the lock can't be immediately obtained, the
|
||||
* script will block for at most the specified amount of seconds.
|
||||
* Setting this to 0 makes lock obtaining non blocking, and setting it
|
||||
* to NULL makes it block without a time limit.
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function lock($key, $timeout = null)
|
||||
{
|
||||
$lock = $this->persistentId . 'l ' . $key;
|
||||
$hasTimeout = $timeout !== null;
|
||||
$start = microtime(true);
|
||||
while (!apcu_add($lock, 1)) {
|
||||
if ($hasTimeout && (microtime(true) - $start) > $timeout) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
static::$locksBackup[$this->persistentId] = $key;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Releases a named lock.
|
||||
*
|
||||
* @param string $key Name of the key to release. Note that $key may
|
||||
* repeat for each distinct $persistentId.
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function unlock($key)
|
||||
{
|
||||
$lock = $this->persistentId . 'l ' . $key;
|
||||
$success = apcu_delete($lock);
|
||||
if ($success) {
|
||||
unset(
|
||||
static::$locksBackup[$this->persistentId][array_search(
|
||||
$key,
|
||||
static::$locksBackup[$this->persistentId],
|
||||
true
|
||||
)]
|
||||
);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a specified key is in the storage.
|
||||
*
|
||||
* @param string $key Name of key to check.
|
||||
*
|
||||
* @return bool TRUE if the key is in the storage, FALSE otherwise.
|
||||
*/
|
||||
public function exists($key)
|
||||
{
|
||||
return apcu_exists($this->persistentId . 'd ' . $key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a value to the shared memory storage.
|
||||
*
|
||||
* Adds a value to the storage if it doesn't exist, or fails if it does.
|
||||
*
|
||||
* @param string $key Name of key to associate the value with.
|
||||
* @param mixed $value Value for the specified key.
|
||||
* @param int $ttl Seconds to store the value. If set to 0 indicates no
|
||||
* time limit.
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function add($key, $value, $ttl = 0)
|
||||
{
|
||||
return apcu_add($this->persistentId . 'd ' . $key, $value, $ttl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a value in the shared memory storage.
|
||||
*
|
||||
* Adds a value to the storage if it doesn't exist, overwrites it otherwise.
|
||||
*
|
||||
* @param string $key Name of key to associate the value with.
|
||||
* @param mixed $value Value for the specified key.
|
||||
* @param int $ttl Seconds to store the value. If set to 0 indicates no
|
||||
* time limit.
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function set($key, $value, $ttl = 0)
|
||||
{
|
||||
return apcu_store($this->persistentId . 'd ' . $key, $value, $ttl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a value from the shared memory storage.
|
||||
*
|
||||
* Gets the current value, or throws an exception if it's not stored.
|
||||
*
|
||||
* @param string $key Name of key to get the value of.
|
||||
*
|
||||
* @return mixed The current value of the specified key.
|
||||
*/
|
||||
public function get($key)
|
||||
{
|
||||
$fullKey = $this->persistentId . 'd ' . $key;
|
||||
if (apcu_exists($fullKey)) {
|
||||
$value = apcu_fetch($fullKey, $success);
|
||||
if (!$success) {
|
||||
throw new SHM\InvalidArgumentException(
|
||||
'Unable to fetch key. ' .
|
||||
'Key has either just now expired or (if no TTL was set) ' .
|
||||
'is possibly in a race condition with another request.',
|
||||
100
|
||||
);
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
throw new SHM\InvalidArgumentException('No such key in cache', 101);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a value from the shared memory storage.
|
||||
*
|
||||
* @param string $key Name of key to delete.
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function delete($key)
|
||||
{
|
||||
return apcu_delete($this->persistentId . 'd ' . $key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Increases a value from the shared memory storage.
|
||||
*
|
||||
* Increases a value from the shared memory storage. Unlike a plain
|
||||
* set($key, get($key)+$step) combination, this function also implicitly
|
||||
* performs locking.
|
||||
*
|
||||
* @param string $key Name of key to increase.
|
||||
* @param int $step Value to increase the key by.
|
||||
*
|
||||
* @return int The new value.
|
||||
*/
|
||||
public function inc($key, $step = 1)
|
||||
{
|
||||
$newValue = apcu_inc(
|
||||
$this->persistentId . 'd ' . $key,
|
||||
(int) $step,
|
||||
$success
|
||||
);
|
||||
if (!$success) {
|
||||
throw new SHM\InvalidArgumentException(
|
||||
'Unable to increase the value. Are you sure the value is int?',
|
||||
102
|
||||
);
|
||||
}
|
||||
return $newValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decreases a value from the shared memory storage.
|
||||
*
|
||||
* Decreases a value from the shared memory storage. Unlike a plain
|
||||
* set($key, get($key)-$step) combination, this function also implicitly
|
||||
* performs locking.
|
||||
*
|
||||
* @param string $key Name of key to decrease.
|
||||
* @param int $step Value to decrease the key by.
|
||||
*
|
||||
* @return int The new value.
|
||||
*/
|
||||
public function dec($key, $step = 1)
|
||||
{
|
||||
$newValue = apcu_dec(
|
||||
$this->persistentId . 'd ' . $key,
|
||||
(int) $step,
|
||||
$success
|
||||
);
|
||||
if (!$success) {
|
||||
throw new SHM\InvalidArgumentException(
|
||||
'Unable to decrease the value. Are you sure the value is int?',
|
||||
103
|
||||
);
|
||||
}
|
||||
return $newValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a new value if a key has a certain value.
|
||||
*
|
||||
* Sets a new value if a key has a certain value. This function only works
|
||||
* when $old and $new are longs.
|
||||
*
|
||||
* @param string $key Key of the value to compare and set.
|
||||
* @param int $old The value to compare the key against.
|
||||
* @param int $new The value to set the key to.
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function cas($key, $old, $new)
|
||||
{
|
||||
return apcu_cas($this->persistentId . 'd ' . $key, $old, $new);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the persistent storage.
|
||||
*
|
||||
* Clears the persistent storage, i.e. removes all keys. Locks are left
|
||||
* intact.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function clear()
|
||||
{
|
||||
foreach (new APCIterator(
|
||||
'user',
|
||||
'/^' . preg_quote($this->persistentId, '/') . 'd /',
|
||||
APC_ITER_KEY,
|
||||
100,
|
||||
APC_LIST_ACTIVE
|
||||
) as $key) {
|
||||
apcu_delete($key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve an external iterator
|
||||
*
|
||||
* Returns an external iterator.
|
||||
*
|
||||
* @param string|null $filter A PCRE regular expression.
|
||||
* Only matching keys will be iterated over.
|
||||
* Setting this to NULL matches all keys of this instance.
|
||||
* @param bool $keysOnly Whether to return only the keys,
|
||||
* or return both the keys and values.
|
||||
*
|
||||
* @return ArrayObject An array with all matching keys as array keys,
|
||||
* and values as array values. If $keysOnly is TRUE, the array keys are
|
||||
* numeric, and the array values are key names.
|
||||
*/
|
||||
public function getIterator($filter = null, $keysOnly = false)
|
||||
{
|
||||
$result = array();
|
||||
foreach (new APCUIterator(
|
||||
'/^' . preg_quote($this->persistentId, '/') . 'd /',
|
||||
APC_ITER_KEY,
|
||||
100,
|
||||
APC_LIST_ACTIVE
|
||||
) as $key) {
|
||||
$localKey = strstr($key, $this->persistentId . 'd ');
|
||||
if (null === $filter || preg_match($filter, $localKey)) {
|
||||
if ($keysOnly) {
|
||||
$result[] = $localKey;
|
||||
} else {
|
||||
$result[$localKey] = apcu_fetch($key);
|
||||
}
|
||||
}
|
||||
}
|
||||
return new ArrayObject($result);
|
||||
}
|
||||
}
|
@ -1,18 +1,19 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* ~~summary~~
|
||||
*
|
||||
* ~~description~~
|
||||
*
|
||||
* Wrapper for shared memory and locking functionality across different extensions.
|
||||
|
||||
*
|
||||
* Allows you to share data across requests as long as the PHP process is running. One of APC or WinCache is required to accomplish this, with other extensions being potentially pluggable as adapters.
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
*
|
||||
* @category Caching
|
||||
* @package PEAR2_Cache_SHM
|
||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||
* @copyright 2011 Vasil Rangelov
|
||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||
* @version 0.1.3
|
||||
* @version 0.2.0
|
||||
* @link http://pear2.php.net/PEAR2_Cache_SHM
|
||||
*/
|
||||
/**
|
||||
@ -31,10 +32,10 @@ use PEAR2\Cache\SHM;
|
||||
use ArrayObject;
|
||||
|
||||
/**
|
||||
* This adapter is not truly persistent. It is intended to emulate persistency
|
||||
* This adapter is not truly persistent. It is intended to emulate persistence
|
||||
* in non persistent environments, so that upper level applications can use a
|
||||
* single code path for persistent and non persistent code.
|
||||
*
|
||||
*
|
||||
* @category Caching
|
||||
* @package PEAR2_Cache_SHM
|
||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||
@ -44,42 +45,50 @@ use ArrayObject;
|
||||
class Placebo extends SHM
|
||||
{
|
||||
/**
|
||||
* @var string ID of the current storage.
|
||||
* ID of the current storage.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $persistentId;
|
||||
|
||||
|
||||
/**
|
||||
* List of persistent IDs.
|
||||
*
|
||||
*
|
||||
* A list of persistent IDs within the current request (as keys) with an int
|
||||
* (as a value) specifying the number of instances in the current request.
|
||||
* Used as an attempt to ensure implicit lock releases on destruction.
|
||||
* @var array
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $requestInstances = array();
|
||||
|
||||
|
||||
/**
|
||||
* @var array Array of lock names (as values) for each persistent ID (as
|
||||
* Array of lock names for each persistent ID.
|
||||
*
|
||||
* Array of lock names (as values) for each persistent ID (as
|
||||
* key) obtained during the current request.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $locksBackup = array();
|
||||
|
||||
|
||||
/**
|
||||
* The data storage.
|
||||
*
|
||||
*
|
||||
* Each persistent ID is a key, and the value is an array.
|
||||
* Each such array has data keys as its keys, and an array as a value.
|
||||
* Each such array has as its elements the value, the timeout and the time
|
||||
* the data was set.
|
||||
* @var array
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $data = array();
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new shared memory storage.
|
||||
*
|
||||
* Estabilishes a separate persistent storage.
|
||||
*
|
||||
*
|
||||
* Establishes a separate persistent storage.
|
||||
*
|
||||
* @param string $persistentId The ID for the storage. The storage will be
|
||||
* reused if it exists, or created if it doesn't exist. Data and locks
|
||||
* are namespaced by this ID.
|
||||
@ -95,9 +104,9 @@ class Placebo extends SHM
|
||||
}
|
||||
$this->persistentId = $persistentId;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Releases any unreleased locks.
|
||||
* Releases any unreleased locks.
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
@ -105,23 +114,23 @@ class Placebo extends SHM
|
||||
static::$locksBackup[$this->persistentId] = array();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks if the adapter meets its requirements.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public static function isMeetingRequirements()
|
||||
{
|
||||
return 'cli' === PHP_SAPI;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Pretends to obtain a lock.
|
||||
*
|
||||
*
|
||||
* @param string $key Ignored.
|
||||
* @param double $timeout Ignored.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function lock($key, $timeout = null)
|
||||
@ -133,12 +142,12 @@ class Placebo extends SHM
|
||||
static::$locksBackup[$this->persistentId][] = $key;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Pretends to release a lock.
|
||||
*
|
||||
*
|
||||
* @param string $key Ignored
|
||||
*
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function unlock($key)
|
||||
@ -147,36 +156,38 @@ class Placebo extends SHM
|
||||
if (!in_array($key, static::$locksBackup[$this->persistentId], true)) {
|
||||
return false;
|
||||
}
|
||||
unset(static::$locksBackup[$this->persistentId][array_search(
|
||||
$key,
|
||||
static::$locksBackup[$this->persistentId],
|
||||
true
|
||||
)]);
|
||||
unset(
|
||||
static::$locksBackup[$this->persistentId][array_search(
|
||||
$key,
|
||||
static::$locksBackup[$this->persistentId],
|
||||
true
|
||||
)]
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks if a specified key is in the storage.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of key to check.
|
||||
*
|
||||
* @return bool TRUE if the key is in the storage, FALSE otherwise.
|
||||
*
|
||||
* @return bool TRUE if the key is in the storage, FALSE otherwise.
|
||||
*/
|
||||
public function exists($key)
|
||||
{
|
||||
return array_key_exists($key, static::$data[$this->persistentId]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Adds a value to the shared memory storage.
|
||||
*
|
||||
*
|
||||
* Adds a value to the storage if it doesn't exist, or fails if it does.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of key to associate the value with.
|
||||
* @param mixed $value Value for the specified key.
|
||||
* @param int $ttl Because "true" adapters purge the cache at the next
|
||||
* request, this setting is ignored.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function add($key, $value, $ttl = 0)
|
||||
@ -186,17 +197,17 @@ class Placebo extends SHM
|
||||
}
|
||||
return $this->set($key, $value, $ttl);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets a value in the shared memory storage.
|
||||
*
|
||||
*
|
||||
* Adds a value to the storage if it doesn't exist, overwrites it otherwise.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of key to associate the value with.
|
||||
* @param mixed $value Value for the specified key.
|
||||
* @param int $ttl Because "true" adapters purge the cache at the next
|
||||
* request, this setting is ignored.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function set($key, $value, $ttl = 0)
|
||||
@ -204,14 +215,14 @@ class Placebo extends SHM
|
||||
static::$data[$this->persistentId][$key] = $value;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets a value from the shared memory storage.
|
||||
*
|
||||
*
|
||||
* Gets the current value, or throws an exception if it's not stored.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of key to get the value of.
|
||||
*
|
||||
*
|
||||
* @return mixed The current value of the specified key.
|
||||
*/
|
||||
public function get($key)
|
||||
@ -224,12 +235,12 @@ class Placebo extends SHM
|
||||
200
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Deletes a value from the shared memory storage.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of key to delete.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function delete($key)
|
||||
@ -240,17 +251,17 @@ class Placebo extends SHM
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Increases a value from the shared memory storage.
|
||||
*
|
||||
*
|
||||
* Increases a value from the shared memory storage. Unlike a plain
|
||||
* set($key, get($key)+$step) combination, this function also implicitly
|
||||
* performs locking.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of key to increase.
|
||||
* @param int $step Value to increase the key by.
|
||||
*
|
||||
*
|
||||
* @return int The new value.
|
||||
*/
|
||||
public function inc($key, $step = 1)
|
||||
@ -265,17 +276,17 @@ class Placebo extends SHM
|
||||
}
|
||||
return $this->get($key);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Decreases a value from the shared memory storage.
|
||||
*
|
||||
*
|
||||
* Decreases a value from the shared memory storage. Unlike a plain
|
||||
* set($key, get($key)-$step) combination, this function also implicitly
|
||||
* performs locking.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of key to decrease.
|
||||
* @param int $step Value to decrease the key by.
|
||||
*
|
||||
*
|
||||
* @return int The new value.
|
||||
*/
|
||||
public function dec($key, $step = 1)
|
||||
@ -293,46 +304,46 @@ class Placebo extends SHM
|
||||
|
||||
/**
|
||||
* Sets a new value if a key has a certain value.
|
||||
*
|
||||
*
|
||||
* Sets a new value if a key has a certain value. This function only works
|
||||
* when $old and $new are longs.
|
||||
*
|
||||
*
|
||||
* @param string $key Key of the value to compare and set.
|
||||
* @param int $old The value to compare the key against.
|
||||
* @param int $new The value to set the key to.
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function cas($key, $old, $new)
|
||||
{
|
||||
return $this->exists($key) && ($this->get($key) === $old)
|
||||
&& is_int($new) && $this->set($key, $new);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Clears the persistent storage.
|
||||
*
|
||||
*
|
||||
* Clears the persistent storage, i.e. removes all keys. Locks are left
|
||||
* intact.
|
||||
*
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function clear()
|
||||
{
|
||||
static::$data[$this->persistentId] = array();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve an external iterator
|
||||
*
|
||||
*
|
||||
* Returns an external iterator.
|
||||
*
|
||||
*
|
||||
* @param string|null $filter A PCRE regular expression.
|
||||
* Only matching keys will be iterated over.
|
||||
* Setting this to NULL matches all keys of this instance.
|
||||
* @param bool $keysOnly Whether to return only the keys,
|
||||
* or return both the keys and values.
|
||||
*
|
||||
*
|
||||
* @return ArrayObject An array with all matching keys as array keys,
|
||||
* and values as array values. If $keysOnly is TRUE, the array keys are
|
||||
* numeric, and the array values are key names.
|
||||
@ -346,7 +357,7 @@ class Placebo extends SHM
|
||||
: static::$data[$this->persistentId]
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
$result = array();
|
||||
foreach (static::$data[$this->persistentId] as $key => $value) {
|
||||
if (preg_match($filter, $key)) {
|
||||
|
@ -1,18 +1,19 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* ~~summary~~
|
||||
*
|
||||
* ~~description~~
|
||||
*
|
||||
* Wrapper for shared memory and locking functionality across different extensions.
|
||||
|
||||
*
|
||||
* Allows you to share data across requests as long as the PHP process is running. One of APC or WinCache is required to accomplish this, with other extensions being potentially pluggable as adapters.
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
*
|
||||
* @category Caching
|
||||
* @package PEAR2_Cache_SHM
|
||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||
* @copyright 2011 Vasil Rangelov
|
||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||
* @version 0.1.3
|
||||
* @version 0.2.0
|
||||
* @link http://pear2.php.net/PEAR2_Cache_SHM
|
||||
*/
|
||||
/**
|
||||
@ -32,7 +33,7 @@ use ArrayObject;
|
||||
|
||||
/**
|
||||
* Shared memory adapter for the WinCache extension.
|
||||
*
|
||||
*
|
||||
* @category Caching
|
||||
* @package PEAR2_Cache_SHM
|
||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||
@ -42,30 +43,35 @@ use ArrayObject;
|
||||
class Wincache extends SHM
|
||||
{
|
||||
/**
|
||||
* @var string ID of the current storage.
|
||||
* ID of the current storage.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $persistentId;
|
||||
|
||||
|
||||
/**
|
||||
* List of persistent IDs.
|
||||
*
|
||||
*
|
||||
* A list of persistent IDs within the current request (as keys) with an int
|
||||
* (as a value) specifying the number of instances in the current request.
|
||||
* Used as an attempt to ensure implicit lock releases on destruction.
|
||||
* @var array
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $requestInstances = array();
|
||||
|
||||
|
||||
/**
|
||||
* @var array Array of lock names obtained during the current request.
|
||||
* Array of lock names obtained during the current request.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $locksBackup = array();
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new shared memory storage.
|
||||
*
|
||||
* Estabilishes a separate persistent storage.
|
||||
*
|
||||
*
|
||||
* Establishes a separate persistent storage.
|
||||
*
|
||||
* @param string $persistentId The ID for the storage. The storage will be
|
||||
* reused if it exists, or created if it doesn't exist. Data and locks
|
||||
* are namespaced by this ID.
|
||||
@ -81,28 +87,29 @@ class Wincache extends SHM
|
||||
static::$locksBackup[$this->persistentId] = array();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Encodes a lock name
|
||||
*
|
||||
*
|
||||
* Encodes a lock name, so that it can be properly obtained. The scheme used
|
||||
* is a subset of URL encoding, with only the "%" and "\" characters being
|
||||
* escaped. The encoding itself is necessary, since lock names can't contain
|
||||
* the "\" character.
|
||||
*
|
||||
*
|
||||
* @param string $name The lock name to encode.
|
||||
*
|
||||
*
|
||||
* @return string The encoded name.
|
||||
*
|
||||
* @link http://msdn.microsoft.com/en-us/library/ms682411(VS.85).aspx
|
||||
*/
|
||||
protected static function encodeLockName($name)
|
||||
{
|
||||
return str_replace(array('%', '\\'), array('%25', '%5C'), $name);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks if the adapter meets its requirements.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public static function isMeetingRequirements()
|
||||
@ -112,7 +119,7 @@ class Wincache extends SHM
|
||||
&& ini_get('wincache.ucenabled')
|
||||
&& ('cli' !== PHP_SAPI || ini_get('wincache.enablecli'));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Releases any locks obtained by this instance as soon as there are no more
|
||||
* references to the object's persistent ID.
|
||||
@ -128,15 +135,15 @@ class Wincache extends SHM
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Obtains a named lock.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of the key to obtain. Note that $key may
|
||||
* repeat for each distinct $persistentId.
|
||||
* @param double $timeout Ignored with WinCache. Script will always block if
|
||||
* the lock can't be immediatly obtained.
|
||||
*
|
||||
* the lock can't be immediately obtained.
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function lock($key, $timeout = null)
|
||||
@ -149,13 +156,13 @@ class Wincache extends SHM
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Releases a named lock.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of the key to release. Note that $key may
|
||||
* repeat for each distinct $persistentId.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function unlock($key)
|
||||
@ -164,68 +171,70 @@ class Wincache extends SHM
|
||||
$this->persistentId . static::encodeLockName($key)
|
||||
);
|
||||
if ($result) {
|
||||
unset(static::$locksBackup[$this->persistentId][array_search(
|
||||
$key,
|
||||
static::$locksBackup[$this->persistentId],
|
||||
true
|
||||
)]);
|
||||
unset(
|
||||
static::$locksBackup[$this->persistentId][array_search(
|
||||
$key,
|
||||
static::$locksBackup[$this->persistentId],
|
||||
true
|
||||
)]
|
||||
);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks if a specified key is in the storage.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of key to check.
|
||||
*
|
||||
* @return bool TRUE if the key is in the storage, FALSE otherwise.
|
||||
*
|
||||
* @return bool TRUE if the key is in the storage, FALSE otherwise.
|
||||
*/
|
||||
public function exists($key)
|
||||
{
|
||||
return wincache_ucache_exists($this->persistentId . $key);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Adds a value to the shared memory storage.
|
||||
*
|
||||
*
|
||||
* Sets a value to the storage if it doesn't exist, or fails if it does.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of key to associate the value with.
|
||||
* @param mixed $value Value for the specified key.
|
||||
* @param int $ttl Seconds to store the value. If set to 0 indicates no
|
||||
* time limit.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function add($key, $value, $ttl = 0)
|
||||
{
|
||||
return wincache_ucache_add($this->persistentId . $key, $value, $ttl);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets a value in the shared memory storage.
|
||||
*
|
||||
*
|
||||
* Adds a value to the storage if it doesn't exist, overwrites it otherwise.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of key to associate the value with.
|
||||
* @param mixed $value Value for the specified key.
|
||||
* @param int $ttl Seconds to store the value. If set to 0 indicates no
|
||||
* time limit.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function set($key, $value, $ttl = 0)
|
||||
{
|
||||
return wincache_ucache_set($this->persistentId . $key, $value, $ttl);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets a value from the shared memory storage.
|
||||
*
|
||||
*
|
||||
* Gets the current value, or throws an exception if it's not stored.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of key to get the value of.
|
||||
*
|
||||
*
|
||||
* @return mixed The current value of the specified key.
|
||||
*/
|
||||
public function get($key)
|
||||
@ -239,29 +248,29 @@ class Wincache extends SHM
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Deletes a value from the shared memory storage.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of key to delete.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function delete($key)
|
||||
{
|
||||
return wincache_ucache_delete($this->persistentId . $key);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Increases a value from the shared memory storage.
|
||||
*
|
||||
*
|
||||
* Increases a value from the shared memory storage. Unlike a plain
|
||||
* set($key, get($key)+$step) combination, this function also implicitly
|
||||
* performs locking.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of key to increase.
|
||||
* @param int $step Value to increase the key by.
|
||||
*
|
||||
*
|
||||
* @return int The new value.
|
||||
*/
|
||||
public function inc($key, $step = 1)
|
||||
@ -279,17 +288,17 @@ class Wincache extends SHM
|
||||
}
|
||||
return $newValue;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Decreases a value from the shared memory storage.
|
||||
*
|
||||
*
|
||||
* Decreases a value from the shared memory storage. Unlike a plain
|
||||
* set($key, get($key)-$step) combination, this function also implicitly
|
||||
* performs locking.
|
||||
*
|
||||
*
|
||||
* @param string $key Name of key to decrease.
|
||||
* @param int $step Value to decrease the key by.
|
||||
*
|
||||
*
|
||||
* @return int The new value.
|
||||
*/
|
||||
public function dec($key, $step = 1)
|
||||
@ -310,27 +319,27 @@ class Wincache extends SHM
|
||||
|
||||
/**
|
||||
* Sets a new value if a key has a certain value.
|
||||
*
|
||||
*
|
||||
* Sets a new value if a key has a certain value. This function only works
|
||||
* when $old and $new are longs.
|
||||
*
|
||||
*
|
||||
* @param string $key Key of the value to compare and set.
|
||||
* @param int $old The value to compare the key against.
|
||||
* @param int $new The value to set the key to.
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function cas($key, $old, $new)
|
||||
{
|
||||
return wincache_ucache_cas($this->persistentId . $key, $old, $new);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Clears the persistent storage.
|
||||
*
|
||||
*
|
||||
* Clears the persistent storage, i.e. removes all keys. Locks are left
|
||||
* intact.
|
||||
*
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function clear()
|
||||
@ -344,18 +353,18 @@ class Wincache extends SHM
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve an external iterator
|
||||
*
|
||||
*
|
||||
* Returns an external iterator.
|
||||
*
|
||||
*
|
||||
* @param string|null $filter A PCRE regular expression.
|
||||
* Only matching keys will be iterated over.
|
||||
* Setting this to NULL matches all keys of this instance.
|
||||
* @param bool $keysOnly Whether to return only the keys,
|
||||
* or return both the keys and values.
|
||||
*
|
||||
*
|
||||
* @return ArrayObject An array with all matching keys as array keys,
|
||||
* and values as array values. If $keysOnly is TRUE, the array keys are
|
||||
* numeric, and the array values are key names.
|
||||
|
@ -1,9 +1,10 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* ~~summary~~
|
||||
* Wrapper for shared memory and locking functionality across different extensions.
|
||||
|
||||
*
|
||||
* ~~description~~
|
||||
* Allows you to share data across requests as long as the PHP process is running. One of APC or WinCache is required to accomplish this, with other extensions being potentially pluggable as adapters.
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
@ -12,7 +13,7 @@
|
||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||
* @copyright 2011 Vasil Rangelov
|
||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||
* @version 0.1.3
|
||||
* @version 0.2.0
|
||||
* @link http://pear2.php.net/PEAR2_Cache_SHM
|
||||
*/
|
||||
/**
|
||||
|
@ -1,9 +1,10 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* ~~summary~~
|
||||
* Wrapper for shared memory and locking functionality across different extensions.
|
||||
|
||||
*
|
||||
* ~~description~~
|
||||
* Allows you to share data across requests as long as the PHP process is running. One of APC or WinCache is required to accomplish this, with other extensions being potentially pluggable as adapters.
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
@ -12,7 +13,7 @@
|
||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||
* @copyright 2011 Vasil Rangelov
|
||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||
* @version 0.1.3
|
||||
* @version 0.2.0
|
||||
* @link http://pear2.php.net/PEAR2_Cache_SHM
|
||||
*/
|
||||
/**
|
||||
|
Reference in New Issue
Block a user