summaryrefslogtreecommitdiffstats
path: root/common/app/lib/Cache.php
blob: c0281102b242caa87359fcabd8abe26d8e33e6ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
/**
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
* 
* © Copyright 2005 Richard Heyes
*/

/**
* Caching Libraries for PHP5
* 
* Handles data and output caching. Defaults to /dev/shm
* (shared memory). All methods are static.
* 
* Eg: (output caching)
* 
* if (!OutputCache::Start('group', 'unique id', 600)) {
* 
*   // ... Output
* 
*   OutputCache::End();
* }
* 
* Eg: (data caching)
* 
* if (!$data = DataCache::Get('group', 'unique id')) {
* 
*   $data = time();
* 
*   DataCache::Put('group', 'unique id', 10, $data);
* }
* 
* echo $data;
*/
    class Cache
    {
        /**
        * Whether caching is enabled
        * @var bool
        */
        public static $enabled = true;

        /**
        * Place to store the cache files
        * @var string
        */
        protected static $store = '/dev/shm/';
        
        /**
        * Prefix to use on cache files
        * @var string
        */
        protected static $prefix = 'cache_';

        /**
        * Stores data
        * 
        * @param string $group Group to store data under
        * @param string $id    Unique ID of this data
        * @param int    $ttl   How long to cache for (in seconds)
        */
        protected static function write($group, $id, $ttl, $data)
        {
            $filename = self::getFilename($group, $id);
            
            if (self::$enabled && $fp = fopen($filename, 'xb')) {
            
                if (flock($fp, LOCK_EX)) {
                    fwrite($fp, $data);
                }
                fclose($fp);
                
                // Set filemtime
                touch($filename, time() + $ttl);
            }
        }
        
        /**
        * Reads data
        * 
        * @param string $group Group to store data under
        * @param string $id    Unique ID of this data
        */
        protected static function read($group, $id)
        {
            $filename = self::getFilename($group, $id);
            
            return file_get_contents($filename);
        }
        
        /**
        * Determines if an entry is cached
        * 
        * @param string $group Group to store data under
        * @param string $id    Unique ID of this data
        */
        protected static function isCached($group, $id)
        {
            $filename = self::getFilename($group, $id);

            if (self::$enabled && file_exists($filename) && filemtime($filename) > time()) {
                return true;
            }

            @unlink($filename);

            return false;
        }
        
        /**
        * Builds a filename/path from group, id and
        * store.
        * 
        * @param string $group Group to store data under
        * @param string $id    Unique ID of this data
        */
        protected static function getFilename($group, $id)
        {
            $id = md5($id);

            return self::$store . self::$prefix . "{$group}_{$id}";
        }
        
        /**
        * Sets the filename prefix to use
        * 
        * @param string $prefix Filename Prefix to use
        */
        public static function setPrefix($prefix)
        {
            self::$prefix = $prefix;
        }

        /**
        * Sets the store for cache files. Defaults to
        * /dev/shm. Must have trailing slash.
        * 
        * @param string $store The dir to store the cache data in
        */
        public static function setStore($store)
        {
            self::$store = $store;
        }
    }
    
    /**
    * Output Cache extension of base caching class
    */
    class OutputCache extends Cache
    {
        /**
        * Group of currently being recorded data
        * @var string
        */
        private static $group;
        
        /**
        * ID of currently being recorded data
        * @var string
        */
        private static $id;
        
        /**
        * Ttl of currently being recorded data
        * @var int
        */
        private static $ttl;

        /**
        * Starts caching off. Returns true if cached, and dumps
        * the output. False if not cached and start output buffering.
        * 
        * @param  string $group Group to store data under
        * @param  string $id    Unique ID of this data
        * @param  int    $ttl   How long to cache for (in seconds)
        * @return bool          True if cached, false if not
        */
        public static function Start($group, $id, $ttl)
        {
            if (self::isCached($group, $id)) {
                echo self::read($group, $id);
                return true;
            
            } else {
                
                ob_start();
                
                self::$group = $group;
                self::$id    = $id;
                self::$ttl   = $ttl;
                
                return false;
            }
        }
        
        /**
        * Ends caching. Writes data to disk.
        */
        public static function End()
        {
            $data = ob_get_contents();
            ob_end_flush();
            
            self::write(self::$group, self::$id, self::$ttl, $data);
        }
    }
    
    /**
    * Data cache extension of base caching class
    */
    class DataCache extends Cache
    {
    
        /**
        * Retrieves data from the cache
        * 
        * @param  string $group Group this data belongs to
        * @param  string $id    Unique ID of the data
        * @return mixed         Either the resulting data, or null
        */
        public static function Get($group, $id)
        {
            if (self::isCached($group, $id)) {
                return unserialize(self::read($group, $id));
            }
            
            return null;
        }
        
        /**
        * Stores data in the cache
        * 
        * @param string $group Group this data belongs to
        * @param string $id    Unique ID of the data
        * @param int    $ttl   How long to cache for (in seconds)
        * @param mixed  $data  The data to store
        */
        public static function Put($group, $id, $ttl, $data)
        {
            self::write($group, $id, $ttl, serialize($data));
        }
    }
?>