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 1.2 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 1.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

Classes

  • AclBase
  • AclBehavior
  • AclComponent
  • AclNode
  • AclShell
  • Aco
  • AcoAction
  • AjaxHelper
  • ApcEngine
  • ApiShell
  • App
  • AppController
  • AppHelper
  • AppModel
  • Aro
  • AuthComponent
  • BakeShell
  • BehaviorCollection
  • Cache
  • CacheEngine
  • CacheHelper
  • CakeErrorController
  • CakeLog
  • CakeSchema
  • CakeSession
  • CakeSocket
  • ClassRegistry
  • Component
  • Configure
  • ConnectionManager
  • ConsoleShell
  • ContainableBehavior
  • Controller
  • ControllerTask
  • CookieComponent
  • DataSource
  • DbAcl
  • DbAclSchema
  • DbConfigTask
  • DboAdodb
  • DboDb2
  • DboFirebird
  • DboMssql
  • DboMysql
  • DboMysqlBase
  • DboMysqli
  • DboOdbc
  • DboOracle
  • DboPostgres
  • DboSource
  • DboSqlite
  • DboSybase
  • Debugger
  • EmailComponent
  • ErrorHandler
  • ExtractTask
  • File
  • FileEngine
  • Flay
  • Folder
  • FormHelper
  • Helper
  • HtmlHelper
  • HttpSocket
  • I18n
  • I18nModel
  • i18nSchema
  • I18nShell
  • Inflector
  • IniAcl
  • JavascriptHelper
  • JsHelper
  • JsHelperObject
  • L10n
  • MagicDb
  • MagicFileResource
  • MediaView
  • MemcacheEngine
  • Model
  • ModelBehavior
  • ModelTask
  • Multibyte
  • NumberHelper
  • Object
  • Overloadable
  • Overloadable2
  • PagesController
  • PaginatorHelper
  • Permission
  • PluginTask
  • ProjectTask
  • RequestHandlerComponent
  • Router
  • RssHelper
  • Sanitize
  • Scaffold
  • ScaffoldView
  • SchemaShell
  • Security
  • SecurityComponent
  • SessionComponent
  • SessionHelper
  • SessionsSchema
  • Set
  • Shell
  • String
  • TestSuiteShell
  • TestTask
  • TextHelper
  • ThemeView
  • TimeHelper
  • TranslateBehavior
  • TreeBehavior
  • Validation
  • View
  • ViewTask
  • XcacheEngine
  • Xml
  • XmlElement
  • XmlHelper
  • XmlManager
  • XmlNode
  • XmlTextNode

Functions

  • __enclose
  • make_clean_css
  • mb_encode_mimeheader
  • mb_stripos
  • mb_stristr
  • mb_strlen
  • mb_strpos
  • mb_strrchr
  • mb_strrichr
  • mb_strripos
  • mb_strrpos
  • mb_strstr
  • mb_strtolower
  • mb_strtoupper
  • mb_substr
  • mb_substr_count
  • write_css_cache
  1: <?php
  2: /* SVN FILE: $Id$ */
  3: /**
  4:  * Caching for CakePHP.
  5:  *
  6:  *
  7:  * PHP versions 4 and 5
  8:  *
  9:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 10:  * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 11:  *
 12:  * Licensed under The MIT License
 13:  * Redistributions of files must retain the above copyright notice.
 14:  *
 15:  * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 16:  * @link          http://cakephp.org CakePHP(tm) Project
 17:  * @package       cake
 18:  * @subpackage    cake.cake.libs
 19:  * @since         CakePHP(tm) v 1.2.0.4933
 20:  * @version       $Revision$
 21:  * @modifiedby    $LastChangedBy$
 22:  * @lastmodified  $Date$
 23:  * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 24:  */
 25: /**
 26:  * Caching for CakePHP.
 27:  *
 28:  * @package       cake
 29:  * @subpackage    cake.cake.libs
 30:  */
 31: class Cache extends Object {
 32: /**
 33:  * Cache engine to use
 34:  *
 35:  * @var CacheEngine
 36:  * @access protected
 37:  */
 38:     var $_Engine = null;
 39: /**
 40:  * Cache configuration stack
 41:  *
 42:  * @var array
 43:  * @access private
 44:  */
 45:     var $__config = array();
 46: /**
 47:  * Holds name of the current configuration being used
 48:  *
 49:  * @var array
 50:  * @access private
 51:  */
 52:     var $__name = 'default';
 53: /**
 54:  * whether to reset the settings with the next call to self::set();
 55:  *
 56:  * @var array
 57:  * @access private
 58:  */
 59:     var $__reset = false;
 60: /**
 61:  * Returns a singleton instance
 62:  *
 63:  * @return object
 64:  * @access public
 65:  * @static
 66:  */
 67:     function &getInstance() {
 68:         static $instance = array();
 69:         if (!$instance) {
 70:             $instance[0] =& new Cache();
 71:         }
 72:         return $instance[0];
 73:     }
 74: /**
 75:  * Tries to find and include a file for a cache engine and returns object instance
 76:  *
 77:  * @param $name Name of the engine (without 'Engine')
 78:  * @return mixed $engine object or null
 79:  * @access private
 80:  */
 81:     function __loadEngine($name) {
 82:         if (!class_exists($name . 'Engine')) {
 83:             require LIBS . 'cache' . DS . strtolower($name) . '.php';
 84:         }
 85:         return true;
 86:     }
 87: /**
 88:  * Set the cache configuration to use
 89:  *
 90:  * @see app/config/core.php for configuration settings
 91:  * @param string $name Name of the configuration
 92:  * @param array $settings Optional associative array of settings passed to the engine
 93:  * @return array(engine, settings) on success, false on failure
 94:  * @access public
 95:  * @static
 96:  */
 97:     function config($name = null, $settings = array()) {
 98:         $_this =& Cache::getInstance();
 99:         if (is_array($name)) {
100:             $settings = $name;
101:         }
102: 
103:         if ($name === null || !is_string($name)) {
104:             $name = $_this->__name;
105:         }
106: 
107:         $current = array();
108:         if (isset($_this->__config[$name])) {
109:             $current = $_this->__config[$name];
110:         }
111: 
112:         if (!empty($settings)) {
113:             $_this->__name = null;
114:             $_this->__config[$name] = array_merge($current, $settings);
115:         }
116: 
117:         if (empty($_this->__config[$name]['engine'])) {
118:             return false;
119:         }
120: 
121:         $_this->__name = $name;
122:         $engine = $_this->__config[$name]['engine'];
123: 
124:         if (!$_this->isInitialized($engine)) {
125:             if ($_this->engine($engine, $_this->__config[$name]) === false) {
126:                 return false;
127:             }
128:             $settings = $_this->__config[$name] = $_this->settings($engine);
129:         } else {
130:             $settings = $_this->__config[$name] = $_this->set($_this->__config[$name]);
131:         }
132:         return compact('engine', 'settings');
133:     }
134: /**
135:  * Set the cache engine to use or modify settings for one instance
136:  *
137:  * @param string $name Name of the engine (without 'Engine')
138:  * @param array $settings Optional associative array of settings passed to the engine
139:  * @return boolean True on success, false on failure
140:  * @access public
141:  * @static
142:  */
143:     function engine($name = 'File', $settings = array()) {
144:         $cacheClass = $name . 'Engine';
145:         $_this =& Cache::getInstance();
146:         if (!isset($_this->_Engine[$name])) {
147:             if ($_this->__loadEngine($name) === false) {
148:                 return false;
149:             }
150:             $_this->_Engine[$name] =& new $cacheClass();
151:         }
152: 
153:         if ($_this->_Engine[$name]->init($settings)) {
154:             if (time() % $_this->_Engine[$name]->settings['probability'] === 0) {
155:                 $_this->_Engine[$name]->gc();
156:             }
157:             return true;
158:         }
159:         $_this->_Engine[$name] = null;
160:         return false;
161:     }
162: /**
163:  * Temporarily change settings to current config options. if no params are passed, resets settings if needed
164:  * Cache::write() will reset the configuration changes made
165:  *
166:  * @param mixed $settings Optional string for simple name-value pair or array
167:  * @param string $value Optional for a simple name-value pair
168:  * @return array of settings
169:  * @access public
170:  * @static
171:  */
172:     function set($settings = array(), $value = null) {
173:         $_this =& Cache::getInstance();
174:         if (!isset($_this->__config[$_this->__name])) {
175:             return false;
176:         }
177: 
178:         $engine = $_this->__config[$_this->__name]['engine'];   
179:         if (isset($settings['engine'])) {
180:             $engine = $settings['engine'];
181:         }
182: 
183:         if (!empty($settings)) {
184:             $_this->__reset = true;
185:         }
186: 
187:         if ($_this->__reset === true) {
188:             if (empty($settings)) {
189:                 $_this->__reset = false;
190:                 $settings = $_this->__config[$_this->__name];
191:             } else {
192:                 if (is_string($settings) && $value !== null) {
193:                     $settings = array($settings => $value);
194:                 }
195:                 $settings = array_merge($_this->__config[$_this->__name], $settings);
196:             }
197:             $_this->_Engine[$engine]->init($settings);
198:         }
199: 
200:         return $_this->settings($engine);
201:     }
202: /**
203:  * Garbage collection
204:  *
205:  * Permanently remove all expired and deleted data
206:  *
207:  * @return void
208:  * @access public
209:  * @static
210:  */
211:     function gc() {
212:         $_this =& Cache::getInstance();
213:         $config = $_this->config();
214:         $_this->_Engine[$config['engine']]->gc();
215:     }
216: /**
217:  * Write data for key into cache
218:  *
219:  * @param string $key Identifier for the data
220:  * @param mixed $value Data to be cached - anything except a resource
221:  * @param string $config Optional - string configuration name
222:  * @return boolean True if the data was successfully cached, false on failure
223:  * @access public
224:  * @static
225:  */
226:     function write($key, $value, $config = null) {
227:         $_this =& Cache::getInstance();
228: 
229:         if (is_array($config)) {
230:             extract($config);
231:         } else if ($config && (is_numeric($config) || is_numeric($config[0]) || (isset($config[1]) && is_numeric($config[1])))) {
232:             $config = null;
233:         }
234: 
235:         if ($config && isset($_this->__config[$config])) {
236:             $settings = $_this->set($_this->__config[$config]);
237:         } else {
238:             $settings = $_this->settings();
239:         }
240: 
241:         if (empty($settings)) {
242:             return null;
243:         }
244:         extract($settings);
245: 
246:         if (!$_this->isInitialized($engine)) {
247:             return false;
248:         }
249: 
250:         if (!$key = $_this->_Engine[$engine]->key($key)) {
251:             return false;
252:         }
253: 
254:         if (is_resource($value)) {
255:             return false;
256:         }
257: 
258:         if ($duration < 1) {
259:             return false;
260:         }
261: 
262:         $success = $_this->_Engine[$engine]->write($settings['prefix'] . $key, $value, $duration);
263:         $settings = $_this->set();
264:         return $success;
265:     }
266: /**
267:  * Read a key from the cache
268:  *
269:  * @param string $key Identifier for the data
270:  * @param string $config name of the configuration to use
271:  * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
272:  * @access public
273:  * @static
274:  */
275:     function read($key, $config = null) {
276:         $_this =& Cache::getInstance();
277: 
278:         if (isset($_this->__config[$config])) {
279:             $settings = $_this->set($_this->__config[$config]);
280:         } else {
281:             $settings = $_this->settings();
282:         }
283: 
284:         if (empty($settings)) {
285:             return null;
286:         }
287:         extract($settings);
288: 
289:         if (!$_this->isInitialized($engine)) {
290:             return false;
291:         }
292:         if (!$key = $_this->_Engine[$engine]->key($key)) {
293:             return false;
294:         }
295:     
296:         $success = $_this->_Engine[$engine]->read($settings['prefix'] . $key);
297: 
298:         if ($config !== null && $config !== $_this->__name) {
299:             $settings = $_this->set();
300:         }
301:         return $success;
302:     }
303: /**
304:  * Delete a key from the cache
305:  *
306:  * @param string $key Identifier for the data
307:  * @param string $config name of the configuration to use
308:  * @return boolean True if the value was succesfully deleted, false if it didn't exist or couldn't be removed
309:  * @access public
310:  * @static
311:  */
312:     function delete($key, $config = null) {
313:         $_this =& Cache::getInstance();
314:         if (isset($_this->__config[$config])) {
315:             $settings = $_this->set($_this->__config[$config]);
316:         } else {
317:             $settings = $_this->settings();
318:         }
319: 
320:         if (empty($settings)) {
321:             return null;
322:         }
323:         extract($settings);
324: 
325:         if (!$_this->isInitialized($engine)) {
326:             return false;
327:         }
328: 
329:         if (!$key = $_this->_Engine[$engine]->key($key)) {
330:             return false;
331:         }
332: 
333:         $success = $_this->_Engine[$engine]->delete($settings['prefix'] . $key);
334:         $settings = $_this->set();
335:         return $success;
336:     }
337: /**
338:  * Delete all keys from the cache
339:  *
340:  * @param boolean $check if true will check expiration, otherwise delete all
341:  * @param string $config name of the configuration to use
342:  * @return boolean True if the cache was succesfully cleared, false otherwise
343:  * @access public
344:  * @static
345:  */
346:     function clear($check = false, $config = null) {
347:         $_this =& Cache::getInstance();
348:         if (isset($_this->__config[$config])) {
349:             $settings = $_this->set($_this->__config[$config]);
350:         } else {
351:             $settings = $_this->settings();
352:         }
353: 
354:         if (empty($settings)) {
355:             return null;
356:         }
357:         extract($settings);
358: 
359:         if (isset($engine) && !$_this->isInitialized($engine)) {
360:             return false;
361:         }
362:         $success = $_this->_Engine[$engine]->clear($check);
363:         $settings = $_this->set();
364:         return $success;
365:     }
366: /**
367:  * Check if Cache has initialized a working storage engine
368:  *
369:  * @param string $engine Name of the engine
370:  * @param string $config Name of the configuration setting
371:  * @return bool
372:  * @access public
373:  * @static
374:  */
375:     function isInitialized($engine = null) {
376:         if (Configure::read('Cache.disable')) {
377:             return false;
378:         }
379:         $_this =& Cache::getInstance();
380:         if (!$engine && isset($_this->__config[$_this->__name]['engine'])) {
381:             $engine = $_this->__config[$_this->__name]['engine'];
382:         }
383:         return isset($_this->_Engine[$engine]);
384:     }
385: 
386: /**
387:  * Return the settings for current cache engine
388:  *
389:  * @param string $engine Name of the engine
390:  * @return array list of settings for this engine
391:  * @access public
392:  * @static
393:  */
394:     function settings($engine = null) {
395:         $_this =& Cache::getInstance();
396:         if (!$engine && isset($_this->__config[$_this->__name]['engine'])) {
397:             $engine = $_this->__config[$_this->__name]['engine'];
398:         }
399: 
400:         if (isset($_this->_Engine[$engine]) && !is_null($_this->_Engine[$engine])) {
401:             return $_this->_Engine[$engine]->settings();
402:         }
403:         return array();
404:     }
405: }
406: /**
407:  * Storage engine for CakePHP caching
408:  *
409:  * @package       cake
410:  * @subpackage    cake.cake.libs
411:  */
412: class CacheEngine extends Object {
413: /**
414:  * settings of current engine instance
415:  *
416:  * @var int
417:  * @access public
418:  */
419:     var $settings = array();
420: /**
421:  * Iitialize the cache engine
422:  *
423:  * Called automatically by the cache frontend
424:  *
425:  * @param array $params Associative array of parameters for the engine
426:  * @return boolean True if the engine has been succesfully initialized, false if not
427:  * @access public
428:  */
429:     function init($settings = array()) {
430:         $this->settings = array_merge(array('prefix' => 'cake_', 'duration'=> 3600, 'probability'=> 100), $this->settings, $settings);
431:         if (!is_numeric($this->settings['duration'])) {
432:             $this->settings['duration'] = strtotime($this->settings['duration']) - time();
433:         }
434:         return true;
435:     }
436: /**
437:  * Garbage collection
438:  *
439:  * Permanently remove all expired and deleted data
440:  *
441:  * @access public
442:  */
443:     function gc() {
444:     }
445: /**
446:  * Write value for a key into cache
447:  *
448:  * @param string $key Identifier for the data
449:  * @param mixed $value Data to be cached
450:  * @param mixed $duration How long to cache the data, in seconds
451:  * @return boolean True if the data was succesfully cached, false on failure
452:  * @access public
453:  */
454:     function write($key, &$value, $duration) {
455:         trigger_error(sprintf(__('Method write() not implemented in %s', true), get_class($this)), E_USER_ERROR);
456:     }
457: /**
458:  * Read a key from the cache
459:  *
460:  * @param string $key Identifier for the data
461:  * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
462:  * @access public
463:  */
464:     function read($key) {
465:         trigger_error(sprintf(__('Method read() not implemented in %s', true), get_class($this)), E_USER_ERROR);
466:     }
467: /**
468:  * Delete a key from the cache
469:  *
470:  * @param string $key Identifier for the data
471:  * @return boolean True if the value was succesfully deleted, false if it didn't exist or couldn't be removed
472:  * @access public
473:  */
474:     function delete($key) {
475:     }
476: /**
477:  * Delete all keys from the cache
478:  *
479:  * @param boolean $check if true will check expiration, otherwise delete all
480:  * @return boolean True if the cache was succesfully cleared, false otherwise
481:  * @access public
482:  */
483:     function clear($check) {
484:     }
485: /**
486:  * Cache Engine settings
487:  *
488:  * @return array settings
489:  * @access public
490:  */
491:     function settings() {
492:         return $this->settings;
493:     }
494: /**
495:  * generates a safe key
496:  *
497:  * @param string $key the key passed over
498:  * @return mixed string $key or false
499:  * @access public
500:  */
501:     function key($key) {
502:         if (empty($key)) {
503:             return false;
504:         }
505:         $key = Inflector::underscore(str_replace(array(DS, '/', '.'), '_', strval($key)));
506:         return $key;
507:     }
508: }
509: ?>
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