Author: romanb Date: 2008-09-12 10:28:18 +0100 (Fri, 12 Sep 2008) New Revision: 4917
Added: trunk/lib/Doctrine/ORM/Cache/Apc.php trunk/lib/Doctrine/ORM/Cache/Array.php trunk/lib/Doctrine/ORM/Cache/Db.php trunk/lib/Doctrine/ORM/Cache/Driver.php trunk/lib/Doctrine/ORM/Cache/Exception.php trunk/lib/Doctrine/ORM/Cache/Interface.php trunk/lib/Doctrine/ORM/Cache/Memcache.php trunk/lib/Doctrine/ORM/Cache/Xcache.php Removed: trunk/lib/Doctrine/Cache/ Log: moved Cache Added: trunk/lib/Doctrine/ORM/Cache/Apc.php =================================================================== --- trunk/lib/Doctrine/ORM/Cache/Apc.php (rev 0) +++ trunk/lib/Doctrine/ORM/Cache/Apc.php 2008-09-12 09:28:18 UTC (rev 4917) @@ -0,0 +1,96 @@ +<?php +/* + * $Id: Apc.php 4910 2008-09-12 08:51:56Z romanb $ + * + * 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.phpdoctrine.org>. + */ + +#namespace Doctrine::ORM::Cache; + +/** + * APC cache driver. + * + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.phpdoctrine.org + * @since 1.0 + * @version $Revision: 4910 $ + * @author Konsta Vesterinen <[EMAIL PROTECTED]> + */ +class Doctrine_Cache_Apc extends Doctrine_Cache_Driver +{ + /** + * constructor + * + * @param array $options associative array of cache driver options + */ + public function __construct($options = array()) + { + if ( ! extension_loaded('apc')) { + throw new Doctrine_Cache_Exception('The apc extension must be loaded for using this backend !'); + } + parent::__construct($options); + } + + /** + * Test if a cache is available for the given id and (if yes) return it (false else). + * + * @param string $id cache id + * @param boolean $testCacheValidity if set to false, the cache validity won't be tested + * @return string cached datas (or false) + */ + public function fetch($id, $testCacheValidity = true) + { + return apc_fetch($id); + } + + /** + * Test if a cache is available or not (for the given id) + * + * @param string $id cache id + * @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record + */ + public function contains($id) + { + return apc_fetch($id) === false ? false : true; + } + + /** + * Save some string datas into a cache record + * + * Note : $data is always saved as a string + * + * @param string $data data to cache + * @param string $id cache id + * @param int $lifeTime if != false, set a specific lifetime for this cache record (null => infinite lifeTime) + * @return boolean true if no problem + */ + public function save($id, $data, $lifeTime = false) + { + return (bool) apc_store($id, $data, $lifeTime); + } + + /** + * Remove a cache record + * + * @param string $id cache id + * @return boolean true if no problem + */ + public function delete($id) + { + return apc_delete($id); + } +} Added: trunk/lib/Doctrine/ORM/Cache/Array.php =================================================================== --- trunk/lib/Doctrine/ORM/Cache/Array.php (rev 0) +++ trunk/lib/Doctrine/ORM/Cache/Array.php 2008-09-12 09:28:18 UTC (rev 4917) @@ -0,0 +1,109 @@ +<?php +/* + * $Id: Array.php 4910 2008-09-12 08:51:56Z romanb $ + * + * 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.phpdoctrine.org>. + */ + +/** + * Array cache driver. + * + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.phpdoctrine.org + * @since 1.0 + * @version $Revision: 4910 $ + * @author Konsta Vesterinen <[EMAIL PROTECTED]> + */ +class Doctrine_Cache_Array implements Doctrine_Cache_Interface +{ + /** + * @var array $data an array of cached data + */ + protected $data; + + /** + * Test if a cache is available for the given id and (if yes) return it (false else). + * + * @param string $id cache id + * @param boolean $testCacheValidity if set to false, the cache validity won't be tested + * @return string cached datas (or false) + */ + public function fetch($id, $testCacheValidity = true) + { + if (isset($this->data[$id])) { + return $this->data[$id]; + } + return false; + } + + /** + * Test if a cache is available or not (for the given id) + * + * @param string $id cache id + * @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record + */ + public function contains($id) + { + return isset($this->data[$id]); + } + + /** + * Save some string datas into a cache record + * + * Note : $data is always saved as a string + * + * @param string $data data to cache + * @param string $id cache id + * @param int $lifeTime if != false, set a specific lifetime for this cache record (null => infinite lifeTime) + * @return boolean true if no problem + */ + public function save($id, $data, $lifeTime = false) + { + $this->data[$id] = $data; + } + + /** + * Remove a cache record + * + * @param string $id cache id + * @return boolean true if no problem + */ + public function delete($id) + { + unset($this->data[$id]); + } + + /** + * Remove all cache record + * + * @return boolean true if no problem + */ + public function deleteAll() + { + $this->data = array(); + } + + /** + * count + * + * @return integer + */ + public function count() + { + return count($this->data); + } +} Added: trunk/lib/Doctrine/ORM/Cache/Db.php =================================================================== --- trunk/lib/Doctrine/ORM/Cache/Db.php (rev 0) +++ trunk/lib/Doctrine/ORM/Cache/Db.php 2008-09-12 09:28:18 UTC (rev 4917) @@ -0,0 +1,198 @@ +<?php +/* + * $Id: Db.php 3931 2008-03-05 11:24:33Z romanb $ + * + * 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.phpdoctrine.org>. + */ + +/** + * Doctrine_Cache_Db + * + * @package Doctrine + * @subpackage Cache + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.phpdoctrine.org + * @since 1.0 + * @version $Revision: 3931 $ + * @author Konsta Vesterinen <[EMAIL PROTECTED]> + */ +class Doctrine_Cache_Db extends Doctrine_Cache_Driver implements Countable +{ + /** + * constructor + * + * @param array $_options an array of options + */ + public function __construct($options) + { + if ( ! isset($options['connection']) || + ! ($options['connection'] instanceof Doctrine_Connection)) { + + throw new Doctrine_Cache_Exception('Connection option not set.'); + } + + if ( ! isset($options['tableName']) || + ! is_string($options['tableName'])) { + + throw new Doctrine_Cache_Exception('Table name option not set.'); + } + + + $this->_options = $options; + } + + /** + * getConnection + * returns the connection object associated with this cache driver + * + * @return Doctrine_Connection connection object + */ + public function getConnection() + { + return $this->_options['connection']; + } + + /** + * Test if a cache is available for the given id and (if yes) return it (false else). + * + * @param string $id cache id + * @param boolean $testCacheValidity if set to false, the cache validity won't be tested + * @return string cached datas (or false) + */ + public function fetch($id, $testCacheValidity = true) + { + $sql = 'SELECT data, expire FROM ' . $this->_options['tableName'] + . ' WHERE id = ?'; + + if ($testCacheValidity) { + $sql .= ' AND (expire=0 OR expire > ' . time() . ')'; + } + + $result = $this->getConnection()->fetchAssoc($sql, array($id)); + + if ( ! isset($result[0])) { + return false; + } + + return unserialize($result[0]['data']); + } + + /** + * Test if a cache is available or not (for the given id) + * + * @param string $id cache id + * @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record + */ + public function contains($id) + { + $sql = 'SELECT expire FROM ' . $this->_options['tableName'] + . ' WHERE id = ? AND (expire=0 OR expire > ' . time() . ')'; + + return $this->getConnection()->fetchOne($sql, array($id)); + } + + /** + * Save some string datas into a cache record + * + * Note : $data is always saved as a string + * + * @param string $data data to cache + * @param string $id cache id + * @param int $lifeTime if != false, set a specific lifetime for this cache record (null => infinite lifeTime) + * @return boolean true if no problem + */ + public function save($data, $id, $lifeTime = false) + { + $sql = 'INSERT INTO ' . $this->_options['tableName'] + . ' (id, data, expire) VALUES (?, ?, ?)'; + + if ($lifeTime) { + $expire = time() + $lifeTime; + } else { + $expire = 0; + } + + $params = array($id, serialize($data), $expire); + + return (bool) $this->getConnection()->exec($sql, $params); + } + + /** + * Remove a cache record + * + * @param string $id cache id + * @return boolean true if no problem + */ + public function delete($id) + { + $sql = 'DELETE FROM ' . $this->_options['tableName'] . ' WHERE id = ?'; + + return (bool) $this->getConnection()->exec($sql, array($id)); + } + + /** + * Removes all cache records + * + * $return bool true on success, false on failure + */ + public function deleteAll() + { + $sql = 'DELETE FROM ' . $this->_options['tableName']; + + return (bool) $this->getConnection()->exec($sql); + } + + /** + * count + * returns the number of cached elements + * + * @return integer + */ + public function count() + { + $sql = 'SELECT COUNT(*) FROM ' . $this->_options['tableName']; + + return (int) $this->getConnection()->fetchOne($sql); + } + + /** + * Creates the cache table. + */ + public function createTable() + { + $name = $this->_options['tableName']; + + $fields = array( + 'id' => array( + 'type' => 'string', + 'length' => 255 + ), + 'data' => array( + 'type' => 'blob' + ), + 'expire' => array( + 'type' => 'timestamp' + ) + ); + + $options = array( + 'primary' => array('id') + ); + + $this->getConnection()->export->createTable($name, $fields, $options); + } +} Added: trunk/lib/Doctrine/ORM/Cache/Driver.php =================================================================== --- trunk/lib/Doctrine/ORM/Cache/Driver.php (rev 0) +++ trunk/lib/Doctrine/ORM/Cache/Driver.php 2008-09-12 09:28:18 UTC (rev 4917) @@ -0,0 +1,78 @@ +<?php +/* + * $Id: Driver.php 4910 2008-09-12 08:51:56Z romanb $ + * + * 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.phpdoctrine.org>. + */ + +/** + * Base class for cache drivers. + * + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.phpdoctrine.org + * @since 1.0 + * @version $Revision: 4910 $ + * @author Konsta Vesterinen <[EMAIL PROTECTED]> + */ +abstract class Doctrine_Cache_Driver implements Doctrine_Cache_Interface +{ + /** + * @var array $_options an array of options + */ + protected $_options = array(); + + /** + * constructor + * + * @param array $_options an array of options + */ + public function __construct($options) + { + $this->_options = $options; + } + + /** + * setOption + * + * @param mixed $option the option name + * @param mixed $value option value + * @return boolean TRUE on success, FALSE on failure + */ + public function setOption($option, $value) + { + if (isset($this->_options[$option])) { + $this->_options[$option] = $value; + return true; + } + return false; + } + + /** + * getOption + * + * @param mixed $option the option name + * @return mixed option value + */ + public function getOption($option) + { + if ( ! isset($this->_options[$option])) { + return null; + } + + return $this->_options[$option]; + } +} Added: trunk/lib/Doctrine/ORM/Cache/Exception.php =================================================================== --- trunk/lib/Doctrine/ORM/Cache/Exception.php (rev 0) +++ trunk/lib/Doctrine/ORM/Cache/Exception.php 2008-09-12 09:28:18 UTC (rev 4917) @@ -0,0 +1,34 @@ +<?php +/* + * $Id: Exception.php 3882 2008-02-22 18:11:35Z jwage $ + * + * 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.phpdoctrine.org>. + */ +Doctrine::autoload('Doctrine_Exception'); +/** + * Doctrine_Cache_Exception + * + * @package Doctrine + * @subpackage Cache + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.phpdoctrine.org + * @since 1.0 + * @version $Revision: 3882 $ + * @author Konsta Vesterinen <[EMAIL PROTECTED]> + */ +class Doctrine_Cache_Exception extends Doctrine_Exception +{ } Added: trunk/lib/Doctrine/ORM/Cache/Interface.php =================================================================== --- trunk/lib/Doctrine/ORM/Cache/Interface.php (rev 0) +++ trunk/lib/Doctrine/ORM/Cache/Interface.php 2008-09-12 09:28:18 UTC (rev 4917) @@ -0,0 +1,73 @@ +<?php +/* + * $Id: Interface.php 3931 2008-03-05 11:24:33Z romanb $ + * + * 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.phpdoctrine.org>. + */ + +/** + * Doctrine_Cache_Interface + * + * @package Doctrine + * @subpackage Cache + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.phpdoctrine.org + * @since 1.0 + * @version $Revision: 3931 $ + * @author Konsta Vesterinen <[EMAIL PROTECTED]> + */ +interface Doctrine_Cache_Interface +{ + /** + * Test if a cache entry is available for the given id and (if yes) return it (false else). + * + * Note : return value is always "string" (unserialization is done by the core not by the backend) + * + * @param string $id cache id + * @param boolean $testCacheValidity if set to false, the cache validity won't be tested + * @return string cached datas (or false) + */ + public function fetch($id, $testCacheValidity = true); + + /** + * Test if a cache is available or not (for the given id) + * + * @param string $id cache id + * @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record + */ + public function contains($id); + + /** + * Save some string datas into a cache record + * + * Note : $data is always saved as a string + * + * @param string $data data to cache + * @param string $id cache id + * @param int $lifeTime if != false, set a specific lifetime for this cache record (null => infinite lifeTime) + * @return boolean true if no problem + */ + public function save($data, $id, $lifeTime = false); + + /** + * Remove a cache record + * + * @param string $id cache id + * @return boolean true if no problem + */ + public function delete($id); +} Added: trunk/lib/Doctrine/ORM/Cache/Memcache.php =================================================================== --- trunk/lib/Doctrine/ORM/Cache/Memcache.php (rev 0) +++ trunk/lib/Doctrine/ORM/Cache/Memcache.php 2008-09-12 09:28:18 UTC (rev 4917) @@ -0,0 +1,126 @@ +<?php +/* + * $Id: Memcache.php 4910 2008-09-12 08:51:56Z romanb $ + * + * 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.phpdoctrine.org>. + */ + +/** + * Memcache cache driver. + * + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.phpdoctrine.org + * @since 1.0 + * @version $Revision: 4910 $ + * @author Konsta Vesterinen <[EMAIL PROTECTED]> + */ +class Doctrine_Cache_Memcache extends Doctrine_Cache_Driver +{ + /** + * @var Memcache $_memcache memcache object + */ + protected $_memcache = null; + + /** + * constructor + * + * @param array $options associative array of cache driver options + */ + public function __construct($options = array()) + { + if ( ! extension_loaded('memcache')) { + throw new Doctrine_Cache_Exception('In order to use Memcache driver, the memcache extension must be loaded.'); + } + parent::__construct($options); + + if (isset($options['servers'])) { + $value= $options['servers']; + if (isset($value['host'])) { + // in this case, $value seems to be a simple associative array (one server only) + $value = array(0 => $value); // let's transform it into a classical array of associative arrays + } + $this->setOption('servers', $value); + } + + $this->_memcache = new Memcache; + + foreach ($this->_options['servers'] as $server) { + if ( ! array_key_exists('persistent', $server)) { + $server['persistent'] = true; + } + if ( ! array_key_exists('port', $server)) { + $server['port'] = 11211; + } + $this->_memcache->addServer($server['host'], $server['port'], $server['persistent']); + } + } + + /** + * Test if a cache is available for the given id and (if yes) return it (false else). + * + * @param string $id cache id + * @param boolean $testCacheValidity if set to false, the cache validity won't be tested + * @return string cached datas (or false) + */ + public function fetch($id, $testCacheValidity = true) + { + return $this->_memcache->get($id); + } + + /** + * Test if a cache is available or not (for the given id) + * + * @param string $id cache id + * @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record + */ + public function contains($id) + { + return (bool) $this->_memcache->get($id); + } + + /** + * Save some string datas into a cache record + * + * Note : $data is always saved as a string + * + * @param string $data data to cache + * @param string $id cache id + * @param int $lifeTime if != false, set a specific lifetime for this cache record (null => infinite lifeTime) + * @return boolean true if no problem + */ + public function save($id, $data, $lifeTime = false) + { + if ($this->_options['compression']) { + $flag = MEMCACHE_COMPRESSED; + } else { + $flag = 0; + } + + $result = $this->_memcache->set($id, $data, $flag, $lifeTime); + } + + /** + * Remove a cache record + * + * @param string $id cache id + * @return boolean true if no problem + */ + public function delete($id) + { + return $this->_memcache->delete($id); + } +} Added: trunk/lib/Doctrine/ORM/Cache/Xcache.php =================================================================== --- trunk/lib/Doctrine/ORM/Cache/Xcache.php (rev 0) +++ trunk/lib/Doctrine/ORM/Cache/Xcache.php 2008-09-12 09:28:18 UTC (rev 4917) @@ -0,0 +1,95 @@ +<?php +/* + * $Id: Xcache.php 2007-11-19 14:47:59Z demongloom $ + * + * 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.phpdoctrine.org>. + */ + +/** + * Xcache cache driver. + * + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.phpdoctrine.org + * @since 1.0 + * @version $Revision: $ + * @author Dmitry Bakaleinik ([EMAIL PROTECTED]) + */ +class Doctrine_Cache_Xcache extends Doctrine_Cache_Driver +{ + /** + * constructor + * + * @param array $options associative array of cache driver options + */ + public function __construct($options = array()) + { + if ( ! extension_loaded('xcache')) { + throw new Doctrine_Cache_Exception('In order to use Xcache driver, the xcache extension must be loaded.'); + } + + parent::__construct($options); + } + + /** + * Test if a cache entry is available for the given id and (if yes) return it (false else). + * + * @param string $id cache id + * @param boolean $testCacheValidity if set to false, the cache validity won't be tested + * @return string cached datas (or false) + */ + public function fetch($id, $testCacheValidity = true) + { + return $this->contains($id) ? xcache_get($id) : false; + } + + /** + * Test if a cache is available or not (for the given id) + * + * @param string $id cache id + * @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record + */ + public function contains($id) + { + return xcache_isset($id); + } + + /** + * Save some string datas into a cache record + * + * Note : $data is always saved as a string + * + * @param string $data data to cache + * @param string $id cache id + * @param int $lifeTime if != false, set a specific lifetime for this cache record (null => infinite lifeTime) + * @return boolean true if no problem + */ + public function save($id, $data, $lifeTime = false) + { + return xcache_set($id, $data, $lifeTime); + } + + /** + * Remove a cache record + * + * @param string $id cache id + * @return boolean true if no problem + */ + public function delete($id) + { + return xcache_unset($id); + } +} \ No newline at end of file --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "doctrine-svn" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.co.uk/group/doctrine-svn?hl=en-GB -~----------~----~----~----~------~----~------~--~---
