CXCache.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * CXCache class file
  4. *
  5. * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CXCache implements a cache application module based on {@link http://xcache.lighttpd.net/ xcache}.
  12. *
  13. * To use this application component, the XCache PHP extension must be loaded.
  14. * Flush functionality will only work correctly if "xcache.admin.enable_auth" is set to "Off" in php.ini.
  15. *
  16. * See {@link CCache} manual for common cache operations that are supported by CXCache.
  17. *
  18. * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
  19. * @package system.caching
  20. */
  21. class CXCache extends CCache
  22. {
  23. /**
  24. * Initializes this application component.
  25. * This method is required by the {@link IApplicationComponent} interface.
  26. * It checks the availability of memcache.
  27. * @throws CException if memcache extension is not loaded or is disabled.
  28. */
  29. public function init()
  30. {
  31. parent::init();
  32. if(!function_exists('xcache_isset'))
  33. throw new CException(Yii::t('yii','CXCache requires PHP XCache extension to be loaded.'));
  34. }
  35. /**
  36. * Retrieves a value from cache with a specified key.
  37. * This is the implementation of the method declared in the parent class.
  38. * @param string $key a unique key identifying the cached value
  39. * @return string|boolean the value stored in cache, false if the value is not in the cache or expired.
  40. */
  41. protected function getValue($key)
  42. {
  43. return xcache_isset($key) ? xcache_get($key) : false;
  44. }
  45. /**
  46. * Stores a value identified by a key in cache.
  47. * This is the implementation of the method declared in the parent class.
  48. *
  49. * @param string $key the key identifying the value to be cached
  50. * @param string $value the value to be cached
  51. * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
  52. * @return boolean true if the value is successfully stored into cache, false otherwise
  53. */
  54. protected function setValue($key,$value,$expire)
  55. {
  56. return xcache_set($key,$value,$expire);
  57. }
  58. /**
  59. * Stores a value identified by a key into cache if the cache does not contain this key.
  60. * This is the implementation of the method declared in the parent class.
  61. *
  62. * @param string $key the key identifying the value to be cached
  63. * @param string $value the value to be cached
  64. * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
  65. * @return boolean true if the value is successfully stored into cache, false otherwise
  66. */
  67. protected function addValue($key,$value,$expire)
  68. {
  69. return !xcache_isset($key) ? $this->setValue($key,$value,$expire) : false;
  70. }
  71. /**
  72. * Deletes a value with the specified key from cache
  73. * This is the implementation of the method declared in the parent class.
  74. * @param string $key the key of the value to be deleted
  75. * @return boolean if no error happens during deletion
  76. */
  77. protected function deleteValue($key)
  78. {
  79. return xcache_unset($key);
  80. }
  81. /**
  82. * Deletes all values from cache.
  83. * This is the implementation of the method declared in the parent class.
  84. * @return boolean whether the flush operation was successful.
  85. * @since 1.1.5
  86. */
  87. protected function flushValues()
  88. {
  89. for($i=0, $max=xcache_count(XC_TYPE_VAR); $i<$max; $i++)
  90. {
  91. if(xcache_clear_cache(XC_TYPE_VAR, $i)===false)
  92. return false;
  93. }
  94. return true;
  95. }
  96. }