XCache.php 2.7 KB

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