You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 satır
2.1KB

  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. * ArrayCache provides caching for the current request only by storing the values in an array.
  10. *
  11. * See [[Cache]] for common cache operations that ArrayCache supports.
  12. *
  13. * Unlike the [[Cache]], ArrayCache allows the expire parameter of [[set]], [[add]], [[mset]] and [[madd]] to
  14. * be a floating point number, so you may specify the time in milliseconds (e.g. 0.1 will be 100 milliseconds).
  15. *
  16. * @author Carsten Brandt <mail@cebe.cc>
  17. * @since 2.0
  18. */
  19. class ArrayCache extends Cache
  20. {
  21. private $_cache;
  22. /**
  23. * @inheritdoc
  24. */
  25. public function exists($key)
  26. {
  27. $key = $this->buildKey($key);
  28. return isset($this->_cache[$key]) && ($this->_cache[$key][1] === 0 || $this->_cache[$key][1] > microtime(true));
  29. }
  30. /**
  31. * @inheritdoc
  32. */
  33. protected function getValue($key)
  34. {
  35. if (isset($this->_cache[$key]) && ($this->_cache[$key][1] === 0 || $this->_cache[$key][1] > microtime(true))) {
  36. return $this->_cache[$key][0];
  37. } else {
  38. return false;
  39. }
  40. }
  41. /**
  42. * @inheritdoc
  43. */
  44. protected function setValue($key, $value, $duration)
  45. {
  46. $this->_cache[$key] = [$value, $duration === 0 ? 0 : microtime(true) + $duration];
  47. return true;
  48. }
  49. /**
  50. * @inheritdoc
  51. */
  52. protected function addValue($key, $value, $duration)
  53. {
  54. if (isset($this->_cache[$key]) && ($this->_cache[$key][1] === 0 || $this->_cache[$key][1] > microtime(true))) {
  55. return false;
  56. } else {
  57. $this->_cache[$key] = [$value, $duration === 0 ? 0 : microtime(true) + $duration];
  58. return true;
  59. }
  60. }
  61. /**
  62. * @inheritdoc
  63. */
  64. protected function deleteValue($key)
  65. {
  66. unset($this->_cache[$key]);
  67. return true;
  68. }
  69. /**
  70. * @inheritdoc
  71. */
  72. protected function flushValues()
  73. {
  74. $this->_cache = [];
  75. return true;
  76. }
  77. }