You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
4.1KB

  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. * XCache provides XCache caching in terms of an application component.
  10. *
  11. * To use this application component, the [XCache PHP extension](http://xcache.lighttpd.net/) must be loaded.
  12. * Also note that the [[flush()]] functionality will work correctly only if "xcache.admin.enable_auth"
  13. * is set to "Off" in php.ini.
  14. *
  15. * See [[Cache]] for common cache operations that XCache supports.
  16. *
  17. * @author Qiang Xue <qiang.xue@gmail.com>
  18. * @since 2.0
  19. */
  20. class XCache extends Cache
  21. {
  22. /**
  23. * Checks whether a specified key exists in the cache.
  24. * This can be faster than getting the value from the cache if the data is big.
  25. * Note that this method does not check whether the dependency associated
  26. * with the cached data, if there is any, has changed. So a call to [[get]]
  27. * may return false while exists returns true.
  28. * @param mixed $key a key identifying the cached value. This can be a simple string or
  29. * a complex data structure consisting of factors representing the key.
  30. * @return boolean true if a value exists in cache, false if the value is not in the cache or expired.
  31. */
  32. public function exists($key)
  33. {
  34. $key = $this->buildKey($key);
  35. return xcache_isset($key);
  36. }
  37. /**
  38. * Retrieves a value from cache with a specified key.
  39. * This is the implementation of the method declared in the parent class.
  40. * @param string $key a unique key identifying the cached value
  41. * @return mixed|false the value stored in cache, false if the value is not in the cache or expired.
  42. */
  43. protected function getValue($key)
  44. {
  45. return xcache_isset($key) ? xcache_get($key) : false;
  46. }
  47. /**
  48. * Stores a value identified by a key in cache.
  49. * This is the implementation of the method declared in the parent class.
  50. *
  51. * @param string $key the key identifying the value to be cached
  52. * @param mixed $value the value to be cached. Most often it's a string. If you have disabled [[serializer]],
  53. * it could be something else.
  54. * @param integer $duration the number of seconds in which the cached value will expire. 0 means never expire.
  55. * @return boolean true if the value is successfully stored into cache, false otherwise
  56. */
  57. protected function setValue($key, $value, $duration)
  58. {
  59. return xcache_set($key, $value, $duration);
  60. }
  61. /**
  62. * Stores a value identified by a key into cache if the cache does not contain this key.
  63. * This is the implementation of the method declared in the parent class.
  64. *
  65. * @param string $key the key identifying the value to be cached
  66. * @param mixed $value the value to be cached. Most often it's a string. If you have disabled [[serializer]],
  67. * it could be something else.
  68. * @param integer $duration the number of seconds in which the cached value will expire. 0 means never expire.
  69. * @return boolean true if the value is successfully stored into cache, false otherwise
  70. */
  71. protected function addValue($key, $value, $duration)
  72. {
  73. return !xcache_isset($key) ? $this->setValue($key, $value, $duration) : false;
  74. }
  75. /**
  76. * Deletes a value with the specified key from cache
  77. * This is the implementation of the method declared in the parent class.
  78. * @param string $key the key of the value to be deleted
  79. * @return boolean if no error happens during deletion
  80. */
  81. protected function deleteValue($key)
  82. {
  83. return xcache_unset($key);
  84. }
  85. /**
  86. * Deletes all values from cache.
  87. * This is the implementation of the method declared in the parent class.
  88. * @return boolean whether the flush operation was successful.
  89. */
  90. protected function flushValues()
  91. {
  92. for ($i = 0, $max = xcache_count(XC_TYPE_VAR); $i < $max; $i++) {
  93. if (xcache_clear_cache(XC_TYPE_VAR, $i) === false) {
  94. return false;
  95. }
  96. }
  97. return true;
  98. }
  99. }