1: <?php
2: /* SVN FILE: $Id$ */
3: /**
4: * APC storage engine for cache.
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.cache
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: * APC storage engine for cache
27: *
28: * @package cake
29: * @subpackage cake.cake.libs.cache
30: */
31: class ApcEngine extends CacheEngine {
32: /**
33: * Initialize the Cache Engine
34: *
35: * Called automatically by the cache frontend
36: * To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
37: *
38: * @param array $setting array of setting for the engine
39: * @return boolean True if the engine has been successfully initialized, false if not
40: * @see CacheEngine::__defaults
41: * @access public
42: */
43: function init($settings = array()) {
44: parent::init(array_merge(array('engine' => 'Apc', 'prefix' => Inflector::slug(APP_DIR) . '_'), $settings));
45: return function_exists('apc_cache_info');
46: }
47: /**
48: * Write data for key into cache
49: *
50: * @param string $key Identifier for the data
51: * @param mixed $value Data to be cached
52: * @param integer $duration How long to cache the data, in seconds
53: * @return boolean True if the data was succesfully cached, false on failure
54: * @access public
55: */
56: function write($key, &$value, $duration) {
57: $expires = time() + $duration;
58: apc_store($key.'_expires', $expires, $duration);
59: return apc_store($key, $value, $duration);
60: }
61: /**
62: * Read a key from the cache
63: *
64: * @param string $key Identifier for the data
65: * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
66: * @access public
67: */
68: function read($key) {
69: $time = time();
70: $cachetime = intval(apc_fetch($key.'_expires'));
71: if ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime) {
72: return false;
73: }
74: return apc_fetch($key);
75: }
76: /**
77: * Delete a key from the cache
78: *
79: * @param string $key Identifier for the data
80: * @return boolean True if the value was succesfully deleted, false if it didn't exist or couldn't be removed
81: * @access public
82: */
83: function delete($key) {
84: return apc_delete($key);
85: }
86: /**
87: * Delete all keys from the cache
88: *
89: * @return boolean True if the cache was succesfully cleared, false otherwise
90: * @access public
91: */
92: function clear() {
93: return apc_clear_cache('user');
94: }
95: }
96: ?>