sfAPCCache.class.php 3.0 KB

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