9 * This source file is subject to the new BSD license that is bundled
10 * with this package in the file LICENSE.
11 * It is also available through the world-wide-web at this URL:
12 * http://phergie.org/license
15 * @package Phergie_Plugin_Cache
16 * @author Phergie Development Team <team@phergie.org>
17 * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
18 * @license http://phergie.org/license New BSD License
19 * @link http://pear.phergie.org/package/Phergie_Plugin_Cache
23 * Implements a generic cache to be used by other plugins.
26 * @package Phergie_Plugin_Cache
27 * @author Phergie Development Team <team@phergie.org>
28 * @license http://phergie.org/license New BSD License
29 * @link http://pear.phergie.org/package/Phergie_Plugin_Cache
31 class Phergie_Plugin_Cache extends Phergie_Plugin_Abstract
34 * Key-value data storage for the cache
38 protected $cache = array();
41 * Stores a value in the cache.
43 * @param string $key Key to associate with the value
44 * @param mixed $data Data to be stored
45 * @param int|null $ttl Time to live in seconds or NULL for forever
46 * @param bool $overwrite TRUE to overwrite any existing value
47 * associated with the specified key
51 public function store($key, $data, $ttl = 3600, $overwrite = true)
53 if (!$overwrite && isset($this->cache[$key])) {
58 $expires = time()+$ttl;
63 $this->cache[$key] = array('data' => $data, 'expires' => $expires);
69 * Fetches a previously stored value.
71 * @param string $key Key associated with the value
73 * @return mixed Stored value or FALSE if no value or an expired value
74 * is associated with the specified key
76 public function fetch($key)
78 if (!isset($this->cache[$key])) {
82 $item = $this->cache[$key];
83 if (!is_null($item['expires']) && $item['expires'] < time()) {
92 * Expires a value that has exceeded its time to live.
94 * @param string $key Key associated with the value to expire
98 protected function expire($key)
100 if (!isset($this->cache[$key])) {
103 unset($this->cache[$key]);