CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.2 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.2
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
      • Validator
    • Network
      • Email
      • Http
    • Routing
      • Filter
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • ApcEngine
  • FileEngine
  • MemcacheEngine
  • RedisEngine
  • WincacheEngine
  • XcacheEngine
  1: <?php
  2: /**
  3:  * File Storage engine for cache.  Filestorage is the slowest cache storage
  4:  * to read and write.  However, it is good for servers that don't have other storage
  5:  * engine available, or have content which is not performance sensitive.
  6:  *
  7:  * You can configure a FileEngine cache, using Cache::config()
  8:  *
  9:  * PHP 5
 10:  *
 11:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 12:  * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 13:  *
 14:  * Licensed under The MIT License
 15:  * Redistributions of files must retain the above copyright notice.
 16:  *
 17:  * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 18:  * @link          http://cakephp.org CakePHP(tm) Project
 19:  * @since         CakePHP(tm) v 1.2.0.4933
 20:  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 21:  */
 22: 
 23: /**
 24:  * File Storage engine for cache.  Filestorage is the slowest cache storage
 25:  * to read and write.  However, it is good for servers that don't have other storage
 26:  * engine available, or have content which is not performance sensitive.
 27:  *
 28:  * You can configure a FileEngine cache, using Cache::config()
 29:  *
 30:  * @package       Cake.Cache.Engine
 31:  */
 32: class FileEngine extends CacheEngine {
 33: 
 34: /**
 35:  * Instance of SplFileObject class
 36:  *
 37:  * @var File
 38:  */
 39:     protected $_File = null;
 40: 
 41: /**
 42:  * Settings
 43:  *
 44:  * - path = absolute path to cache directory, default => CACHE
 45:  * - prefix = string prefix for filename, default => cake_
 46:  * - lock = enable file locking on write, default => false
 47:  * - serialize = serialize the data, default => true
 48:  *
 49:  * @var array
 50:  * @see CacheEngine::__defaults
 51:  */
 52:     public $settings = array();
 53: 
 54: /**
 55:  * True unless FileEngine::__active(); fails
 56:  *
 57:  * @var boolean
 58:  */
 59:     protected $_init = true;
 60: 
 61: /**
 62:  * Initialize the Cache Engine
 63:  *
 64:  * Called automatically by the cache frontend
 65:  * To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
 66:  *
 67:  * @param array $settings array of setting for the engine
 68:  * @return boolean True if the engine has been successfully initialized, false if not
 69:  */
 70:     public function init($settings = array()) {
 71:         $settings += array(
 72:             'engine' => 'File',
 73:             'path' => CACHE,
 74:             'prefix' => 'cake_',
 75:             'lock' => true,
 76:             'serialize' => true,
 77:             'isWindows' => false,
 78:             'mask' => 0664
 79:         );
 80:         parent::init($settings);
 81: 
 82:         if (DS === '\\') {
 83:             $this->settings['isWindows'] = true;
 84:         }
 85:         if (substr($this->settings['path'], -1) !== DS) {
 86:             $this->settings['path'] .= DS;
 87:         }
 88:         if (!empty($this->_groupPrefix)) {
 89:             $this->_groupPrefix = str_replace('_', DS, $this->_groupPrefix);
 90:         }
 91:         return $this->_active();
 92:     }
 93: 
 94: /**
 95:  * Garbage collection. Permanently remove all expired and deleted data
 96:  *
 97:  * @param integer $expires [optional] An expires timestamp, invalidataing all data before.
 98:  * @return boolean True if garbage collection was successful, false on failure
 99:  */
100:     public function gc($expires = null) {
101:         return $this->clear(true);
102:     }
103: 
104: /**
105:  * Write data for key into cache
106:  *
107:  * @param string $key Identifier for the data
108:  * @param mixed $data Data to be cached
109:  * @param integer $duration How long to cache the data, in seconds
110:  * @return boolean True if the data was successfully cached, false on failure
111:  */
112:     public function write($key, $data, $duration) {
113:         if ($data === '' || !$this->_init) {
114:             return false;
115:         }
116: 
117:         if ($this->_setKey($key, true) === false) {
118:             return false;
119:         }
120: 
121:         $lineBreak = "\n";
122: 
123:         if ($this->settings['isWindows']) {
124:             $lineBreak = "\r\n";
125:         }
126: 
127:         if (!empty($this->settings['serialize'])) {
128:             if ($this->settings['isWindows']) {
129:                 $data = str_replace('\\', '\\\\\\\\', serialize($data));
130:             } else {
131:                 $data = serialize($data);
132:             }
133:         }
134: 
135:         $expires = time() + $duration;
136:         $contents = $expires . $lineBreak . $data . $lineBreak;
137: 
138:         if ($this->settings['lock']) {
139:             $this->_File->flock(LOCK_EX);
140:         }
141: 
142:         $this->_File->rewind();
143:         $success = $this->_File->ftruncate(0) && $this->_File->fwrite($contents) && $this->_File->fflush();
144: 
145:         if ($this->settings['lock']) {
146:             $this->_File->flock(LOCK_UN);
147:         }
148: 
149:         return $success;
150:     }
151: 
152: /**
153:  * Read a key from the cache
154:  *
155:  * @param string $key Identifier for the data
156:  * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
157:  */
158:     public function read($key) {
159:         if (!$this->_init || $this->_setKey($key) === false) {
160:             return false;
161:         }
162: 
163:         if ($this->settings['lock']) {
164:             $this->_File->flock(LOCK_SH);
165:         }
166: 
167:         $this->_File->rewind();
168:         $time = time();
169:         $cachetime = intval($this->_File->current());
170: 
171:         if ($cachetime !== false && ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime)) {
172:             if ($this->settings['lock']) {
173:                 $this->_File->flock(LOCK_UN);
174:             }
175:             return false;
176:         }
177: 
178:         $data = '';
179:         $this->_File->next();
180:         while ($this->_File->valid()) {
181:             $data .= $this->_File->current();
182:             $this->_File->next();
183:         }
184: 
185:         if ($this->settings['lock']) {
186:             $this->_File->flock(LOCK_UN);
187:         }
188: 
189:         $data = trim($data);
190: 
191:         if ($data !== '' && !empty($this->settings['serialize'])) {
192:             if ($this->settings['isWindows']) {
193:                 $data = str_replace('\\\\\\\\', '\\', $data);
194:             }
195:             $data = unserialize((string)$data);
196:         }
197:         return $data;
198:     }
199: 
200: /**
201:  * Delete a key from the cache
202:  *
203:  * @param string $key Identifier for the data
204:  * @return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed
205:  */
206:     public function delete($key) {
207:         if ($this->_setKey($key) === false || !$this->_init) {
208:             return false;
209:         }
210:         $path = $this->_File->getRealPath();
211:         $this->_File = null;
212:         return unlink($path);
213:     }
214: 
215: /**
216:  * Delete all values from the cache
217:  *
218:  * @param boolean $check Optional - only delete expired cache items
219:  * @return boolean True if the cache was successfully cleared, false otherwise
220:  */
221:     public function clear($check) {
222:         if (!$this->_init) {
223:             return false;
224:         }
225:         $dir = dir($this->settings['path']);
226:         if ($check) {
227:             $now = time();
228:             $threshold = $now - $this->settings['duration'];
229:         }
230:         $prefixLength = strlen($this->settings['prefix']);
231:         while (($entry = $dir->read()) !== false) {
232:             if (substr($entry, 0, $prefixLength) !== $this->settings['prefix']) {
233:                 continue;
234:             }
235:             if ($this->_setKey($entry) === false) {
236:                 continue;
237:             }
238:             if ($check) {
239:                 $mtime = $this->_File->getMTime();
240: 
241:                 if ($mtime > $threshold) {
242:                     continue;
243:                 }
244: 
245:                 $expires = (int)$this->_File->current();
246: 
247:                 if ($expires > $now) {
248:                     continue;
249:                 }
250:             }
251:             $path = $this->_File->getRealPath();
252:             $this->_File = null;
253:             if (file_exists($path)) {
254:                 unlink($path);
255:             }
256:         }
257:         $dir->close();
258:         return true;
259:     }
260: 
261: /**
262:  * Not implemented
263:  *
264:  * @param string $key
265:  * @param integer $offset
266:  * @return void
267:  * @throws CacheException
268:  */
269:     public function decrement($key, $offset = 1) {
270:         throw new CacheException(__d('cake_dev', 'Files cannot be atomically decremented.'));
271:     }
272: 
273: /**
274:  * Not implemented
275:  *
276:  * @param string $key
277:  * @param integer $offset
278:  * @return void
279:  * @throws CacheException
280:  */
281:     public function increment($key, $offset = 1) {
282:         throw new CacheException(__d('cake_dev', 'Files cannot be atomically incremented.'));
283:     }
284: 
285: /**
286:  * Sets the current cache key this class is managing, and creates a writable SplFileObject
287:  * for the cache file the key is referring to.
288:  *
289:  * @param string $key The key
290:  * @param boolean $createKey Whether the key should be created if it doesn't exists, or not
291:  * @return boolean true if the cache key could be set, false otherwise
292:  */
293:     protected function _setKey($key, $createKey = false) {
294:         $groups = null;
295:         if (!empty($this->_groupPrefix)) {
296:             $groups = vsprintf($this->_groupPrefix, $this->groups());
297:         }
298:         $dir = $this->settings['path'] . $groups;
299: 
300:         if (!is_dir($dir)) {
301:             mkdir($dir, 0777, true);
302:         }
303:         $path = new SplFileInfo($dir . $key);
304: 
305:         if (!$createKey && !$path->isFile()) {
306:             return false;
307:         }
308:         if (empty($this->_File) || $this->_File->getBaseName() !== $key) {
309:             $exists = file_exists($path->getPathname());
310:             try {
311:                 $this->_File = $path->openFile('c+');
312:             } catch (Exception $e) {
313:                 trigger_error($e->getMessage(), E_USER_WARNING);
314:                 return false;
315:             }
316:             unset($path);
317: 
318:             if (!$exists && !chmod($this->_File->getPathname(), (int)$this->settings['mask'])) {
319:                 trigger_error(__d(
320:                     'cake_dev', 'Could not apply permission mask "%s" on cache file "%s"',
321:                     array($this->_File->getPathname(), $this->settings['mask'])), E_USER_WARNING);
322:             }
323:         }
324:         return true;
325:     }
326: 
327: /**
328:  * Determine is cache directory is writable
329:  *
330:  * @return boolean
331:  */
332:     protected function _active() {
333:         $dir = new SplFileInfo($this->settings['path']);
334:         if ($this->_init && !($dir->isDir() && $dir->isWritable())) {
335:             $this->_init = false;
336:             trigger_error(__d('cake_dev', '%s is not writable', $this->settings['path']), E_USER_WARNING);
337:             return false;
338:         }
339:         return true;
340:     }
341: 
342: /**
343:  * Generates a safe key for use with cache engine storage engines.
344:  *
345:  * @param string $key the key passed over
346:  * @return mixed string $key or false
347:  */
348:     public function key($key) {
349:         if (empty($key)) {
350:             return false;
351:         }
352: 
353:         $key = Inflector::underscore(str_replace(array(DS, '/', '.'), '_', strval($key)));
354:         return $key;
355:     }
356: 
357: /**
358:  * Recursively deletes all files under any directory named as $group
359:  *
360:  * @return boolean success
361:  **/
362:     public function clearGroup($group) {
363:         $directoryIterator = new RecursiveDirectoryIterator($this->settings['path']);
364:         $contents = new RecursiveIteratorIterator($directoryIterator, RecursiveIteratorIterator::CHILD_FIRST);
365:         foreach ($contents as $object) {
366:             $containsGroup = strpos($object->getPathName(), DS . $group . DS) !== false;
367:             if ($object->isFile() && $containsGroup) {
368:                 unlink($object->getPathName());
369:             }
370:         }
371:         return true;
372:     }
373: }
374: 
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs