CWinCache.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * CWinCache class file
  4. *
  5. * @author Alexander Makarov <sam@rmcreative.ru>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CWinCache implements a cache application component based on {@link http://www.iis.net/expand/wincacheforphp WinCache}.
  12. *
  13. * To use this application component, the WinCache PHP extension must be loaded.
  14. *
  15. * See {@link CCache} manual for common cache operations that are supported by CWinCache.
  16. *
  17. * @author Alexander Makarov <sam@rmcreative.ru>
  18. * @package system.caching
  19. * @since 1.1.2
  20. */
  21. class CWinCache extends CCache {
  22. /**
  23. * Initializes this application component.
  24. * This method is required by the {@link IApplicationComponent} interface.
  25. * It checks the availability of WinCache extension and WinCache user cache.
  26. * @throws CException if WinCache extension is not loaded or user cache is disabled
  27. */
  28. public function init()
  29. {
  30. parent::init();
  31. if(!extension_loaded('wincache'))
  32. throw new CException(Yii::t('yii', 'CWinCache requires PHP wincache extension to be loaded.'));
  33. if(!ini_get('wincache.ucenabled'))
  34. throw new CException(Yii::t('yii', 'CWinCache user cache is disabled. Please set wincache.ucenabled to On in your php.ini.'));
  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 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 wincache_ucache_set($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 wincache_ucache_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 wincache_ucache_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. return wincache_ucache_clear();
  100. }
  101. }