WinCache.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\caching;
  8. /**
  9. * WinCache provides Windows Cache caching in terms of an application component.
  10. *
  11. * To use this application component, the [WinCache PHP extension](http://www.iis.net/expand/wincacheforphp)
  12. * must be loaded. Also note that "wincache.ucenabled" should be set to "On" in your php.ini file.
  13. *
  14. * See [[Cache]] manual for common cache operations that are supported by WinCache.
  15. *
  16. * For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
  17. *
  18. * @author Qiang Xue <qiang.xue@gmail.com>
  19. * @since 2.0
  20. */
  21. class WinCache extends Cache
  22. {
  23. /**
  24. * Checks whether a specified key exists in the cache.
  25. * This can be faster than getting the value from the cache if the data is big.
  26. * Note that this method does not check whether the dependency associated
  27. * with the cached data, if there is any, has changed. So a call to [[get]]
  28. * may return false while exists returns true.
  29. * @param mixed $key a key identifying the cached value. This can be a simple string or
  30. * a complex data structure consisting of factors representing the key.
  31. * @return bool true if a value exists in cache, false if the value is not in the cache or expired.
  32. */
  33. public function exists($key)
  34. {
  35. $key = $this->buildKey($key);
  36. return wincache_ucache_exists($key);
  37. }
  38. /**
  39. * Retrieves a value from cache with a specified key.
  40. * This is the implementation of the method declared in the parent class.
  41. * @param string $key a unique key identifying the cached value
  42. * @return string|bool the value stored in cache, false if the value is not in the cache or expired.
  43. */
  44. protected function getValue($key)
  45. {
  46. return wincache_ucache_get($key);
  47. }
  48. /**
  49. * Retrieves multiple values from cache with the specified keys.
  50. * @param array $keys a list of keys identifying the cached values
  51. * @return array a list of cached values indexed by the keys
  52. */
  53. protected function getValues($keys)
  54. {
  55. return wincache_ucache_get($keys);
  56. }
  57. /**
  58. * Stores a value identified by a key in cache.
  59. * This is the implementation of the method declared in the parent class.
  60. *
  61. * @param string $key the key identifying the value to be cached
  62. * @param mixed $value the value to be cached. Most often it's a string. If you have disabled [[serializer]],
  63. * it could be something else.
  64. * @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
  65. * @return bool true if the value is successfully stored into cache, false otherwise
  66. */
  67. protected function setValue($key, $value, $duration)
  68. {
  69. return wincache_ucache_set($key, $value, $duration);
  70. }
  71. /**
  72. * Stores multiple key-value pairs in cache.
  73. * @param array $data array where key corresponds to cache key while value is the value stored
  74. * @param int $duration the number of seconds in which the cached values will expire. 0 means never expire.
  75. * @return array array of failed keys
  76. */
  77. protected function setValues($data, $duration)
  78. {
  79. return wincache_ucache_set($data, null, $duration);
  80. }
  81. /**
  82. * Stores a value identified by a key into cache if the cache does not contain this key.
  83. * This is the implementation of the method declared in the parent class.
  84. *
  85. * @param string $key the key identifying the value to be cached
  86. * @param mixed $value the value to be cached. Most often it's a string. If you have disabled [[serializer]],
  87. * it could be something else.
  88. * @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
  89. * @return bool true if the value is successfully stored into cache, false otherwise
  90. */
  91. protected function addValue($key, $value, $duration)
  92. {
  93. return wincache_ucache_add($key, $value, $duration);
  94. }
  95. /**
  96. * Adds multiple key-value pairs to cache.
  97. * The default implementation calls [[addValue()]] multiple times add values one by one. If the underlying cache
  98. * storage supports multiadd, this method should be overridden to exploit that feature.
  99. * @param array $data array where key corresponds to cache key while value is the value stored
  100. * @param int $duration the number of seconds in which the cached values will expire. 0 means never expire.
  101. * @return array array of failed keys
  102. */
  103. protected function addValues($data, $duration)
  104. {
  105. return wincache_ucache_add($data, null, $duration);
  106. }
  107. /**
  108. * Deletes a value with the specified key from cache
  109. * This is the implementation of the method declared in the parent class.
  110. * @param string $key the key of the value to be deleted
  111. * @return bool if no error happens during deletion
  112. */
  113. protected function deleteValue($key)
  114. {
  115. return wincache_ucache_delete($key);
  116. }
  117. /**
  118. * Deletes all values from cache.
  119. * This is the implementation of the method declared in the parent class.
  120. * @return bool whether the flush operation was successful.
  121. */
  122. protected function flushValues()
  123. {
  124. return wincache_ucache_clear();
  125. }
  126. }