Skip to content
This repository
Browse code

Merge branch '2.0' of github.com:cakephp/cakephp into 2.0

  • Loading branch information...
commit ff34137d387bf57be545abb991d2a91252a4183e 2 parents d0ef655 + 9e4c367
José Lorenzo Rodríguez authored June 21, 2011
124  lib/Cake/Cache/Engine/WincacheEngine.php
... ...
@@ -0,0 +1,124 @@
  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.libs.cache
  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.libs.cache
  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 cache value stored 
  116
+ * in wincache.
  117
+ *
  118
+ * @return boolean True if the cache was successfully cleared, false otherwise
  119
+ */
  120
+	public function clear($check) {
  121
+		return wincache_ucache_clear();
  122
+	}
  123
+
  124
+}
198  lib/Cake/Test/Case/Cache/Engine/WincacheEngineTest.php
... ...
@@ -0,0 +1,198 @@
  1
+<?php
  2
+/**
  3
+ * WincacheEngineTest file
  4
+ *
  5
+ * PHP 5
  6
+ *
  7
+ * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8
+ * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9
+ *
  10
+ * Licensed under The MIT License
  11
+ * Redistributions of files must retain the above copyright notice
  12
+ *
  13
+ * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14
+ * @link          http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15
+ * @package       cake.tests.cases.libs.cache
  16
+ * @since         CakePHP(tm) v 1.2.0.5434
  17
+ * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
  18
+ */
  19
+
  20
+App::uses('Cache', 'Cache');
  21
+
  22
+/**
  23
+ * WincacheEngineTest class
  24
+ *
  25
+ * @package       cake.tests.cases.libs.cache
  26
+ */
  27
+class WincacheEngineTest extends CakeTestCase {
  28
+
  29
+/**
  30
+ * setUp method
  31
+ *
  32
+ * @access public
  33
+ * @return void
  34
+ */
  35
+	public function setUp() {
  36
+		$this->skipIf(!function_exists('wincache_ucache_set'), 'Wincache is not installed or configured properly.');
  37
+		$this->_cacheDisable = Configure::read('Cache.disable');
  38
+		Configure::write('Cache.disable', false);
  39
+		Cache::config('wincache', array('engine' => 'Wincache', 'prefix' => 'cake_'));
  40
+	}
  41
+
  42
+/**
  43
+ * tearDown method
  44
+ *
  45
+ * @access public
  46
+ * @return void
  47
+ */
  48
+	public function tearDown() {
  49
+		Configure::write('Cache.disable', $this->_cacheDisable);
  50
+		Cache::drop('wincache');
  51
+		Cache::config('default');
  52
+	}
  53
+
  54
+/**
  55
+ * testReadAndWriteCache method
  56
+ *
  57
+ * @access public
  58
+ * @return void
  59
+ */
  60
+	public function testReadAndWriteCache() {
  61
+		Cache::set(array('duration' => 1), 'wincache');
  62
+
  63
+		$result = Cache::read('test', 'wincache');
  64
+		$expecting = '';
  65
+		$this->assertEqual($result, $expecting);
  66
+
  67
+		$data = 'this is a test of the emergency broadcasting system';
  68
+		$result = Cache::write('test', $data, 'wincache');
  69
+		var_dump(wincache_ucache_get('cake_test'));
  70
+		$this->assertTrue($result);
  71
+
  72
+		$result = Cache::read('test', 'wincache');
  73
+		$expecting = $data;
  74
+		$this->assertEqual($result, $expecting);
  75
+
  76
+		Cache::delete('test', 'wincache');
  77
+	}
  78
+
  79
+/**
  80
+ * testExpiry method
  81
+ *
  82
+ * @access public
  83
+ * @return void
  84
+ */
  85
+	public function testExpiry() {
  86
+		Cache::set(array('duration' => 1), 'wincache');
  87
+
  88
+		$result = Cache::read('test', 'wincache');
  89
+		$this->assertFalse($result);
  90
+
  91
+		$data = 'this is a test of the emergency broadcasting system';
  92
+		$result = Cache::write('other_test', $data, 'wincache');
  93
+		$this->assertTrue($result);
  94
+
  95
+		sleep(2);
  96
+		$result = Cache::read('other_test', 'wincache');
  97
+		$this->assertFalse($result);
  98
+
  99
+		Cache::set(array('duration' =>  1), 'wincache');
  100
+
  101
+		$data = 'this is a test of the emergency broadcasting system';
  102
+		$result = Cache::write('other_test', $data, 'wincache');
  103
+		$this->assertTrue($result);
  104
+
  105
+		sleep(2);
  106
+		$result = Cache::read('other_test', 'wincache');
  107
+		$this->assertFalse($result);
  108
+
  109
+		sleep(2);
  110
+		$result = Cache::read('other_test', 'wincache');
  111
+		$this->assertFalse($result);
  112
+	}
  113
+
  114
+/**
  115
+ * testDeleteCache method
  116
+ *
  117
+ * @access public
  118
+ * @return void
  119
+ */
  120
+	public function testDeleteCache() {
  121
+		$data = 'this is a test of the emergency broadcasting system';
  122
+		$result = Cache::write('delete_test', $data, 'wincache');
  123
+		$this->assertTrue($result);
  124
+
  125
+		$result = Cache::delete('delete_test', 'wincache');
  126
+		$this->assertTrue($result);
  127
+	}
  128
+
  129
+/**
  130
+ * testDecrement method
  131
+ *
  132
+ * @access public
  133
+ * @return void
  134
+ */
  135
+	public function testDecrement() {
  136
+		$this->skipIf(
  137
+			!function_exists('wincache_ucache_dec'),
  138
+			'No wincache_ucache_dec() function, cannot test decrement().'
  139
+		);
  140
+
  141
+		$result = Cache::write('test_decrement', 5, 'wincache');
  142
+		$this->assertTrue($result);
  143
+
  144
+		$result = Cache::decrement('test_decrement', 1, 'wincache');
  145
+		$this->assertEqual(4, $result);
  146
+
  147
+		$result = Cache::read('test_decrement', 'wincache');
  148
+		$this->assertEqual(4, $result);
  149
+
  150
+		$result = Cache::decrement('test_decrement', 2, 'wincache');
  151
+		$this->assertEqual(2, $result);
  152
+
  153
+		$result = Cache::read('test_decrement', 'wincache');
  154
+		$this->assertEqual(2, $result);
  155
+		
  156
+	}
  157
+
  158
+/**
  159
+ * testIncrement method
  160
+ *
  161
+ * @access public
  162
+ * @return void
  163
+ */
  164
+	public function testIncrement() {
  165
+		$this->skipIf(
  166
+			!function_exists('wincache_ucache_inc'),
  167
+			'No wincache_inc() function, cannot test increment().'
  168
+		);
  169
+
  170
+		$result = Cache::write('test_increment', 5, 'wincache');
  171
+		$this->assertTrue($result);
  172
+
  173
+		$result = Cache::increment('test_increment', 1, 'wincache');
  174
+		$this->assertEqual(6, $result);
  175
+
  176
+		$result = Cache::read('test_increment', 'wincache');
  177
+		$this->assertEqual(6, $result);
  178
+
  179
+		$result = Cache::increment('test_increment', 2, 'wincache');
  180
+		$this->assertEqual(8, $result);
  181
+
  182
+		$result = Cache::read('test_increment', 'wincache');
  183
+		$this->assertEqual(8, $result);
  184
+	}
  185
+
  186
+/**
  187
+ * test the clearing of cache keys
  188
+ *
  189
+ * @return void
  190
+ */
  191
+	public function testClear() {
  192
+		Cache::write('some_value', 'value', 'wincache');
  193
+
  194
+		$result = Cache::clear(false, 'wincache');
  195
+		$this->assertTrue($result);
  196
+		$this->assertFalse(Cache::read('some_value', 'wincache'));
  197
+	}
  198
+}

0 notes on commit ff34137

Please sign in to comment.
Something went wrong with that request. Please try again.