APC.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * Class Minify_Cache_APC
  4. * @package Minify
  5. */
  6. /**
  7. * APC-based cache class for Minify
  8. *
  9. * <code>
  10. * Minify::setCache(new Minify_Cache_APC());
  11. * </code>
  12. *
  13. * @package Minify
  14. * @author Chris Edwards
  15. **/
  16. class Minify_Cache_APC {
  17. /**
  18. * Create a Minify_Cache_APC object, to be passed to
  19. * Minify::setCache().
  20. *
  21. *
  22. * @param int $expire seconds until expiration (default = 0
  23. * meaning the item will not get an expiration date)
  24. *
  25. * @return null
  26. */
  27. public function __construct($expire = 0)
  28. {
  29. $this->_exp = $expire;
  30. }
  31. /**
  32. * Write data to cache.
  33. *
  34. * @param string $id cache id
  35. *
  36. * @param string $data
  37. *
  38. * @return bool success
  39. */
  40. public function store($id, $data)
  41. {
  42. return apc_store($id, "{$_SERVER['REQUEST_TIME']}|{$data}", $this->_exp);
  43. }
  44. /**
  45. * Get the size of a cache entry
  46. *
  47. * @param string $id cache id
  48. *
  49. * @return int size in bytes
  50. */
  51. public function getSize($id)
  52. {
  53. if (! $this->_fetch($id)) {
  54. return false;
  55. }
  56. return (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2))
  57. ? mb_strlen($this->_data, '8bit')
  58. : strlen($this->_data);
  59. }
  60. /**
  61. * Does a valid cache entry exist?
  62. *
  63. * @param string $id cache id
  64. *
  65. * @param int $srcMtime mtime of the original source file(s)
  66. *
  67. * @return bool exists
  68. */
  69. public function isValid($id, $srcMtime)
  70. {
  71. return ($this->_fetch($id) && ($this->_lm >= $srcMtime));
  72. }
  73. /**
  74. * Send the cached content to output
  75. *
  76. * @param string $id cache id
  77. */
  78. public function display($id)
  79. {
  80. echo $this->_fetch($id)
  81. ? $this->_data
  82. : '';
  83. }
  84. /**
  85. * Fetch the cached content
  86. *
  87. * @param string $id cache id
  88. *
  89. * @return string
  90. */
  91. public function fetch($id)
  92. {
  93. return $this->_fetch($id)
  94. ? $this->_data
  95. : '';
  96. }
  97. private $_exp = null;
  98. // cache of most recently fetched id
  99. private $_lm = null;
  100. private $_data = null;
  101. private $_id = null;
  102. /**
  103. * Fetch data and timestamp from apc, store in instance
  104. *
  105. * @param string $id
  106. *
  107. * @return bool success
  108. */
  109. private function _fetch($id)
  110. {
  111. if ($this->_id === $id) {
  112. return true;
  113. }
  114. $ret = apc_fetch($id);
  115. if (false === $ret) {
  116. $this->_id = null;
  117. return false;
  118. }
  119. list($this->_lm, $this->_data) = explode('|', $ret, 2);
  120. $this->_id = $id;
  121. return true;
  122. }
  123. }