136 lines
5.3KB

  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 mixed $value the value to be cached. Most often it's a string. If you have disabled [[serializer]],
  61. * it could be something else.
  62. * @param integer $duration the number of seconds in which the cached value will expire. 0 means never expire.
  63. * @return boolean true if the value is successfully stored into cache, false otherwise
  64. */
  65. protected function setValue($key, $value, $duration)
  66. {
  67. return wincache_ucache_set($key, $value, $duration);
  68. }
  69. /**
  70. * Stores multiple key-value pairs in cache.
  71. * @param array $data array where key corresponds to cache key while value is the value stored
  72. * @param integer $duration the number of seconds in which the cached values will expire. 0 means never expire.
  73. * @return array array of failed keys
  74. */
  75. protected function setValues($data, $duration)
  76. {
  77. return wincache_ucache_set($data, null, $duration);
  78. }
  79. /**
  80. * Stores a value identified by a key into cache if the cache does not contain this key.
  81. * This is the implementation of the method declared in the parent class.
  82. *
  83. * @param string $key the key identifying the value to be cached
  84. * @param mixed $value the value to be cached. Most often it's a string. If you have disabled [[serializer]],
  85. * it could be something else.
  86. * @param integer $duration the number of seconds in which the cached value will expire. 0 means never expire.
  87. * @return boolean true if the value is successfully stored into cache, false otherwise
  88. */
  89. protected function addValue($key, $value, $duration)
  90. {
  91. return wincache_ucache_add($key, $value, $duration);
  92. }
  93. /**
  94. * Adds multiple key-value pairs to cache.
  95. * The default implementation calls [[addValue()]] multiple times add values one by one. If the underlying cache
  96. * storage supports multiadd, this method should be overridden to exploit that feature.
  97. * @param array $data array where key corresponds to cache key while value is the value stored
  98. * @param integer $duration the number of seconds in which the cached values will expire. 0 means never expire.
  99. * @return array array of failed keys
  100. */
  101. protected function addValues($data, $duration)
  102. {
  103. return wincache_ucache_add($data, null, $duration);
  104. }
  105. /**
  106. * Deletes a value with the specified key from cache
  107. * This is the implementation of the method declared in the parent class.
  108. * @param string $key the key of the value to be deleted
  109. * @return boolean if no error happens during deletion
  110. */
  111. protected function deleteValue($key)
  112. {
  113. return wincache_ucache_delete($key);
  114. }
  115. /**
  116. * Deletes all values from cache.
  117. * This is the implementation of the method declared in the parent class.
  118. * @return boolean whether the flush operation was successful.
  119. */
  120. protected function flushValues()
  121. {
  122. return wincache_ucache_clear();
  123. }
  124. }