Wincache.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * [Kohana Cache](api/Kohana_Cache) Wincache driver. Provides an opcode based
  4. * driver for the Kohana Cache library.
  5. *
  6. * ### Configuration example
  7. *
  8. * Below is an example of an _wincache_ server configuration.
  9. *
  10. * return array(
  11. * 'wincache' => array( // Driver group
  12. * 'driver' => 'wincache', // using wincache driver
  13. * ),
  14. * )
  15. *
  16. * In cases where only one cache group is required, if the group is named `default` there is
  17. * no need to pass the group name when instantiating a cache instance.
  18. *
  19. * #### General cache group configuration settings
  20. *
  21. * Below are the settings available to all types of cache driver.
  22. *
  23. * Name | Required | Description
  24. * -------------- | -------- | ---------------------------------------------------------------
  25. * driver | __YES__ | (_string_) The driver type to use
  26. *
  27. * ### System requirements
  28. *
  29. * * Windows XP SP3 with IIS 5.1 and » FastCGI Extension
  30. * * Windows Server 2003 with IIS 6.0 and » FastCGI Extension
  31. * * Windows Vista SP1 with IIS 7.0 and FastCGI Module
  32. * * Windows Server 2008 with IIS 7.0 and FastCGI Module
  33. * * Windows 7 with IIS 7.5 and FastCGI Module
  34. * * Windows Server 2008 R2 with IIS 7.5 and FastCGI Module
  35. * * PHP 5.2.X, Non-thread-safe build
  36. * * PHP 5.3 X86, Non-thread-safe VC9 build
  37. *
  38. * @package Kohana/Cache
  39. * @category Base
  40. * @author Kohana Team
  41. * @copyright (c) Kohana Team
  42. * @license https://koseven.ga/LICENSE.md
  43. */
  44. class Kohana_Cache_Wincache extends Cache {
  45. /**
  46. * Check for existence of the wincache extension This method cannot be invoked externally. The driver must
  47. * be instantiated using the `Cache::instance()` method.
  48. *
  49. * @param array $config configuration
  50. * @throws Cache_Exception
  51. */
  52. protected function __construct(array $config)
  53. {
  54. if ( ! extension_loaded('wincache'))
  55. {
  56. throw new Cache_Exception('PHP wincache extension is not available.');
  57. }
  58. parent::__construct($config);
  59. }
  60. /**
  61. * Retrieve a cached value entry by id.
  62. *
  63. * // Retrieve cache entry from wincache group
  64. * $data = Cache::instance('wincache')->get('foo');
  65. *
  66. * // Retrieve cache entry from wincache group and return 'bar' if miss
  67. * $data = Cache::instance('wincache')->get('foo', 'bar');
  68. *
  69. * @param string $id id of cache to entry
  70. * @param string $default default value to return if cache miss
  71. * @return mixed
  72. * @throws Cache_Exception
  73. */
  74. public function get($id, $default = NULL)
  75. {
  76. $data = wincache_ucache_get($this->_sanitize_id($id), $success);
  77. return $success ? $data : $default;
  78. }
  79. /**
  80. * Set a value to cache with id and lifetime
  81. *
  82. * $data = 'bar';
  83. *
  84. * // Set 'bar' to 'foo' in wincache group, using default expiry
  85. * Cache::instance('wincache')->set('foo', $data);
  86. *
  87. * // Set 'bar' to 'foo' in wincache group for 30 seconds
  88. * Cache::instance('wincache')->set('foo', $data, 30);
  89. *
  90. * @param string $id id of cache entry
  91. * @param string $data data to set to cache
  92. * @param integer $lifetime lifetime in seconds
  93. * @return boolean
  94. */
  95. public function set($id, $data, $lifetime = NULL)
  96. {
  97. if ($lifetime === NULL)
  98. {
  99. $lifetime = Arr::get($this->_config, 'default_expire', Cache::DEFAULT_EXPIRE);
  100. }
  101. return wincache_ucache_set($this->_sanitize_id($id), $data, $lifetime);
  102. }
  103. /**
  104. * Delete a cache entry based on id
  105. *
  106. * // Delete 'foo' entry from the wincache group
  107. * Cache::instance('wincache')->delete('foo');
  108. *
  109. * @param string $id id to remove from cache
  110. * @return boolean
  111. */
  112. public function delete($id)
  113. {
  114. return wincache_ucache_delete($this->_sanitize_id($id));
  115. }
  116. /**
  117. * Delete all cache entries.
  118. *
  119. * Beware of using this method when
  120. * using shared memory cache systems, as it will wipe every
  121. * entry within the system for all clients.
  122. *
  123. * // Delete all cache entries in the wincache group
  124. * Cache::instance('wincache')->delete_all();
  125. *
  126. * @return boolean
  127. */
  128. public function delete_all()
  129. {
  130. return wincache_ucache_clear();
  131. }
  132. }