.gitignore000064400000000010144760125330006530 0ustar00vendor/ .travis.yml000064400000000141144760125330006656 0ustar00language: php php: - 5.3 - 5.4 before_script: - composer --prefer-source --dev install LICENSE000064400000002051144760125330005554 0ustar00Copyright (c) 2006-2012 Doctrine Project 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. README.md000064400000000116144760125330006026 0ustar00# Doctrine Cache Cache component extracted from the Doctrine Common project. composer.json000064400000001400144760125330007266 0ustar00{ "name": "doctrine/cache", "type": "library", "description": "Caching library offering an object-oriented API for many cache backends", "keywords": ["cache", "caching"], "homepage": "http://www.doctrine-project.org", "license": "MIT", "authors": [ {"name": "Guilherme Blanco", "email": "guilhermeblanco@gmail.com"}, {"name": "Roman Borschel", "email": "roman@code-factory.org"}, {"name": "Benjamin Eberlei", "email": "kontakt@beberlei.de"}, {"name": "Jonathan Wage", "email": "jonwage@gmail.com"}, {"name": "Johannes Schmitt", "email": "schmittjoh@gmail.com"} ], "require": { "php": ">=5.3.2" }, "autoload": { "psr-0": { "Doctrine\\Common\\Cache\\": "lib/" } } } lib/Doctrine/Common/Cache/ApcCache.php000064400000005147144760125330013470 0ustar00. */ namespace Doctrine\Common\Cache; /** * APC cache provider. * * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @link www.doctrine-project.org * @since 2.0 * @author Benjamin Eberlei * @author Guilherme Blanco * @author Jonathan Wage * @author Roman Borschel * @author David Abdemoulaie */ class ApcCache extends CacheProvider { /** * {@inheritdoc} */ protected function doFetch($id) { return apc_fetch($id); } /** * {@inheritdoc} */ protected function doContains($id) { return apc_exists($id); } /** * {@inheritdoc} */ protected function doSave($id, $data, $lifeTime = 0) { return (bool) apc_store($id, $data, (int) $lifeTime); } /** * {@inheritdoc} */ protected function doDelete($id) { return apc_delete($id); } /** * {@inheritdoc} */ protected function doFlush() { return apc_clear_cache() && apc_clear_cache('user'); } /** * {@inheritdoc} */ protected function doGetStats() { $info = apc_cache_info(); $sma = apc_sma_info(); return array( Cache::STATS_HITS => $info['num_hits'], Cache::STATS_MISSES => $info['num_misses'], Cache::STATS_UPTIME => $info['start_time'], Cache::STATS_MEMORY_USAGE => $info['mem_size'], Cache::STATS_MEMORY_AVAILIABLE => $sma['avail_mem'], ); } } lib/Doctrine/Common/Cache/ArrayCache.php000064400000004531144760125330014037 0ustar00. */ namespace Doctrine\Common\Cache; /** * Array cache driver. * * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @link www.doctrine-project.org * @since 2.0 * @author Benjamin Eberlei * @author Guilherme Blanco * @author Jonathan Wage * @author Roman Borschel * @author David Abdemoulaie */ class ArrayCache extends CacheProvider { /** * @var array $data */ private $data = array(); /** * {@inheritdoc} */ protected function doFetch($id) { return (isset($this->data[$id])) ? $this->data[$id] : false; } /** * {@inheritdoc} */ protected function doContains($id) { return isset($this->data[$id]); } /** * {@inheritdoc} */ protected function doSave($id, $data, $lifeTime = 0) { $this->data[$id] = $data; return true; } /** * {@inheritdoc} */ protected function doDelete($id) { unset($this->data[$id]); return true; } /** * {@inheritdoc} */ protected function doFlush() { $this->data = array(); return true; } /** * {@inheritdoc} */ protected function doGetStats() { return null; } } lib/Doctrine/Common/Cache/Cache.php000064400000006722144760125330013044 0ustar00. */ namespace Doctrine\Common\Cache; /** * Interface for cache drivers. * * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @link www.doctrine-project.org * @since 2.0 * @author Benjamin Eberlei * @author Guilherme Blanco * @author Jonathan Wage * @author Roman Borschel * @author Fabio B. Silva */ interface Cache { const STATS_HITS = 'hits'; const STATS_MISSES = 'misses'; const STATS_UPTIME = 'uptime'; const STATS_MEMORY_USAGE = 'memory_usage'; const STATS_MEMORY_AVAILIABLE = 'memory_available'; /** * Fetches an entry from the cache. * * @param string $id cache id The id of the cache entry to fetch. * @return mixed The cached data or FALSE, if no cache entry exists for the given id. */ function fetch($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. */ function contains($id); /** * Puts data into the cache. * * @param string $id The cache id. * @param mixed $data The cache entry/data. * @param int $lifeTime The lifetime. If != 0, sets a specific lifetime for this cache entry (0 => infinite lifeTime). * @return boolean TRUE if the entry was successfully stored in the cache, FALSE otherwise. */ function save($id, $data, $lifeTime = 0); /** * Deletes a cache entry. * * @param string $id cache id * @return boolean TRUE if the cache entry was successfully deleted, FALSE otherwise. */ function delete($id); /** * Retrieves cached information from data store * * The server's statistics array has the following values: * * - hits * Number of keys that have been requested and found present. * * - misses * Number of items that have been requested and not found. * * - uptime * Time that the server is running. * * - memory_usage * Memory used by this server to store items. * * - memory_available * Memory allowed to use for storage. * * @since 2.2 * @return array Associative array with server's statistics if available, NULL otherwise. */ function getStats(); } lib/Doctrine/Common/Cache/CacheProvider.php000064400000014423144760125330014554 0ustar00. */ namespace Doctrine\Common\Cache; /** * Base class for cache provider implementations. * * @since 2.2 * @author Benjamin Eberlei * @author Guilherme Blanco * @author Jonathan Wage * @author Roman Borschel * @author Fabio B. Silva */ abstract class CacheProvider implements Cache { const DOCTRINE_NAMESPACE_CACHEKEY = 'DoctrineNamespaceCacheKey[%s]'; /** * @var string The namespace to prefix all cache ids with */ private $namespace = ''; /** * @var string The namespace version */ private $namespaceVersion; /** * Set the namespace to prefix all cache ids with. * * @param string $namespace * @return void */ public function setNamespace($namespace) { $this->namespace = (string) $namespace; } /** * Retrieve the namespace that prefixes all cache ids. * * @return string */ public function getNamespace() { return $this->namespace; } /** * {@inheritdoc} */ public function fetch($id) { return $this->doFetch($this->getNamespacedId($id)); } /** * {@inheritdoc} */ public function contains($id) { return $this->doContains($this->getNamespacedId($id)); } /** * {@inheritdoc} */ public function save($id, $data, $lifeTime = 0) { return $this->doSave($this->getNamespacedId($id), $data, $lifeTime); } /** * {@inheritdoc} */ public function delete($id) { return $this->doDelete($this->getNamespacedId($id)); } /** * {@inheritdoc} */ public function getStats() { return $this->doGetStats(); } /** * Deletes all cache entries. * * @return boolean TRUE if the cache entries were successfully flushed, FALSE otherwise. */ public function flushAll() { return $this->doFlush(); } /** * Delete all cache entries. * * @return boolean TRUE if the cache entries were successfully deleted, FALSE otherwise. */ public function deleteAll() { $namespaceCacheKey = $this->getNamespaceCacheKey(); $namespaceVersion = $this->getNamespaceVersion() + 1; $this->namespaceVersion = $namespaceVersion; return $this->doSave($namespaceCacheKey, $namespaceVersion); } /** * 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) { $namespaceVersion = $this->getNamespaceVersion(); return sprintf('%s[%s][%s]', $this->namespace, $id, $namespaceVersion); } /** * Namespace cache key * * @return string $namespaceCacheKey */ private function getNamespaceCacheKey() { return sprintf(self::DOCTRINE_NAMESPACE_CACHEKEY, $this->namespace); } /** * Namespace version * * @return string $namespaceVersion */ private function getNamespaceVersion() { if (null !== $this->namespaceVersion) { return $this->namespaceVersion; } $namespaceCacheKey = $this->getNamespaceCacheKey(); $namespaceVersion = $this->doFetch($namespaceCacheKey); if (false === $namespaceVersion) { $namespaceVersion = 1; $this->doSave($namespaceCacheKey, $namespaceVersion); } $this->namespaceVersion = $namespaceVersion; return $this->namespaceVersion; } /** * Fetches an entry from the cache. * * @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 bool|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. * * @param string $id cache id * @return boolean TRUE if the cache entry was successfully deleted, FALSE otherwise. */ abstract protected function doDelete($id); /** * Deletes all cache entries. * * @return boolean TRUE if the cache entry was successfully deleted, FALSE otherwise. */ abstract protected function doFlush(); /** * Retrieves cached information from data store * * @since 2.2 * @return array An associative array with server's statistics if available, NULL otherwise. */ abstract protected function doGetStats(); } lib/Doctrine/Common/Cache/CouchbaseCache.php000064400000006222144760125330014654 0ustar00. */ namespace Doctrine\Common\Cache; use \Couchbase; /** * Couchbase cache provider. * * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @link www.doctrine-project.org * @since 2.4 * @author Michael Nitschinger */ class CouchbaseCache extends CacheProvider { /** * @var Couchbase */ private $couchbase; /** * Sets the Couchbase instance to use. * * @param Couchbase $couchbase */ public function setCouchbase(Couchbase $couchbase) { $this->couchbase = $couchbase; } /** * Gets the Couchbase instance used by the cache. * * @return Couchbase */ public function getCouchbase() { return $this->couchbase; } /** * {@inheritdoc} */ protected function doFetch($id) { return $this->couchbase->get($id) ?: false; } /** * {@inheritdoc} */ protected function doContains($id) { return (null !== $this->couchbase->get($id)); } /** * {@inheritdoc} */ protected function doSave($id, $data, $lifeTime = 0) { if ($lifeTime > 30 * 24 * 3600) { $lifeTime = time() + $lifeTime; } return $this->couchbase->set($id, $data, (int) $lifeTime); } /** * {@inheritdoc} */ protected function doDelete($id) { return $this->couchbase->delete($id); } /** * {@inheritdoc} */ protected function doFlush() { return $this->couchbase->flush(); } /** * {@inheritdoc} */ protected function doGetStats() { $stats = $this->couchbase->getStats(); $servers = $this->couchbase->getServers(); $server = explode(":", $servers[0]); $key = $server[0] . ":" . "11210"; $stats = $stats[$key]; return array( Cache::STATS_HITS => $stats['get_hits'], Cache::STATS_MISSES => $stats['get_misses'], Cache::STATS_UPTIME => $stats['uptime'], Cache::STATS_MEMORY_USAGE => $stats['bytes'], Cache::STATS_MEMORY_AVAILIABLE => $stats['limit_maxbytes'], ); } } lib/Doctrine/Common/Cache/FileCache.php000064400000006655144760125330013651 0ustar00. */ namespace Doctrine\Common\Cache; /** * Base file cache driver. * * @since 2.3 * @author Fabio B. Silva */ abstract class FileCache extends CacheProvider { /** * @var string Cache directory. */ protected $directory; /** * @var string Cache file extension. */ protected $extension; /** * Constructor * * @param string $directory Cache directory. * @param string $directory Cache file extension. * * @throws \InvalidArgumentException */ public function __construct($directory, $extension = null) { if ( ! is_dir($directory) && ! @mkdir($directory, 0777, true)) { throw new \InvalidArgumentException(sprintf( 'The directory "%s" does not exist and could not be created.', $directory )); } if ( ! is_writable($directory)) { throw new \InvalidArgumentException(sprintf( 'The directory "%s" is not writable.', $directory )); } $this->directory = realpath($directory); $this->extension = $extension ?: $this->extension; } /** * Gets the cache directory. * * @return string */ public function getDirectory() { return $this->directory; } /** * Gets the cache file extension. * * @return string */ public function getExtension() { return $this->extension; } /** * @return string */ protected function getFilename($id) { $path = implode(str_split(md5($id), 12), DIRECTORY_SEPARATOR); $path = $this->directory . DIRECTORY_SEPARATOR . $path; return $path . DIRECTORY_SEPARATOR . $id . $this->extension; } /** * {@inheritdoc} */ protected function doDelete($id) { return @unlink($this->getFilename($id)); } /** * {@inheritdoc} */ protected function doFlush() { $pattern = '/^.+\\' . $this->extension . '$/i'; $iterator = new \RecursiveDirectoryIterator($this->directory); $iterator = new \RecursiveIteratorIterator($iterator); $iterator = new \RegexIterator($iterator, $pattern); foreach ($iterator as $name => $file) { @unlink($name); } return true; } /** * {@inheritdoc} */ protected function doGetStats() { return null; } }lib/Doctrine/Common/Cache/FilesystemCache.php000064400000005607144760125330015112 0ustar00. */ namespace Doctrine\Common\Cache; /** * Filesystem cache driver. * * @since 2.3 * @author Fabio B. Silva */ class FilesystemCache extends FileCache { const EXTENSION = '.doctrinecache.data'; /** * {@inheritdoc} */ protected $extension = self::EXTENSION; /** * {@inheritdoc} */ protected function doFetch($id) { $data = ''; $lifetime = -1; $filename = $this->getFilename($id); if ( ! is_file($filename)) { return false; } $resource = fopen($filename, "r"); if (false !== ($line = fgets($resource))) { $lifetime = (integer) $line; } if ($lifetime !== 0 && $lifetime < time()) { fclose($resource); return false; } while (false !== ($line = fgets($resource))) { $data .= $line; } fclose($resource); return unserialize($data); } /** * {@inheritdoc} */ protected function doContains($id) { $lifetime = -1; $filename = $this->getFilename($id); if ( ! is_file($filename)) { return false; } $resource = fopen($filename, "r"); if (false !== ($line = fgets($resource))) { $lifetime = (integer) $line; } fclose($resource); return $lifetime === 0 || $lifetime > time(); } /** * {@inheritdoc} */ protected function doSave($id, $data, $lifeTime = 0) { if ($lifeTime > 0) { $lifeTime = time() + $lifeTime; } $data = serialize($data); $filename = $this->getFilename($id); $filepath = pathinfo($filename, PATHINFO_DIRNAME); if ( ! is_dir($filepath)) { mkdir($filepath, 0777, true); } return file_put_contents($filename, $lifeTime . PHP_EOL . $data); } }lib/Doctrine/Common/Cache/MemcacheCache.php000064400000006205144760125330014463 0ustar00. */ namespace Doctrine\Common\Cache; use \Memcache; /** * Memcache cache provider. * * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @link www.doctrine-project.org * @since 2.0 * @author Benjamin Eberlei * @author Guilherme Blanco * @author Jonathan Wage * @author Roman Borschel * @author David Abdemoulaie */ class MemcacheCache extends CacheProvider { /** * @var Memcache */ private $memcache; /** * Sets the memcache instance to use. * * @param Memcache $memcache */ public function setMemcache(Memcache $memcache) { $this->memcache = $memcache; } /** * Gets the memcache instance used by the cache. * * @return Memcache */ public function getMemcache() { return $this->memcache; } /** * {@inheritdoc} */ protected function doFetch($id) { return $this->memcache->get($id); } /** * {@inheritdoc} */ protected function doContains($id) { return (bool) $this->memcache->get($id); } /** * {@inheritdoc} */ protected function doSave($id, $data, $lifeTime = 0) { if ($lifeTime > 30 * 24 * 3600) { $lifeTime = time() + $lifeTime; } return $this->memcache->set($id, $data, 0, (int) $lifeTime); } /** * {@inheritdoc} */ protected function doDelete($id) { return $this->memcache->delete($id); } /** * {@inheritdoc} */ protected function doFlush() { return $this->memcache->flush(); } /** * {@inheritdoc} */ protected function doGetStats() { $stats = $this->memcache->getStats(); return array( Cache::STATS_HITS => $stats['get_hits'], Cache::STATS_MISSES => $stats['get_misses'], Cache::STATS_UPTIME => $stats['uptime'], Cache::STATS_MEMORY_USAGE => $stats['bytes'], Cache::STATS_MEMORY_AVAILIABLE => $stats['limit_maxbytes'], ); } } lib/Doctrine/Common/Cache/MemcachedCache.php000064400000006472144760125330014635 0ustar00. */ namespace Doctrine\Common\Cache; use \Memcached; /** * Memcached cache provider. * * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @link www.doctrine-project.org * @since 2.2 * @author Benjamin Eberlei * @author Guilherme Blanco * @author Jonathan Wage * @author Roman Borschel * @author David Abdemoulaie */ class MemcachedCache extends CacheProvider { /** * @var Memcached */ private $memcached; /** * Sets the memcache instance to use. * * @param Memcached $memcached */ public function setMemcached(Memcached $memcached) { $this->memcached = $memcached; } /** * Gets the memcached instance used by the cache. * * @return Memcached */ public function getMemcached() { return $this->memcached; } /** * {@inheritdoc} */ protected function doFetch($id) { return $this->memcached->get($id); } /** * {@inheritdoc} */ protected function doContains($id) { return (false !== $this->memcached->get($id)); } /** * {@inheritdoc} */ protected function doSave($id, $data, $lifeTime = 0) { if ($lifeTime > 30 * 24 * 3600) { $lifeTime = time() + $lifeTime; } return $this->memcached->set($id, $data, (int) $lifeTime); } /** * {@inheritdoc} */ protected function doDelete($id) { return $this->memcached->delete($id); } /** * {@inheritdoc} */ protected function doFlush() { return $this->memcached->flush(); } /** * {@inheritdoc} */ protected function doGetStats() { $stats = $this->memcached->getStats(); $servers = $this->memcached->getServerList(); $key = $servers[0]['host'] . ':' . $servers[0]['port']; $stats = $stats[$key]; return array( Cache::STATS_HITS => $stats['get_hits'], Cache::STATS_MISSES => $stats['get_misses'], Cache::STATS_UPTIME => $stats['uptime'], Cache::STATS_MEMORY_USAGE => $stats['bytes'], Cache::STATS_MEMORY_AVAILIABLE => $stats['limit_maxbytes'], ); } } lib/Doctrine/Common/Cache/PhpFileCache.php000064400000005777144760125330014325 0ustar00. */ namespace Doctrine\Common\Cache; /** * Php file cache driver. * * @since 2.3 * @author Fabio B. Silva */ class PhpFileCache extends FileCache { const EXTENSION = '.doctrinecache.php'; /** * {@inheritdoc} */ protected $extension = self::EXTENSION; /** * {@inheritdoc} */ protected function doFetch($id) { $filename = $this->getFilename($id); if ( ! is_file($filename)) { return false; } $value = include $filename; if ($value['lifetime'] !== 0 && $value['lifetime'] < time()) { return false; } return $value['data']; } /** * {@inheritdoc} */ protected function doContains($id) { $filename = $this->getFilename($id); if ( ! is_file($filename)) { return false; } $value = include $filename; return $value['lifetime'] === 0 || $value['lifetime'] > time(); } /** * {@inheritdoc} */ protected function doSave($id, $data, $lifeTime = 0) { if ($lifeTime > 0) { $lifeTime = time() + $lifeTime; } if (is_object($data) && ! method_exists($data, '__set_state')) { throw new \InvalidArgumentException( "Invalid argument given, PhpFileCache only allows objects that implement __set_state() " . "and fully support var_export(). You can use the FilesystemCache to save arbitrary object " . "graphs using serialize()/deserialize()." ); } $filename = $this->getFilename($id); $filepath = pathinfo($filename, PATHINFO_DIRNAME); if ( ! is_dir($filepath)) { mkdir($filepath, 0777, true); } $value = array( 'lifetime' => $lifeTime, 'data' => $data ); $value = var_export($value, true); $code = sprintf('. */ namespace Doctrine\Common\Cache; use Redis; /** * Redis cache provider. * * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @link www.doctrine-project.org * @since 2.2 * @author Osman Ungur */ class RedisCache extends CacheProvider { /** * @var Redis */ private $redis; /** * Sets the redis instance to use. * * @param Redis $redis */ public function setRedis(Redis $redis) { $redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY); $this->redis = $redis; } /** * Gets the redis instance used by the cache. * * @return Redis */ public function getRedis() { return $this->redis; } /** * {@inheritdoc} */ protected function doFetch($id) { return $this->redis->get($id); } /** * {@inheritdoc} */ protected function doContains($id) { return $this->redis->exists($id); } /** * {@inheritdoc} */ protected function doSave($id, $data, $lifeTime = 0) { $result = $this->redis->set($id, $data); if ($lifeTime > 0) { $this->redis->expire($id, $lifeTime); } return $result; } /** * {@inheritdoc} */ protected function doDelete($id) { return $this->redis->delete($id); } /** * {@inheritdoc} */ protected function doFlush() { return $this->redis->flushDB(); } /** * {@inheritdoc} */ protected function doGetStats() { $info = $this->redis->info(); return array( Cache::STATS_HITS => false, Cache::STATS_MISSES => false, Cache::STATS_UPTIME => $info['uptime_in_seconds'], Cache::STATS_MEMORY_USAGE => $info['used_memory'], Cache::STATS_MEMORY_AVAILIABLE => false ); } } lib/Doctrine/Common/Cache/WinCacheCache.php000064400000005301144760125330014436 0ustar00. */ namespace Doctrine\Common\Cache; /** * WinCache cache provider. * * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @link www.doctrine-project.org * @since 2.2 * @author Benjamin Eberlei * @author Guilherme Blanco * @author Jonathan Wage * @author Roman Borschel * @author David Abdemoulaie */ class WinCacheCache extends CacheProvider { /** * {@inheritdoc} */ protected function doFetch($id) { return wincache_ucache_get($id); } /** * {@inheritdoc} */ protected function doContains($id) { return wincache_ucache_exists($id); } /** * {@inheritdoc} */ protected function doSave($id, $data, $lifeTime = 0) { return (bool) wincache_ucache_set($id, $data, (int) $lifeTime); } /** * {@inheritdoc} */ protected function doDelete($id) { return wincache_ucache_delete($id); } /** * {@inheritdoc} */ protected function doFlush() { return wincache_ucache_clear(); } /** * {@inheritdoc} */ protected function doGetStats() { $info = wincache_ucache_info(); $meminfo = wincache_ucache_meminfo(); return array( Cache::STATS_HITS => $info['total_hit_count'], Cache::STATS_MISSES => $info['total_miss_count'], Cache::STATS_UPTIME => $info['total_cache_uptime'], Cache::STATS_MEMORY_USAGE => $meminfo['memory_total'], Cache::STATS_MEMORY_AVAILIABLE => $meminfo['memory_free'], ); } } lib/Doctrine/Common/Cache/XcacheCache.php000064400000006157144760125330014162 0ustar00. */ namespace Doctrine\Common\Cache; /** * Xcache cache driver. * * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @link www.doctrine-project.org * @since 2.0 * @author Benjamin Eberlei * @author Guilherme Blanco * @author Jonathan Wage * @author Roman Borschel * @author David Abdemoulaie */ class XcacheCache extends CacheProvider { /** * {@inheritdoc} */ protected function doFetch($id) { return $this->doContains($id) ? unserialize(xcache_get($id)) : false; } /** * {@inheritdoc} */ protected function doContains($id) { return xcache_isset($id); } /** * {@inheritdoc} */ protected function doSave($id, $data, $lifeTime = 0) { return xcache_set($id, serialize($data), (int) $lifeTime); } /** * {@inheritdoc} */ protected function doDelete($id) { return xcache_unset($id); } /** * {@inheritdoc} */ protected function doFlush() { $this->checkAuthorization(); xcache_clear_cache(XC_TYPE_VAR, 0); return true; } /** * Checks that xcache.admin.enable_auth is Off * * @throws \BadMethodCallException When xcache.admin.enable_auth is On * @return void */ protected function checkAuthorization() { if (ini_get('xcache.admin.enable_auth')) { throw new \BadMethodCallException('To use all features of \Doctrine\Common\Cache\XcacheCache, you must set "xcache.admin.enable_auth" to "Off" in your php.ini.'); } } /** * {@inheritdoc} */ protected function doGetStats() { $this->checkAuthorization(); $info = xcache_info(XC_TYPE_VAR, 0); return array( Cache::STATS_HITS => $info['hits'], Cache::STATS_MISSES => $info['misses'], Cache::STATS_UPTIME => null, Cache::STATS_MEMORY_USAGE => $info['size'], Cache::STATS_MEMORY_AVAILIABLE => $info['avail'], ); } } lib/Doctrine/Common/Cache/ZendDataCache.php000064400000004355144760125330014457 0ustar00. */ namespace Doctrine\Common\Cache; /** * Zend Data Cache cache driver. * * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @link www.doctrine-project.org * @since 2.0 * @author Ralph Schindler * @author Guilherme Blanco */ class ZendDataCache extends CacheProvider { /** * {@inheritdoc} */ protected function doFetch($id) { return zend_shm_cache_fetch($id); } /** * {@inheritdoc} */ protected function doContains($id) { return (false !== zend_shm_cache_fetch($id)); } /** * {@inheritdoc} */ protected function doSave($id, $data, $lifeTime = 0) { return zend_shm_cache_store($id, $data, $lifeTime); } /** * {@inheritdoc} */ protected function doDelete($id) { return zend_shm_cache_delete($id); } /** * {@inheritdoc} */ protected function doFlush() { $namespace = $this->getNamespace(); if (empty($namespace)) { return zend_shm_cache_clear(); } return zend_shm_cache_clear($namespace); } /** * {@inheritdoc} */ protected function doGetStats() { return null; } } phpunit.xml.dist000064400000001461144760125330007726 0ustar00 ./tests/Doctrine/ ./lib/Doctrine/ performance tests/Doctrine/Tests/Common/Cache/ApcCacheTest.php000064400000000640144760125330016017 0ustar00markTestSkipped('The ' . __CLASS__ .' requires the use of APC'); } } protected function _getCacheDriver() { return new ApcCache(); } }tests/Doctrine/Tests/Common/Cache/ArrayCacheTest.php000064400000000572144760125330016376 0ustar00_getCacheDriver(); $stats = $cache->getStats(); $this->assertNull($stats); } }tests/Doctrine/Tests/Common/Cache/CacheTest.php000064400000005632144760125330015401 0ustar00_getCacheDriver(); // Test save $cache->save('test_key', 'testing this out'); // Test contains to test that save() worked $this->assertTrue($cache->contains('test_key')); // Test fetch $this->assertEquals('testing this out', $cache->fetch('test_key')); // Test delete $cache->save('test_key2', 'test2'); $cache->delete('test_key2'); $this->assertFalse($cache->contains('test_key2')); } public function testObjects() { $cache = $this->_getCacheDriver(); // Fetch/save test with objects (Is cache driver serializes/unserializes objects correctly ?) $cache->save('test_object_key', new \ArrayObject()); $this->assertTrue($cache->fetch('test_object_key') instanceof \ArrayObject); } public function testDeleteAll() { $cache = $this->_getCacheDriver(); $cache->save('test_key1', '1'); $cache->save('test_key2', '2'); $cache->deleteAll(); $this->assertFalse($cache->contains('test_key1')); $this->assertFalse($cache->contains('test_key2')); } public function testFlushAll() { $cache = $this->_getCacheDriver(); $cache->save('test_key1', '1'); $cache->save('test_key2', '2'); $cache->flushAll(); $this->assertFalse($cache->contains('test_key1')); $this->assertFalse($cache->contains('test_key2')); } public function testNamespace() { $cache = $this->_getCacheDriver(); $cache->setNamespace('test_'); $cache->save('key1', 'test'); $this->assertTrue($cache->contains('key1')); $cache->setNamespace('test2_'); $this->assertFalse($cache->contains('key1')); } /** * @group DCOM-43 */ public function testGetStats() { $cache = $this->_getCacheDriver(); $stats = $cache->getStats(); $this->assertArrayHasKey(Cache::STATS_HITS, $stats); $this->assertArrayHasKey(Cache::STATS_MISSES, $stats); $this->assertArrayHasKey(Cache::STATS_UPTIME, $stats); $this->assertArrayHasKey(Cache::STATS_MEMORY_USAGE, $stats); $this->assertArrayHasKey(Cache::STATS_MEMORY_AVAILIABLE, $stats); } /** * Make sure that all supported caches return "false" instead of "null" to be compatible * with ORM integration. */ public function testFalseOnFailedFetch() { $cache = $this->_getCacheDriver(); $result = $cache->fetch('nonexistent_key'); $this->assertFalse($result); $this->assertNotNull($result); } /** * @return \Doctrine\Common\Cache\CacheProvider */ abstract protected function _getCacheDriver(); } tests/Doctrine/Tests/Common/Cache/CouchbaseCacheTest.php000064400000002514144760125330017212 0ustar00couchbase = new Couchbase('127.0.0.1', 'Administrator', 'password', 'default'); } catch(Exception $ex) { $this->markTestSkipped('Could not instantiate the Couchbase cache because of: ' . $ex); } } else { $this->markTestSkipped('The ' . __CLASS__ .' requires the use of the couchbase extension'); } } public function testNoExpire() { $cache = $this->_getCacheDriver(); $cache->save('noexpire', 'value', 0); sleep(1); $this->assertTrue($cache->contains('noexpire'), 'Couchbase provider should support no-expire'); } public function testLongLifetime() { $cache = $this->_getCacheDriver(); $cache->save('key', 'value', 30 * 24 * 3600 + 1); $this->assertTrue($cache->contains('key'), 'Couchbase provider should support TTL > 30 days'); } protected function _getCacheDriver() { $driver = new CouchbaseCache(); $driver->setCouchbase($this->couchbase); return $driver; } }tests/Doctrine/Tests/Common/Cache/FilesystemCacheTest.php000064400000005136144760125330017445 0ustar00assertFalse(is_dir($dir)); $this->driver = new FilesystemCache($dir); $this->assertTrue(is_dir($dir)); return $this->driver; } public function testLifetime() { $cache = $this->_getCacheDriver(); // Test save $cache->save('test_key', 'testing this out', 10); // Test contains to test that save() worked $this->assertTrue($cache->contains('test_key')); // Test fetch $this->assertEquals('testing this out', $cache->fetch('test_key')); // access private methods $getFilename = new \ReflectionMethod($cache, 'getFilename'); $getNamespacedId = new \ReflectionMethod($cache, 'getNamespacedId'); $getFilename->setAccessible(true); $getNamespacedId->setAccessible(true); $id = $getNamespacedId->invoke($cache, 'test_key'); $filename = $getFilename->invoke($cache, $id); $data = ''; $lifetime = 0; $resource = fopen($filename, "r"); if (false !== ($line = fgets($resource))) { $lifetime = (integer) $line; } while (false !== ($line = fgets($resource))) { $data .= $line; } $this->assertNotEquals(0, $lifetime, "previous lifetime could not be loaded"); // update lifetime $lifetime = $lifetime - 20; file_put_contents($filename, $lifetime . PHP_EOL . $data); // test expired data $this->assertFalse($cache->contains('test_key')); $this->assertFalse($cache->fetch('test_key')); } public function testGetStats() { $cache = $this->_getCacheDriver(); $stats = $cache->getStats(); $this->assertNull($stats); } public function tearDown() { $dir = $this->driver->getDirectory(); $ext = $this->driver->getExtension(); $iterator = new \RecursiveDirectoryIterator($dir); foreach (new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::CHILD_FIRST) as $file) { if ($file->isFile()) { @unlink($file->getRealPath()); } else { @rmdir($file->getRealPath()); } } } }tests/Doctrine/Tests/Common/Cache/MemcacheCacheTest.php000064400000002376144760125330017026 0ustar00_memcache = new \Memcache; $ok = @$this->_memcache->connect('localhost', 11211); if (!$ok) { $this->markTestSkipped('The ' . __CLASS__ .' requires the use of memcache'); } } else { $this->markTestSkipped('The ' . __CLASS__ .' requires the use of memcache'); } } public function testNoExpire() { $cache = $this->_getCacheDriver(); $cache->save('noexpire', 'value', 0); sleep(1); $this->assertTrue($cache->contains('noexpire'), 'Memcache provider should support no-expire'); } public function testLongLifetime() { $cache = $this->_getCacheDriver(); $cache->save('key', 'value', 30 * 24 * 3600 + 1); $this->assertTrue($cache->contains('key'), 'Memcache provider should support TTL > 30 days'); } protected function _getCacheDriver() { $driver = new MemcacheCache(); $driver->setMemcache($this->_memcache); return $driver; } } tests/Doctrine/Tests/Common/Cache/MemcachedCacheTest.php000064400000002602144760125330017162 0ustar00memcached = new \Memcached(); $this->memcached->setOption(\Memcached::OPT_COMPRESSION, false); $this->memcached->addServer('127.0.0.1', 11211); $fh = @fsockopen('127.0.0.1', 11211); if (!$fh) { $this->markTestSkipped('The ' . __CLASS__ .' requires the use of memcache'); } } else { $this->markTestSkipped('The ' . __CLASS__ .' requires the use of memcache'); } } public function testNoExpire() { $cache = $this->_getCacheDriver(); $cache->save('noexpire', 'value', 0); sleep(1); $this->assertTrue($cache->contains('noexpire'), 'Memcache provider should support no-expire'); } public function testLongLifetime() { $cache = $this->_getCacheDriver(); $cache->save('key', 'value', 30 * 24 * 3600 + 1); $this->assertTrue($cache->contains('key'), 'Memcached provider should support TTL > 30 days'); } protected function _getCacheDriver() { $driver = new MemcachedCache(); $driver->setMemcached($this->memcached); return $driver; } } tests/Doctrine/Tests/Common/Cache/PhpFileCacheTest.php000064400000007512144760125330016650 0ustar00assertFalse(is_dir($dir)); $this->driver = new PhpFileCache($dir); $this->assertTrue(is_dir($dir)); return $this->driver; } public function testObjects() { $this->markTestSkipped('PhpFileCache does not support saving objects that dont implement __set_state()'); } public function testLifetime() { $cache = $this->_getCacheDriver(); // Test save $cache->save('test_key', 'testing this out', 10); // Test contains to test that save() worked $this->assertTrue($cache->contains('test_key')); // Test fetch $this->assertEquals('testing this out', $cache->fetch('test_key')); // access private methods $getFilename = new \ReflectionMethod($cache, 'getFilename'); $getNamespacedId = new \ReflectionMethod($cache, 'getNamespacedId'); $getFilename->setAccessible(true); $getNamespacedId->setAccessible(true); $id = $getNamespacedId->invoke($cache, 'test_key'); $path = $getFilename->invoke($cache, $id); $value = include $path; // update lifetime $value['lifetime'] = $value['lifetime'] - 20; file_put_contents($path, 'assertFalse($cache->contains('test_key')); $this->assertFalse($cache->fetch('test_key')); } public function testImplementsSetState() { $cache = $this->_getCacheDriver(); // Test save $cache->save('test_set_state', new SetStateClass(array(1,2,3))); //Test __set_state call $this->assertCount(0, SetStateClass::$values); // Test fetch $value = $cache->fetch('test_set_state'); $this->assertInstanceOf('Doctrine\Tests\Common\Cache\SetStateClass', $value); $this->assertEquals(array(1,2,3), $value->getValue()); //Test __set_state call $this->assertCount(1, SetStateClass::$values); // Test contains $this->assertTrue($cache->contains('test_set_state')); } public function testNotImplementsSetState() { $cache = $this->_getCacheDriver(); $this->setExpectedException('InvalidArgumentException'); $cache->save('test_not_set_state', new NotSetStateClass(array(1,2,3))); } public function testGetStats() { $cache = $this->_getCacheDriver(); $stats = $cache->getStats(); $this->assertNull($stats); } public function tearDown() { if (!$this->driver) { return; } $dir = $this->driver->getDirectory(); $ext = $this->driver->getExtension(); $iterator = new \RecursiveDirectoryIterator($dir); foreach (new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::CHILD_FIRST) as $file) { if ($file->isFile()) { @unlink($file->getRealPath()); } else { @rmdir($file->getRealPath()); } } } } class NotSetStateClass { private $value; public function __construct($value) { $this->value = $value; } public function getValue() { return $this->value; } } class SetStateClass extends NotSetStateClass { public static $values = array(); public static function __set_state($data) { self::$values = $data; return new self($data['value']); } } tests/Doctrine/Tests/Common/Cache/RedisCacheTest.php000064400000001327144760125330016365 0ustar00_redis = new \Redis(); $ok = @$this->_redis->connect('127.0.0.1'); if (!$ok) { $this->markTestSkipped('The ' . __CLASS__ .' requires the use of redis'); } } else { $this->markTestSkipped('The ' . __CLASS__ .' requires the use of redis'); } } protected function _getCacheDriver() { $driver = new RedisCache(); $driver->setRedis($this->_redis); return $driver; } } tests/Doctrine/Tests/Common/Cache/WinCacheCacheTest.php000064400000000707144760125330017001 0ustar00markTestSkipped('The ' . __CLASS__ .' requires the use of Wincache'); } } protected function _getCacheDriver() { return new WincacheCache(); } }tests/Doctrine/Tests/Common/Cache/XcacheCacheTest.php000064400000000620144760125330016505 0ustar00markTestSkipped('The ' . __CLASS__ .' requires the use of xcache'); } } protected function _getCacheDriver() { return new XcacheCache(); } }tests/Doctrine/Tests/Common/Cache/ZendDataCacheTest.php000064400000001237144760125330017011 0ustar00markTestSkipped('The ' . __CLASS__ .' requires the use of Zend Data Cache which only works in apache2handler SAPI'); } } public function testGetStats() { $cache = $this->_getCacheDriver(); $stats = $cache->getStats(); $this->assertNull($stats); } protected function _getCacheDriver() { return new ZendDataCache(); } }tests/Doctrine/Tests/DoctrineTestCase.php000064400000000244144760125330014500 0ustar00