CDummyCache.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * CDummyCache 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. * CDummyCache is a placeholder cache component.
  12. *
  13. * CDummyCache does not cache anything. It is provided so that one can always configure
  14. * a 'cache' application component and he does not need to check if Yii::app()->cache is null or not.
  15. * By replacing CDummyCache with some other cache component, one can quickly switch from
  16. * non-caching mode to caching mode.
  17. *
  18. * @author Qiang Xue <qiang.xue@gmail.com>
  19. * @package system.caching
  20. * @since 1.0
  21. */
  22. class CDummyCache extends CApplicationComponent implements ICache, ArrayAccess
  23. {
  24. /**
  25. * @var string a string prefixed to every cache key so that it is unique. Defaults to {@link CApplication::getId() application ID}.
  26. */
  27. public $keyPrefix;
  28. /**
  29. * Initializes the application component.
  30. * This method overrides the parent implementation by setting default cache key prefix.
  31. */
  32. public function init()
  33. {
  34. parent::init();
  35. if($this->keyPrefix===null)
  36. $this->keyPrefix=Yii::app()->getId();
  37. }
  38. /**
  39. * Retrieves a value from cache with a specified key.
  40. * @param string $id a key identifying the cached value
  41. * @return mixed the value stored in cache, false if the value is not in the cache, expired or the dependency has changed.
  42. */
  43. public function get($id)
  44. {
  45. return false;
  46. }
  47. /**
  48. * Retrieves multiple values from cache with the specified keys.
  49. * Some caches (such as memcache, apc) allow retrieving multiple cached values at one time,
  50. * which may improve the performance since it reduces the communication cost.
  51. * In case a cache doesn't support this feature natively, it will be simulated by this method.
  52. * @param array $ids list of keys identifying the cached values
  53. * @return array list of cached values corresponding to the specified keys. The array
  54. * is returned in terms of (key,value) pairs.
  55. * If a value is not cached or expired, the corresponding array value will be false.
  56. */
  57. public function mget($ids)
  58. {
  59. $results=array();
  60. foreach($ids as $id)
  61. $results[$id]=false;
  62. return $results;
  63. }
  64. /**
  65. * Stores a value identified by a key into cache.
  66. * If the cache already contains such a key, the existing value and
  67. * expiration time will be replaced with the new ones.
  68. *
  69. * @param string $id the key identifying the value to be cached
  70. * @param mixed $value the value to be cached
  71. * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
  72. * @param ICacheDependency $dependency dependency of the cached item. If the dependency changes, the item is labeled invalid.
  73. * @return boolean true if the value is successfully stored into cache, false otherwise
  74. */
  75. public function set($id,$value,$expire=0,$dependency=null)
  76. {
  77. return true;
  78. }
  79. /**
  80. * Stores a value identified by a key into cache if the cache does not contain this key.
  81. * Nothing will be done if the cache already contains the key.
  82. * @param string $id the key identifying the value to be cached
  83. * @param mixed $value the value to be cached
  84. * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
  85. * @param ICacheDependency $dependency dependency of the cached item. If the dependency changes, the item is labeled invalid.
  86. * @return boolean true if the value is successfully stored into cache, false otherwise
  87. */
  88. public function add($id,$value,$expire=0,$dependency=null)
  89. {
  90. return true;
  91. }
  92. /**
  93. * Deletes a value with the specified key from cache
  94. * @param string $id the key of the value to be deleted
  95. * @return boolean if no error happens during deletion
  96. */
  97. public function delete($id)
  98. {
  99. return true;
  100. }
  101. /**
  102. * Deletes all values from cache.
  103. * Be careful of performing this operation if the cache is shared by multiple applications.
  104. * @return boolean whether the flush operation was successful.
  105. * @throws CException if this method is not overridden by child classes
  106. */
  107. public function flush()
  108. {
  109. return true;
  110. }
  111. /**
  112. * Returns whether there is a cache entry with a specified key.
  113. * This method is required by the interface ArrayAccess.
  114. * @param string $id a key identifying the cached value
  115. * @return boolean
  116. */
  117. public function offsetExists($id)
  118. {
  119. return false;
  120. }
  121. /**
  122. * Retrieves the value from cache with a specified key.
  123. * This method is required by the interface ArrayAccess.
  124. * @param string $id a key identifying the cached value
  125. * @return mixed the value stored in cache, false if the value is not in the cache or expired.
  126. */
  127. public function offsetGet($id)
  128. {
  129. return false;
  130. }
  131. /**
  132. * Stores the value identified by a key into cache.
  133. * If the cache already contains such a key, the existing value will be
  134. * replaced with the new ones. To add expiration and dependencies, use the set() method.
  135. * This method is required by the interface ArrayAccess.
  136. * @param string $id the key identifying the value to be cached
  137. * @param mixed $value the value to be cached
  138. */
  139. public function offsetSet($id, $value)
  140. {
  141. }
  142. /**
  143. * Deletes the value with the specified key from cache
  144. * This method is required by the interface ArrayAccess.
  145. * @param string $id the key of the value to be deleted
  146. * @return boolean if no error happens during deletion
  147. */
  148. public function offsetUnset($id)
  149. {
  150. }
  151. }