| 1 | <?php |
| 2 | /* |
| 3 | * $Id: Array.php 2963 2007-10-21 06:23:59Z Jonathan.Wage $ |
| 4 | * |
| 5 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 6 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 7 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 8 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 9 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 10 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 11 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 12 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 13 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 14 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 15 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 16 | * |
| 17 | * This software consists of voluntary contributions made by many individuals |
| 18 | * and is licensed under the LGPL. For more information, see |
| 19 | * <http://www.phpdoctrine.org>. |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * Doctrine_Cache_Interface |
| 24 | * |
| 25 | * @package Doctrine |
| 26 | * @subpackage Cache |
| 27 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
| 28 | * @link www.phpdoctrine.org |
| 29 | * @since 1.0 |
| 30 | * @version $Revision: 2963 $ |
| 31 | * @author Konsta Vesterinen <kvesteri@cc.hut.fi> |
| 32 | */ |
| 33 | class Doctrine_Cache_Array implements Countable, Doctrine_Cache_Interface |
| 34 | { |
| 35 | /** |
| 36 | * @var array $data an array of cached data |
| 37 | */ |
| 38 | protected $data; |
| 39 | |
| 40 | /** |
| 41 | * Test if a cache is available for the given id and (if yes) return it (false else) |
| 42 | * |
| 43 | * Note : return value is always "string" (unserialization is done by the core not by the backend) |
| 44 | * |
| 45 | * @param string $id cache id |
| 46 | * @param boolean $testCacheValidity if set to false, the cache validity won't be tested |
| 47 | * @return string cached datas (or false) |
| 48 | */ |
| 49 | public function fetch($id, $testCacheValidity = true) |
| 50 | { |
| 51 | if (isset($this->data[$id])) { |
| 52 | return $this->data[$id]; |
| 53 | } |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Test if a cache is available or not (for the given id) |
| 59 | * |
| 60 | * @param string $id cache id |
| 61 | * @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record |
| 62 | */ |
| 63 | public function contains($id) |
| 64 | { |
| 65 | return isset($this->data[$id]); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Save some string datas into a cache record |
| 70 | * |
| 71 | * Note : $data is always saved as a string |
| 72 | * |
| 73 | * @param string $data data to cache |
| 74 | * @param string $id cache id |
| 75 | * @param int $lifeTime if != false, set a specific lifetime for this cache record (null => infinite lifeTime) |
| 76 | * @return boolean true if no problem |
| 77 | */ |
| 78 | public function save($id, $data, $lifeTime = false) |
| 79 | { |
| 80 | $this->data[$id] = $data; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Remove a cache record |
| 85 | * |
| 86 | * @param string $id cache id |
| 87 | * @return boolean true if no problem |
| 88 | */ |
| 89 | public function delete($id) |
| 90 | { |
| 91 | unset($this->data[$id]); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Remove all cache record |
| 96 | * |
| 97 | * @return boolean true if no problem |
| 98 | */ |
| 99 | public function deleteAll() |
| 100 | { |
| 101 | $this->data = array(); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * count |
| 106 | * |
| 107 | * @return integer |
| 108 | */ |
| 109 | public function count() |
| 110 | { |
| 111 | return count($this->data); |
| 112 | } |
| 113 | } |