PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Cache/Engine/WincacheEngine.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 137 lines | 50 code | 10 blank | 77 comment | 4 complexity | 52fee7d9e1d63b4178dae3484a8bf09a MD5 | raw file
  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. * Wincache storage engine for cache
  23. *
  24. * @package Cake.Cache.Engine
  25. */
  26. class WincacheEngine extends CacheEngine {
  27. /**
  28. * Initialize the Cache Engine
  29. *
  30. * Called automatically by the cache frontend
  31. * To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
  32. *
  33. * @param array $settings array of setting for the engine
  34. * @return boolean True if the engine has been successfully initialized, false if not
  35. * @see CacheEngine::__defaults
  36. */
  37. public function init($settings = array()) {
  38. parent::init(array_merge(array(
  39. 'engine' => 'Wincache',
  40. 'prefix' => Inflector::slug(APP_DIR) . '_'),
  41. $settings));
  42. return function_exists('wincache_ucache_info');
  43. }
  44. /**
  45. * Write data for key into cache
  46. *
  47. * @param string $key Identifier for the data
  48. * @param mixed $value Data to be cached
  49. * @param integer $duration How long to cache the data, in seconds
  50. * @return boolean True if the data was successfully cached, false on failure
  51. */
  52. public function write($key, $value, $duration) {
  53. $expires = time() + $duration;
  54. $data = array(
  55. $key . '_expires' => $expires,
  56. $key => $value
  57. );
  58. $result = wincache_ucache_set($data, null, $duration);
  59. return empty($result);
  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
  66. * there was an error fetching it
  67. */
  68. public function read($key) {
  69. $time = time();
  70. $cachetime = intval(wincache_ucache_get($key . '_expires'));
  71. if ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime) {
  72. return false;
  73. }
  74. return wincache_ucache_get($key);
  75. }
  76. /**
  77. * Increments the value of an integer cached key
  78. *
  79. * @param string $key Identifier for the data
  80. * @param integer $offset How much to increment
  81. * @return New incremented value, false otherwise
  82. */
  83. public function increment($key, $offset = 1) {
  84. return wincache_ucache_inc($key, $offset);
  85. }
  86. /**
  87. * Decrements the value of an integer cached key
  88. *
  89. * @param string $key Identifier for the data
  90. * @param integer $offset How much to subtract
  91. * @return New decremented value, false otherwise
  92. */
  93. public function decrement($key, $offset = 1) {
  94. return wincache_ucache_dec($key, $offset);
  95. }
  96. /**
  97. * Delete a key from the cache
  98. *
  99. * @param string $key Identifier for the data
  100. * @return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed
  101. */
  102. public function delete($key) {
  103. return wincache_ucache_delete($key);
  104. }
  105. /**
  106. * Delete all keys from the cache. This will clear every
  107. * item in the cache matching the cache config prefix.
  108. *
  109. * @param boolean $check If true, nothing will be cleared, as entries will
  110. * naturally expire in wincache..
  111. * @return boolean True Returns true.
  112. */
  113. public function clear($check) {
  114. if ($check) {
  115. return true;
  116. }
  117. $info = wincache_ucache_info();
  118. $cacheKeys = $info['ucache_entries'];
  119. unset($info);
  120. foreach ($cacheKeys as $key) {
  121. if (strpos($key['key_name'], $this->settings['prefix']) === 0) {
  122. wincache_ucache_delete($key['key_name']);
  123. }
  124. }
  125. return true;
  126. }
  127. }