1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This class extends Cache_Lite and offers a form of setting expiring time in a |
5
|
|
|
* per file basis. |
6
|
|
|
* |
7
|
|
|
* This works by setting the file time to 'now + expiration time'. This avoids |
8
|
|
|
* having to read and write data from a separate database or file, or to saving |
9
|
|
|
* metadata in the header of the cached data. |
10
|
|
|
* |
11
|
|
|
* Some filesystems may be troublesome. FAT and NTFS systems seem to be accurate |
12
|
|
|
* to 2 seconds, and this module has not been tested on NFS or SAMBA filesystems. |
13
|
|
|
* |
14
|
|
|
* @package Cache_Lite |
15
|
|
|
* @author Bruno Barberi Gnecco <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
require_once('Lite.php'); |
19
|
|
|
|
20
|
|
|
class Cache_Lite_Timed extends Cache_Lite |
21
|
|
|
{ |
22
|
|
|
var $_bufferedLifetime; |
23
|
|
|
|
24
|
|
|
// --- Public methods ---- |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Constructor |
28
|
|
|
* |
29
|
|
|
* $options is an assoc. To have a look at availables options, |
30
|
|
|
* see the constructor of the Cache_Lite class in 'Cache_Lite.php' |
31
|
|
|
* |
32
|
|
|
* @param array $options options |
33
|
|
|
* @access public |
34
|
|
|
*/ |
35
|
|
|
function __construct($options = array(NULL)) |
36
|
|
|
{ |
37
|
|
|
parent::__construct($options); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Save some data in a cache file |
42
|
|
|
* |
43
|
|
|
* @param string $data data to put in cache (can be another type than strings if automaticSerialization is on) |
44
|
|
|
* @param string $id cache id |
45
|
|
|
* @param string $group name of the cache group |
46
|
|
|
* @param int $lifetime The time in seconds that this entry should live. Defaults to the lifetime |
47
|
|
|
* set by the constructor. |
48
|
|
|
* @return boolean true if no problem (else : false or a PEAR_Error object) |
49
|
|
|
* @access public |
50
|
|
|
*/ |
51
|
|
|
function save($data, $id = NULL, $group = 'default', $lifetime = null) |
52
|
|
|
{ |
53
|
|
|
$res = parent::save($data, $id, $group); |
54
|
|
|
if ($res === true) { |
55
|
|
|
if ($lifetime == null) { |
56
|
|
|
$lifetime = $this->_bufferedLifetime; |
57
|
|
|
} |
58
|
|
|
if ($lifetime == null) { |
59
|
|
|
$lifetime = $this->_lifeTime; |
60
|
|
|
} |
61
|
|
|
$res = $this->_setLastModified(time() + $lifetime); |
62
|
|
|
if (is_object($res)) { |
63
|
|
|
// $res is a PEAR_Error object |
64
|
|
|
if (!($this->_errorHandlingAPIBreak)) { |
65
|
|
|
return false; // we return false (old API) |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
return $res; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Sets the ctime/mtime status for a file for the given time. |
74
|
|
|
* |
75
|
|
|
* @param integer $time Unix timestamp |
76
|
|
|
* @return boolean |
77
|
|
|
*/ |
78
|
|
|
function _setLastModified($time) { |
79
|
|
|
if (@touch($this->_file, $time, $time) === false) { |
80
|
|
|
return $this->raiseError('Cache_Lite : Unable to write cache file : '.$this->_file, -1); |
81
|
|
|
} |
82
|
|
|
return true; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Override refresh time function. Returns current time. |
87
|
|
|
* |
88
|
|
|
*/ |
89
|
|
|
function _setRefreshTime() { |
90
|
|
|
if (is_null($this->_lifeTime)) { |
91
|
|
|
$this->_refreshTime = null; |
92
|
|
|
} else { |
93
|
|
|
$this->_refreshTime = time(); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|