1 <?php 2 /* Copyright (c) 2012, Geert Bergman (geert@scrivo.nl) 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. Neither the name of "Scrivo" nor the names of its contributors may be 14 * used to endorse or promote products derived from this software without 15 * specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 * 29 * $Id: ApcCache.php 629 2013-05-20 23:02:09Z geert $ 30 */ 31 32 /** 33 * Implementation of the \Scrivo\ApcCache class. 34 */ 35 36 namespace Scrivo\Cache; 37 38 /** 39 * Implementation of the Cache interface using APC. 40 * 41 * This is an implementation of the Cache interface using APC. It is a very 42 * straightforward mapping of the interface methods to the APC functions. 43 * An notable difference is that cache slams are not allowed, so store will 44 * fail to store where apc_store would succeed. 45 */ 46 class ApcCache implements \Scrivo\Cache { 47 48 /** 49 * Create an APC cache wrapper. 50 */ 51 public function __construct() { 52 } 53 54 /** 55 * Store a variable in the cache. 56 * 57 * Store any serializable variable in the cache. Note that it is not 58 * possible to overwrite an existing entry (cache slam). Such an event 59 * will not raise an error but the function will report it. 60 * 61 * @param \Scrivo\String $key A cache unique name for the key. 62 * @param mixed $val The (serializable) variabele to strore. 63 * @param int $ttl Time to live in seconds. 64 * 65 * @return int DATA_STORED if the variable was succesfully stored or 66 * CACHE_SLAM if key already exists. 67 * 68 * @throws \Scrivo\SystemException When trying to store a NULL value. 69 */ 70 public function store(\Scrivo\String $key, $val, $ttl=3600) { 71 if ($val === null) { 72 throw new \Scrivo\SystemException( 73 "Can't store null values in the cache"); 74 } 75 if (apc_fetch((string)$key)) { 76 return self::CACHE_SLAM; 77 } 78 apc_store((string)$key, $val, $ttl); 79 return self::DATA_STORED; 80 } 81 82 /** 83 * Store a variable in the cache, overwrite it if it already exists. 84 * 85 * Store any serializable variable in the cache. It is guaranteed that 86 * the data will be written. But note that it is not guaranteed that the 87 * next fetch will retrieve this value. 88 * 89 * @param \Scrivo\String $key A cache unique name for the key. 90 * @param mixed $val The (serializable) variabele to strore. 91 * @param int $ttl Time to live in seconds. 92 * 93 * @return int DATA_STORED if the variable was succesfully stored. 94 * 95 * @throws \Scrivo\SystemException When trying to store a NULL value or 96 * when a file operation fails. 97 */ 98 public function overwrite(\Scrivo\String $key, $val, $ttl=3600) { 99 if ($val === null) { 100 throw new \Scrivo\SystemException( 101 "Can't store null values in the cache"); 102 } 103 apc_store((string)$key, $val, $ttl); 104 return self::DATA_STORED; 105 } 106 107 /** 108 * Delete/remove a cache entry. 109 * 110 * @param \Scrivo\String $key A cache unique name for the key. 111 */ 112 public function delete(\Scrivo\String $key) { 113 apc_delete((string)$key); 114 } 115 116 /** 117 * Retrieve a value from the cache. 118 * 119 * @param \Scrivo\String $key The key for which to retrieve the value. 120 * 121 * @return mixed The value of the stored variable or NULL if the key 122 * does not exists or is expired. 123 */ 124 public function fetch(\Scrivo\String $key) { 125 $res = apc_fetch((string)$key); 126 return $res ? $res : null; 127 } 128 129 /** 130 * List all entries in the cache. 131 * 132 * This method returns an array in which the cache keys are the keys of 133 * the array entries and the data of each entry is an object of type 134 * stdClass that contains at least the following properties: 135 * 136 * * accessed: the access time 137 * * expires: the expiration time 138 * * created: the creation time 139 * * size: the size of the entry 140 * 141 * @return object[] A array of objects that describe the current cache 142 * entries. 143 */ 144 public function entryList() { 145 $l = array(); 146 $d = apc_cache_info('user'); 147 foreach ($d["cache_list"] as $v) { 148 $l[(string)$v["info"]] = (object)array( 149 "accessed" => $v["access_time"], 150 "expires" => $v["creation_time"]+$v["ttl"], 151 "created" => $v["creation_time"], 152 "size" => $v["mem_size"] 153 ); 154 } 155 return $l; 156 } 157 158 } 159
Documentation generated by phpDocumentor 2.0.0a12 and ScrivoDocumentor on August 29, 2013