CApcCache.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * CApcCache class file
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CApcCache provides APC caching in terms of an application component.
  12. *
  13. * The caching is based on {@link http://www.php.net/apc APC}.
  14. * To use this application component, the APC PHP extension must be loaded.
  15. *
  16. * See {@link CCache} manual for common cache operations that are supported by CApcCache.
  17. *
  18. * @author Qiang Xue <qiang.xue@gmail.com>
  19. * @package system.caching
  20. * @since 1.0
  21. */
  22. class CApcCache extends CCache
  23. {
  24. /**
  25. * Initializes this application component.
  26. * This method is required by the {@link IApplicationComponent} interface.
  27. * It checks the availability of APC.
  28. * @throws CException if APC cache extension is not loaded or is disabled.
  29. */
  30. public function init()
  31. {
  32. parent::init();
  33. if(!extension_loaded('apc'))
  34. throw new CException(Yii::t('yii','CApcCache requires PHP apc extension to be loaded.'));
  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 apc_fetch($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 apc_fetch($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 $expire 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,$expire)
  65. {
  66. return apc_store($key,$value,$expire);
  67. }
  68. /**
  69. * Stores a value identified by a key into cache if the cache does not contain this key.
  70. * This is the implementation of the method declared in the parent class.
  71. *
  72. * @param string $key the key identifying the value to be cached
  73. * @param string $value the value to be cached
  74. * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
  75. * @return boolean true if the value is successfully stored into cache, false otherwise
  76. */
  77. protected function addValue($key,$value,$expire)
  78. {
  79. return apc_add($key,$value,$expire);
  80. }
  81. /**
  82. * Deletes a value with the specified key from cache
  83. * This is the implementation of the method declared in the parent class.
  84. * @param string $key the key of the value to be deleted
  85. * @return boolean if no error happens during deletion
  86. */
  87. protected function deleteValue($key)
  88. {
  89. return apc_delete($key);
  90. }
  91. /**
  92. * Deletes all values from cache.
  93. * This is the implementation of the method declared in the parent class.
  94. * @return boolean whether the flush operation was successful.
  95. * @since 1.1.5
  96. */
  97. protected function flushValues()
  98. {
  99. if(extension_loaded('apcu'))
  100. return apc_clear_cache();
  101. return apc_clear_cache('user');
  102. }
  103. }