1: <?php
2: /**
3: * Wincache storage engine for cache.
4: *
5: * Supports wincache 1.1.0 and higher.
6: *
7: * PHP 5
8: *
9: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
10: * Copyright 2005-2011, 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-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
16: * @link http://cakephp.org CakePHP(tm) Project
17: * @package Cake.Cache.Engine
18: * @since CakePHP(tm) v 1.2.0.4933
19: * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
20: */
21:
22: /**
23: * Wincache storage engine for cache
24: *
25: * @package Cake.Cache.Engine
26: */
27: class WincacheEngine extends CacheEngine {
28:
29: /**
30: * Initialize the Cache Engine
31: *
32: * Called automatically by the cache frontend
33: * To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
34: *
35: * @param array $settings array of setting for the engine
36: * @return boolean True if the engine has been successfully initialized, false if not
37: * @see CacheEngine::__defaults
38: */
39: public function init($settings = array()) {
40: parent::init(array_merge(array(
41: 'engine' => 'Wincache',
42: 'prefix' => Inflector::slug(APP_DIR) . '_'),
43: $settings));
44: return function_exists('wincache_ucache_info');
45: }
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 successfully cached, false on failure
54: */
55: public function write($key, $value, $duration) {
56: $expires = time() + $duration;
57:
58: $data = array(
59: $key . '_expires' => $expires,
60: $key => $value
61: );
62: $result = wincache_ucache_set($data, null, $duration);
63: return empty($result);
64: }
65:
66: /**
67: * Read a key from the cache
68: *
69: * @param string $key Identifier for the data
70: * @return mixed The cached data, or false if the data doesn't exist, has expired, or if
71: * there was an error fetching it
72: */
73: public function read($key) {
74: $time = time();
75: $cachetime = intval(wincache_ucache_get($key . '_expires'));
76: if ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime) {
77: return false;
78: }
79: return wincache_ucache_get($key);
80: }
81:
82: /**
83: * Increments the value of an integer cached key
84: *
85: * @param string $key Identifier for the data
86: * @param integer $offset How much to increment
87: * @return New incremented value, false otherwise
88: */
89: public function increment($key, $offset = 1) {
90: return wincache_ucache_inc($key, $offset);
91: }
92:
93: /**
94: * Decrements the value of an integer cached key
95: *
96: * @param string $key Identifier for the data
97: * @param integer $offset How much to subtract
98: * @return New decremented value, false otherwise
99: */
100: public function decrement($key, $offset = 1) {
101: return wincache_ucache_dec($key, $offset);
102: }
103:
104: /**
105: * Delete a key from the cache
106: *
107: * @param string $key Identifier for the data
108: * @return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed
109: */
110: public function delete($key) {
111: return wincache_ucache_delete($key);
112: }
113:
114: /**
115: * Delete all keys from the cache. This will clear every
116: * item in the cache matching the cache config prefix.
117: *
118: * @param boolean $check If true, nothing will be cleared, as entries will
119: * naturally expire in wincache..
120: * @return boolean True Returns true.
121: */
122: public function clear($check) {
123: if ($check) {
124: return true;
125: }
126: $info = wincache_ucache_info();
127: $cacheKeys = $info['ucache_entries'];
128: unset($info);
129: foreach ($cacheKeys as $key) {
130: if (strpos($key['key_name'], $this->settings['prefix']) === 0) {
131: wincache_ucache_delete($key['key_name']);
132: }
133: }
134: return true;
135: }
136:
137: }
138: