2009-10-23 02:19:17 +04:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
|
|
|
* <http://www.doctrine-project.org>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Doctrine\Common\Cache;
|
|
|
|
|
|
|
|
/**
|
2009-10-23 19:03:00 +04:00
|
|
|
* Base class for cache driver implementations.
|
2009-10-23 02:19:17 +04:00
|
|
|
*
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @link www.doctrine-project.org
|
|
|
|
* @since 2.0
|
|
|
|
* @version $Revision: 3938 $
|
|
|
|
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
|
|
|
* @author Jonathan Wage <jonwage@gmail.com>
|
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
|
|
|
*/
|
|
|
|
abstract class AbstractCache implements Cache
|
|
|
|
{
|
2009-10-23 19:03:00 +04:00
|
|
|
/** @var string The cache id to store the index of cache ids under */
|
2009-10-23 02:19:17 +04:00
|
|
|
private $_cacheIdsIndexId = 'doctrine_cache_ids';
|
|
|
|
|
2009-10-23 19:03:00 +04:00
|
|
|
/** @var string The namespace to prefix all cache ids with */
|
2009-10-23 02:19:17 +04:00
|
|
|
private $_namespace = null;
|
|
|
|
|
|
|
|
/**
|
2009-10-23 19:03:00 +04:00
|
|
|
* Set the namespace to prefix all cache ids with.
|
2009-10-23 02:19:17 +04:00
|
|
|
*
|
|
|
|
* @param string $namespace
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setNamespace($namespace)
|
|
|
|
{
|
|
|
|
$this->_namespace = $namespace;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function fetch($id)
|
|
|
|
{
|
2009-10-23 19:03:00 +04:00
|
|
|
return $this->_doFetch($this->_getNamespacedId($id));
|
2009-10-23 02:19:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function contains($id)
|
|
|
|
{
|
2009-10-23 19:03:00 +04:00
|
|
|
return $this->_doContains($this->_getNamespacedId($id));
|
2009-10-23 02:19:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function save($id, $data, $lifeTime = false)
|
|
|
|
{
|
|
|
|
$id = $this->_getNamespacedId($id);
|
2010-01-29 04:38:37 +03:00
|
|
|
return $this->_doSave($id, $data, $lifeTime);
|
2009-10-23 02:19:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function delete($id)
|
|
|
|
{
|
|
|
|
$id = $this->_getNamespacedId($id);
|
|
|
|
|
|
|
|
if (strpos($id, '*') !== false) {
|
|
|
|
return $this->deleteByRegex('/' . str_replace('*', '.*', $id) . '/');
|
|
|
|
}
|
|
|
|
|
2010-01-29 04:38:37 +03:00
|
|
|
return $this->_doDelete($id);
|
2009-10-23 02:19:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete all cache entries.
|
|
|
|
*
|
|
|
|
* @return array $deleted Array of the deleted cache ids
|
|
|
|
*/
|
|
|
|
public function deleteAll()
|
|
|
|
{
|
|
|
|
$ids = $this->getIds();
|
|
|
|
foreach ($ids as $id) {
|
|
|
|
$this->delete($id);
|
|
|
|
}
|
|
|
|
return $ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete cache entries where the id matches a PHP regular expressions
|
|
|
|
*
|
|
|
|
* @param string $regex
|
|
|
|
* @return array $deleted Array of the deleted cache ids
|
|
|
|
*/
|
|
|
|
public function deleteByRegex($regex)
|
|
|
|
{
|
|
|
|
$deleted = array();
|
|
|
|
$ids = $this->getIds();
|
|
|
|
foreach ($ids as $id) {
|
|
|
|
if (preg_match($regex, $id)) {
|
|
|
|
$this->delete($id);
|
|
|
|
$deleted[] = $id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $deleted;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete cache entries where the id has the passed prefix
|
|
|
|
*
|
|
|
|
* @param string $prefix
|
|
|
|
* @return array $deleted Array of the deleted cache ids
|
|
|
|
*/
|
|
|
|
public function deleteByPrefix($prefix)
|
|
|
|
{
|
|
|
|
$deleted = array();
|
|
|
|
$ids = $this->getIds();
|
|
|
|
foreach ($ids as $id) {
|
2010-01-29 04:38:37 +03:00
|
|
|
if (strpos($id, $prefix) === 0) {
|
2009-10-23 02:19:17 +04:00
|
|
|
$this->delete($id);
|
|
|
|
$deleted[] = $id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $deleted;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete cache entries where the id has the passed suffix
|
|
|
|
*
|
2010-01-29 04:38:37 +03:00
|
|
|
* @param string $suffix
|
2009-10-23 02:19:17 +04:00
|
|
|
* @return array $deleted Array of the deleted cache ids
|
|
|
|
*/
|
|
|
|
public function deleteBySuffix($suffix)
|
|
|
|
{
|
|
|
|
$deleted = array();
|
|
|
|
$ids = $this->getIds();
|
|
|
|
foreach ($ids as $id) {
|
2010-01-29 04:38:37 +03:00
|
|
|
if (substr($id, -1 * strlen($suffix)) === $suffix) {
|
2009-10-23 02:19:17 +04:00
|
|
|
$this->delete($id);
|
|
|
|
$deleted[] = $id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $deleted;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prefix the passed id with the configured namespace value
|
|
|
|
*
|
|
|
|
* @param string $id The id to namespace
|
|
|
|
* @return string $id The namespaced id
|
|
|
|
*/
|
|
|
|
private function _getNamespacedId($id)
|
|
|
|
{
|
|
|
|
if ( ! $this->_namespace || strpos($id, $this->_namespace) === 0) {
|
|
|
|
return $id;
|
|
|
|
} else {
|
|
|
|
return $this->_namespace . $id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches an entry from the cache.
|
2010-01-29 04:38:37 +03:00
|
|
|
*
|
2009-10-23 02:19:17 +04:00
|
|
|
* @param string $id cache id The id of the cache entry to fetch.
|
|
|
|
* @return string The cached data or FALSE, if no cache entry exists for the given id.
|
|
|
|
*/
|
|
|
|
abstract protected function _doFetch($id);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if an entry exists in the cache.
|
|
|
|
*
|
|
|
|
* @param string $id cache id The cache id of the entry to check for.
|
|
|
|
* @return boolean TRUE if a cache entry exists for the given cache id, FALSE otherwise.
|
|
|
|
*/
|
|
|
|
abstract protected function _doContains($id);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Puts data into the cache.
|
|
|
|
*
|
|
|
|
* @param string $id The cache id.
|
|
|
|
* @param string $data The cache entry/data.
|
|
|
|
* @param int $lifeTime The lifetime. If != false, sets a specific lifetime for this cache entry (null => infinite lifeTime).
|
|
|
|
* @return boolean TRUE if the entry was successfully stored in the cache, FALSE otherwise.
|
|
|
|
*/
|
|
|
|
abstract protected function _doSave($id, $data, $lifeTime = false);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a cache entry.
|
2010-01-29 04:38:37 +03:00
|
|
|
*
|
2009-10-23 02:19:17 +04:00
|
|
|
* @param string $id cache id
|
|
|
|
* @return boolean TRUE if the cache entry was successfully deleted, FALSE otherwise.
|
|
|
|
*/
|
|
|
|
abstract protected function _doDelete($id);
|
2010-01-29 04:38:37 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an array of all the cache ids stored
|
|
|
|
*
|
|
|
|
* @return array $ids
|
|
|
|
*/
|
|
|
|
abstract public function getIds();
|
2009-10-23 02:19:17 +04:00
|
|
|
}
|