]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/Cache.php
Merge branch '0.9.x' of gitorious.org:statusnet/mainline into 1.0.x
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Plugin / Cache.php
1 <?php
2 /**
3  * Phergie
4  *
5  * PHP version 5
6  *
7  * LICENSE
8  *
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
13  *
14  * @category  Phergie
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
20  */
21
22 /**
23  * Implements a generic cache to be used by other plugins.
24  *
25  * @category Phergie
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
30  */
31 class Phergie_Plugin_Cache extends Phergie_Plugin_Abstract
32 {
33     /**
34      * Key-value data storage for the cache 
35      * 
36      * @var array 
37      */
38     protected $cache = array();
39
40     /**
41      * Stores a value in the cache. 
42      *
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
48      *
49      * @return bool
50      */
51     public function store($key, $data, $ttl = 3600, $overwrite = true)
52     {
53         if (!$overwrite && isset($this->cache[$key])) {
54             return false;
55         }
56
57         if ($ttl) {
58             $expires = time()+$ttl;
59         } else {
60             $expires = null;
61         }
62
63         $this->cache[$key] = array('data' => $data, 'expires' => $expires);
64         return true;
65
66     }
67
68     /**
69      * Fetches a previously stored value. 
70      *
71      * @param string $key Key associated with the value 
72      *
73      * @return mixed Stored value or FALSE if no value or an expired value 
74      *         is associated with the specified key 
75      */
76     public function fetch($key)
77     {
78         if (!isset($this->cache[$key])) {
79             return false;
80         }
81
82         $item = $this->cache[$key];
83         if (!is_null($item['expires']) && $item['expires'] < time()) {
84             $this->expire($key);
85             return false;
86         }
87
88         return $item['data'];
89     }
90
91     /**
92      * Expires a value that has exceeded its time to live.
93      *
94      * @param string $key Key associated with the value to expire 
95      *
96      * @return bool
97      */
98     protected function expire($key)
99     {
100         if (!isset($this->cache[$key])) {
101             return false;
102         }
103         unset($this->cache[$key]);
104         return true;
105     }
106 }