WinCache.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Class Minify_Cache_Wincache
  4. * @package Minify
  5. */
  6. /**
  7. * WinCache-based cache class for Minify
  8. *
  9. * <code>
  10. * Minify::setCache(new Minify_Cache_WinCache());
  11. * </code>
  12. *
  13. * @package Minify
  14. * @author Matthias Fax
  15. **/
  16. class Minify_Cache_WinCache
  17. {
  18. /**
  19. * Create a Minify_Cache_Wincache object, to be passed to
  20. * Minify::setCache().
  21. *
  22. *
  23. * @param int $expire seconds until expiration (default = 0
  24. * meaning the item will not get an expiration date)
  25. */
  26. public function __construct($expire = 0)
  27. {
  28. if (!function_exists('wincache_ucache_info')) {
  29. throw new Exception("WinCache for PHP is not installed to be able to use Minify_Cache_WinCache!");
  30. }
  31. $this->_exp = $expire;
  32. }
  33. /**
  34. * Write data to cache.
  35. *
  36. * @param string $id cache id
  37. *
  38. * @param string $data
  39. *
  40. * @return bool success
  41. */
  42. public function store($id, $data)
  43. {
  44. return wincache_ucache_set($id, "{$_SERVER['REQUEST_TIME']}|{$data}", $this->_exp);
  45. }
  46. /**
  47. * Get the size of a cache entry
  48. *
  49. * @param string $id cache id
  50. *
  51. * @return int size in bytes
  52. */
  53. public function getSize($id)
  54. {
  55. if (!$this->_fetch($id)) {
  56. return false;
  57. }
  58. return (function_exists('mb_strlen') && ((int) ini_get('mbstring.func_overload') & 2)) ? mb_strlen($this->_data, '8bit') : 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) ? $this->_data : '';
  81. }
  82. /**
  83. * Fetch the cached content
  84. *
  85. * @param string $id cache id
  86. *
  87. * @return string
  88. */
  89. public function fetch($id)
  90. {
  91. return $this->_fetch($id) ? $this->_data : '';
  92. }
  93. private $_exp = NULL;
  94. // cache of most recently fetched id
  95. private $_lm = NULL;
  96. private $_data = NULL;
  97. private $_id = NULL;
  98. /**
  99. * Fetch data and timestamp from WinCache, store in instance
  100. *
  101. * @param string $id
  102. *
  103. * @return bool success
  104. */
  105. private function _fetch($id)
  106. {
  107. if ($this->_id === $id) {
  108. return true;
  109. }
  110. $suc = false;
  111. $ret = wincache_ucache_get($id, $suc);
  112. if (!$suc) {
  113. $this->_id = NULL;
  114. return false;
  115. }
  116. list($this->_lm, $this->_data) = explode('|', $ret, 2);
  117. $this->_id = $id;
  118. return true;
  119. }
  120. }