1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: 18: 19: 20: 21: 22:
23: class cFileCache {
24:
25: 26: 27: 28: 29:
30: protected $_options = array();
31:
32: 33: 34: 35: 36: 37:
38: public function __construct($options = array()) {
39: $this->setOptions($options);
40: }
41:
42: 43: 44: 45: 46: 47: 48: 49:
50: public function setOptions($options) {
51:
52: if (isset($options['cacheDir']) === true && substr($options['cacheDir'], -1) != '/') {
53: $options['cacheDir'] = $options['cacheDir'] . '/';
54: }
55:
56: if (isset($options['cacheDir']) === false) {
57: $options['cacheDir'] = '/tmp/';
58: }
59:
60: if (isset($options['lifeTime']) !== false && isset($options['lifetime']) === false) {
61: $options['lifetime'] = $options['lifeTime'];
62: }
63:
64: if (isset($options['lifetime']) === false) {
65: $options['lifetime'] = 3600;
66: }
67:
68: if (isset($options['fileNamePrefix']) === false) {
69: $options['fileNamePrefix'] = 'cache_';
70: }
71:
72: if (isset($options['fileExtension']) === false) {
73: $options['fileExtension'] = 'tmp';
74: }
75:
76: if (isset($options['fileNameProtection']) === false) {
77: $options['fileNameProtection'] = false;
78: }
79:
80: $this->_options = $options;
81: }
82:
83: 84: 85: 86: 87: 88: 89: 90: 91: 92:
93: public function generateFileName($id, $group = '') {
94: $id = ($this->_options['fileNameProtection'] === true) ? md5($id) : $id;
95: if ($group != '') {
96: $groupName = ($this->_options['fileNameProtection'] === true ? md5($group) : $group) . '_';
97: $group = $groupName . '_';
98: }
99:
100: return $this->_options['fileNamePrefix'] . $group . $id . '.' . $this->_options['fileExtension'];
101: }
102:
103: 104: 105: 106: 107:
108: protected function _validateDirectory() {
109: $directory = $this->_options['cacheDir'];
110: if ($directory == '') {
111: throw new cInvalidArgumentException('The caching directory is empty.');
112: }
113:
114: if (is_dir($directory) === false) {
115: throw new cInvalidArgumentException('The specified caching directory is not a directory.');
116: }
117:
118: if (cFileHandler::writeable($directory) === false) {
119: throw new cInvalidArgumentException('The caching directory is not writable.');
120: }
121: }
122:
123: 124: 125: 126: 127: 128: 129: 130: 131: 132:
133: public function getDestination($id, $group = '') {
134: $this->_validateDirectory();
135:
136: $directory = $this->_options['cacheDir'];
137: $filename = $this->generateFileName($id, $group);
138:
139: return $directory . $filename;
140: }
141:
142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153:
154: public function get($id, $group = '') {
155: $data = false;
156:
157: $destination = $this->getDestination($id, $group);
158:
159: if (cFileHandler::exists($destination) === false) {
160: return false;
161: }
162:
163: $refreshTime = ($this->_options['lifetime'] == 0) ? 0 : time() - (int) $this->_options['lifetime'];
164:
165: clearstatcache();
166: $info = cFileHandler::info($destination);
167: $lastModifyTime = $info['mtime'];
168:
169: if ($lastModifyTime > $refreshTime) {
170: $data = cFileHandler::read($destination);
171: }
172:
173: return $data;
174: }
175:
176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187:
188: public function save($data, $id, $group = '') {
189: return cFileHandler::write($this->getDestination($id, $group), $data);
190: }
191:
192: 193: 194: 195: 196: 197: 198: 199: 200: 201:
202: public function remove($id, $group = '') {
203: $destination = $this->getDestination($id, $group);
204: if (cFileHandler::exists($destination) === false) {
205: return false;
206: }
207:
208: return cFileHandler::remove($this->getDestination($id, $group));
209: }
210:
211: 212: 213: 214: 215: 216: 217: 218:
219: public function generateID($variables) {
220: return md5(serialize($variables));
221: }
222:
223: }
224: