1. sfAPCCache.class.php
  2. /** * Cache class that stores cached content in APC. * * @package symfony * @subpackage cache * @author Fabien Potencier * @version SVN: $Id: sfAPCCache.class.php 21990 2009-09-13 21:09:18Z FabianLange $ */
  3. class sfAPCCache extends sfCache
  4. {
  5. /**
  6. * Initializes this sfCache instance.
  7. *
  8. * Available options:
  9. *
  10. * * see sfCache for options available for all drivers
  11. *
  12. * @see sfCache
  13. */
  14. public function initialize($options = array())
  15. {
  16. parent::initialize($options);
  17. if (!function_exists('apc_store') || !ini_get('apc.enabled'))
  18. {
  19. throw new sfInitializationException('You must have APC installed and enabled to use sfAPCCache class.');
  20. }
  21. }
  22. /**
  23. * @see sfCache
  24. */
  25. public function get($key, $default = null)
  26. {
  27. $value = $this->fetch($this->getOption('prefix').$key, $has);
  28. return $has ? $value : $default;
  29. }
  30. /**
  31. * @see sfCache
  32. */
  33. public function has($key)
  34. {
  35. $this->fetch($this->getOption('prefix').$key, $has);
  36. return $has;
  37. }
  38. private function fetch($key, &$success)
  39. {
  40. $has = null;
  41. $value = apc_fetch($key, $has);
  42. // the second argument was added in APC 3.0.17. If it is still null we fall back to the value returned
  43. if (null !== $has)
  44. {
  45. $success = $has;
  46. }
  47. else
  48. {
  49. $success = $value !== false;
  50. }
  51. return $value;
  52. }
  53. /**
  54. * @see sfCache
  55. */
  56. public function set($key, $data, $lifetime = null)
  57. {
  58. return apc_store($this->getOption('prefix').$key, $data, $this->getLifetime($lifetime));
  59. }
  60. /**
  61. * @see sfCache
  62. */
  63. public function remove($key)
  64. {
  65. return apc_delete($this->getOption('prefix').$key);
  66. }
  67. /**
  68. * @see sfCache
  69. */
  70. public function clean($mode = sfCache::ALL)
  71. {
  72. if (sfCache::ALL === $mode)
  73. {
  74. return apc_clear_cache('user');
  75. }
  76. }
  77. /**
  78. * @see sfCache
  79. */
  80. public function getLastModified($key)
  81. {
  82. if ($info = $this->getCacheInfo($key))
  83. {
  84. return $info['creation_time'] + $info['ttl'] > time() ? $info['mtime'] : 0;
  85. }
  86. return 0;
  87. }
  88. /**
  89. * @see sfCache
  90. */
  91. public function getTimeout($key)
  92. {
  93. if ($info = $this->getCacheInfo($key))
  94. {
  95. return $info['creation_time'] + $info['ttl'] > time() ? $info['creation_time'] + $info['ttl'] : 0;
  96. }
  97. return 0;
  98. }
  99. /**
  100. * @see sfCache
  101. */
  102. public function removePattern($pattern)
  103. {
  104. $infos = apc_cache_info('user');
  105. if (!is_array($infos['cache_list']))
  106. {
  107. return;
  108. }
  109. $regexp = self::patternToRegexp($this->getOption('prefix').$pattern);
  110. foreach ($infos['cache_list'] as $info)
  111. {
  112. if (preg_match($regexp, $info['info']))
  113. {
  114. apc_delete($info['info']);
  115. }
  116. }
  117. }
  118. protected function getCacheInfo($key)
  119. {
  120. $infos = apc_cache_info('user');
  121. if (is_array($infos['cache_list']))
  122. {
  123. foreach ($infos['cache_list'] as $info)
  124. {
  125. if ($this->getOption('prefix').$key == $info['info'])
  126. {
  127. return $info;
  128. }
  129. }
  130. }
  131. return null;
  132. }
  133. }

Debug toolbar