XCache.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. * For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
  18. *
  19. * @author Qiang Xue <qiang.xue@gmail.com>
  20. * @since 2.0
  21. * @deprecated since 2.0.14. This class will be removed in 2.1.0.
  22. */
  23. class XCache extends Cache
  24. {
  25. /**
  26. * Checks whether a specified key exists in the cache.
  27. * This can be faster than getting the value from the cache if the data is big.
  28. * Note that this method does not check whether the dependency associated
  29. * with the cached data, if there is any, has changed. So a call to [[get]]
  30. * may return false while exists returns true.
  31. * @param mixed $key a key identifying the cached value. This can be a simple string or
  32. * a complex data structure consisting of factors representing the key.
  33. * @return bool true if a value exists in cache, false if the value is not in the cache or expired.
  34. */
  35. public function exists($key)
  36. {
  37. $key = $this->buildKey($key);
  38. return xcache_isset($key);
  39. }
  40. /**
  41. * Retrieves a value from cache with a specified key.
  42. * This is the implementation of the method declared in the parent class.
  43. * @param string $key a unique key identifying the cached value
  44. * @return mixed|false the value stored in cache, false if the value is not in the cache or expired.
  45. */
  46. protected function getValue($key)
  47. {
  48. return xcache_isset($key) ? xcache_get($key) : false;
  49. }
  50. /**
  51. * Stores a value identified by a key in cache.
  52. * This is the implementation of the method declared in the parent class.
  53. *
  54. * @param string $key the key identifying the value to be cached
  55. * @param mixed $value the value to be cached. Most often it's a string. If you have disabled [[serializer]],
  56. * it could be something else.
  57. * @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
  58. * @return bool true if the value is successfully stored into cache, false otherwise
  59. */
  60. protected function setValue($key, $value, $duration)
  61. {
  62. return xcache_set($key, $value, $duration);
  63. }
  64. /**
  65. * Stores a value identified by a key into cache if the cache does not contain this key.
  66. * This is the implementation of the method declared in the parent class.
  67. *
  68. * @param string $key the key identifying the value to be cached
  69. * @param mixed $value the value to be cached. Most often it's a string. If you have disabled [[serializer]],
  70. * it could be something else.
  71. * @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
  72. * @return bool true if the value is successfully stored into cache, false otherwise
  73. */
  74. protected function addValue($key, $value, $duration)
  75. {
  76. return !xcache_isset($key) ? $this->setValue($key, $value, $duration) : false;
  77. }
  78. /**
  79. * Deletes a value with the specified key from cache
  80. * This is the implementation of the method declared in the parent class.
  81. * @param string $key the key of the value to be deleted
  82. * @return bool if no error happens during deletion
  83. */
  84. protected function deleteValue($key)
  85. {
  86. return xcache_unset($key);
  87. }
  88. /**
  89. * Deletes all values from cache.
  90. * This is the implementation of the method declared in the parent class.
  91. * @return bool whether the flush operation was successful.
  92. */
  93. protected function flushValues()
  94. {
  95. for ($i = 0, $max = xcache_count(XC_TYPE_VAR); $i < $max; $i++) {
  96. if (xcache_clear_cache(XC_TYPE_VAR, $i) === false) {
  97. return false;
  98. }
  99. }
  100. return true;
  101. }
  102. }