123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. * @author Qiang Xue <qiang.xue@gmail.com>
  17. * @since 2.0
  18. */
  19. class WinCache extends Cache
  20. {
  21. /**
  22. * Checks whether a specified key exists in the cache.
  23. * This can be faster than getting the value from the cache if the data is big.
  24. * Note that this method does not check whether the dependency associated
  25. * with the cached data, if there is any, has changed. So a call to [[get]]
  26. * may return false while exists returns true.
  27. * @param mixed $key a key identifying the cached value. This can be a simple string or
  28. * a complex data structure consisting of factors representing the key.
  29. * @return boolean true if a value exists in cache, false if the value is not in the cache or expired.
  30. */
  31. public function exists($key)
  32. {
  33. $key = $this->buildKey($key);
  34. return wincache_ucache_exists($key);
  35. }
  36. /**
  37. * Retrieves a value from cache with a specified key.
  38. * This is the implementation of the method declared in the parent class.
  39. * @param string $key a unique key identifying the cached value
  40. * @return string|boolean the value stored in cache, false if the value is not in the cache or expired.
  41. */
  42. protected function getValue($key)
  43. {
  44. return wincache_ucache_get($key);
  45. }
  46. /**
  47. * Retrieves multiple values from cache with the specified keys.
  48. * @param array $keys a list of keys identifying the cached values
  49. * @return array a list of cached values indexed by the keys
  50. */
  51. protected function getValues($keys)
  52. {
  53. return wincache_ucache_get($keys);
  54. }
  55. /**
  56. * Stores a value identified by a key in cache.
  57. * This is the implementation of the method declared in the parent class.
  58. *
  59. * @param string $key the key identifying the value to be cached
  60. * @param string $value the value to be cached
  61. * @param integer $duration the number of seconds in which the cached value will expire. 0 means never expire.
  62. * @return boolean true if the value is successfully stored into cache, false otherwise
  63. */
  64. protected function setValue($key, $value, $duration)
  65. {
  66. return wincache_ucache_set($key, $value, $duration);
  67. }
  68. /**
  69. * Stores multiple key-value pairs in cache.
  70. * @param array $data array where key corresponds to cache key while value is the value stored
  71. * @param integer $duration the number of seconds in which the cached values will expire. 0 means never expire.
  72. * @return array array of failed keys
  73. */
  74. protected function setValues($data, $duration)
  75. {
  76. return wincache_ucache_set($data, null, $duration);
  77. }
  78. /**
  79. * Stores a value identified by a key into cache if the cache does not contain this key.
  80. * This is the implementation of the method declared in the parent class.
  81. *
  82. * @param string $key the key identifying the value to be cached
  83. * @param string $value the value to be cached
  84. * @param integer $duration the number of seconds in which the cached value will expire. 0 means never expire.
  85. * @return boolean true if the value is successfully stored into cache, false otherwise
  86. */
  87. protected function addValue($key, $value, $duration)
  88. {
  89. return wincache_ucache_add($key, $value, $duration);
  90. }
  91. /**
  92. * Adds multiple key-value pairs to cache.
  93. * The default implementation calls [[addValue()]] multiple times add values one by one. If the underlying cache
  94. * storage supports multiadd, this method should be overridden to exploit that feature.
  95. * @param array $data array where key corresponds to cache key while value is the value stored
  96. * @param integer $duration the number of seconds in which the cached values will expire. 0 means never expire.
  97. * @return array array of failed keys
  98. */
  99. protected function addValues($data, $duration)
  100. {
  101. return wincache_ucache_add($data, null, $duration);
  102. }
  103. /**
  104. * Deletes a value with the specified key from cache
  105. * This is the implementation of the method declared in the parent class.
  106. * @param string $key the key of the value to be deleted
  107. * @return boolean if no error happens during deletion
  108. */
  109. protected function deleteValue($key)
  110. {
  111. return wincache_ucache_delete($key);
  112. }
  113. /**
  114. * Deletes all values from cache.
  115. * This is the implementation of the method declared in the parent class.
  116. * @return boolean whether the flush operation was successful.
  117. */
  118. protected function flushValues()
  119. {
  120. return wincache_ucache_clear();
  121. }
  122. }