Memcache.php 3.0 KB

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