fastcache.inc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. /**
  3. * @file
  4. * fastcache class.
  5. */
  6. include_once 'fastcacheitem.inc';
  7. /**
  8. * Static caching layer.
  9. *
  10. * Database layer for SQL Server
  11. * is very Regex intensive.
  12. * Cannot use a regultar cache
  13. * backend because the enormous number
  14. * of cache_get and cache_set calls
  15. * end up crashing memcache or wincache!
  16. * Here everything is statically managed
  17. * and sent to a real cache backend once
  18. * the request is over.
  19. */
  20. class fastcache {
  21. // @var fastcacheitem[] $fastcacheitems
  22. private static $fastcacheitems = array();
  23. // @var bool $enabled
  24. private static $enabled = NULL;
  25. // @var bool $shutdown_registered
  26. private static $shutdown_registered = FALSE;
  27. /**
  28. * Test info is loaded at database bootstrap phase
  29. * but this cache can be used earlier. Make sure
  30. * we have a valid test prefix before any operation on
  31. * the cache is performed.
  32. *
  33. * @var string
  34. */
  35. private static $test_run_id;
  36. /**
  37. * Add test prefix to current binary key, and account for atomic
  38. * items where $bin = NULL;
  39. *
  40. * @param string $prefix
  41. */
  42. private static function FixKeyAndBin(&$cid, &$bin) {
  43. // We always need a binary, if non is specified, this item
  44. // should be treated as having it's own binary.
  45. if (empty($bin)) {
  46. $bin = $cid;
  47. }
  48. // Try with the "official" test_run_id first.
  49. if (isset($GLOBALS['drupal_test_info']['test_run_id'])) {
  50. $bin = $GLOBALS['drupal_test_info']['test_run_id'] . $bin;
  51. return;
  52. }
  53. // Only do this once per request, if the this is the testing system
  54. // then $GLOBALS['drupal_test_info']['test_run_id'] will be set upon
  55. // test context switching.
  56. if (!isset(static::$test_run_id)) {
  57. if ($test_prefix = drupal_valid_test_ua()) {
  58. // Keep a local copy of the current test_prefix.
  59. static::$test_run_id = $test_prefix;
  60. }
  61. else {
  62. static::$test_run_id = '';
  63. }
  64. }
  65. $bin = static::$test_run_id . $bin;
  66. }
  67. /**
  68. * Tell if cache persistence is enabled. If not, this cache
  69. * will behave as DRUPAL_STATIC until the end of request.
  70. *
  71. * Only enable this cache if the backend is DrupalWinCache
  72. * and the lock implementation is DrupalWinCache
  73. */
  74. public static function Enabled($refresh = FALSE) {
  75. if (static::$enabled === NULL || $refresh) {
  76. // Make sure _cache_get_object exists, if fastache
  77. // used out of database driver there is a chance that
  78. // cache storage might not yet be initialized.
  79. if (function_exists('_cache_get_object')) {
  80. // If you only bootstrap to database the lock system is not yet started
  81. // but if locking is not database dependant (memcache or wincache) you can run
  82. // lock_initialize is ASAP.
  83. $lock_file = variable_get('lock_inc', 'includes/lock.inc');
  84. if ($lock_file != 'includes/lock.inc') {
  85. require_once DRUPAL_ROOT . '/' . $lock_file;
  86. lock_initialize();
  87. }
  88. global $WINCACHE_LOCK_ACTIVE;
  89. // Only enabled storage if Cache Backend is DrupalWinCache or ChainedFastBackend.
  90. $object = _cache_get_object('fastcache');
  91. static::$enabled = (is_a($object, \DrupalWinCache::class) || is_a($object, \ChainedFastBackend::class)) && $WINCACHE_LOCK_ACTIVE;
  92. }
  93. else {
  94. static::$enabled = FALSE;
  95. }
  96. }
  97. return static::$enabled;
  98. }
  99. /**
  100. * cache_clear_all wrapper.
  101. */
  102. public static function cache_clear_all($cid = NULL, $bin = NULL, $wildcard = FALSE) {
  103. static::FixKeyAndBin($cid, $bin);
  104. if (!isset(static::$fastcacheitems[$bin])) {
  105. static::cache_load_ensure($bin, TRUE);
  106. }
  107. // If the cache did not exist, it will still not be loaded.
  108. if (isset(static::$fastcacheitems[$bin])) {
  109. static::$fastcacheitems[$bin]->clear($cid, $wildcard);
  110. }
  111. }
  112. /**
  113. * Ensure cache binary is statically loaded.
  114. */
  115. private static function cache_load_ensure($bin, $skiploadifempty = FALSE) {
  116. if (!isset(static::$fastcacheitems[$bin])) {
  117. // If storage is enabled, try to load from cache.
  118. if (static::Enabled()) {
  119. if ($cache = cache_get($bin, 'fastcache')) {
  120. static::$fastcacheitems[$bin] = new fastcacheitem($bin, $cache);
  121. }
  122. // Don't bother initializing this.
  123. elseif ($skiploadifempty) {
  124. return;
  125. }
  126. }
  127. // If still not set, initialize.
  128. if (!isset(static::$fastcacheitems[$bin])) {
  129. static::$fastcacheitems[$bin] = new fastcacheitem($bin);
  130. }
  131. }
  132. }
  133. /**
  134. * cache_get wrapper.
  135. */
  136. public static function cache_get($cid, $bin = NULL) {
  137. static::FixKeyAndBin($cid, $bin);
  138. static::cache_load_ensure($bin);
  139. return static::$fastcacheitems[$bin]->data_get($cid);
  140. }
  141. /**
  142. * cache_set wrapper.
  143. */
  144. public static function cache_set($cid, $data, $bin = NULL) {
  145. static::FixKeyAndBin($cid, $bin);
  146. static::cache_load_ensure($bin);
  147. if (static::$fastcacheitems[$bin]->changed == FALSE) {
  148. static::$fastcacheitems[$bin]->changed = TRUE;
  149. // Do not lock if this is an atomic binary ($cid = $bin).
  150. if ($cid === $bin) {
  151. static::$fastcacheitems[$bin]->persist = TRUE;
  152. static::$fastcacheitems[$bin]->locked = FALSE;
  153. }
  154. else {
  155. // Do persist or lock if it is not enabled!
  156. if (static::Enabled()) {
  157. // Hold this locks longer than usual because
  158. // they run after the request has finished.
  159. if (function_exists('lock_acquire') && lock_acquire('fastcache_' . $bin, 120)) {
  160. static::$fastcacheitems[$bin]->persist = TRUE;
  161. static::$fastcacheitems[$bin]->locked = TRUE;
  162. }
  163. }
  164. }
  165. // Register shutdown persistence once, only if enabled!
  166. if (static::$shutdown_registered == FALSE && static::Enabled()) {
  167. register_shutdown_function(array('fastcache','fastcache_persist'));
  168. static::$shutdown_registered = TRUE;
  169. }
  170. }
  171. static::$fastcacheitems[$bin]->data_set($cid, $data);
  172. }
  173. /**
  174. * Called on shutdown, persists the cache
  175. * if necessary.
  176. */
  177. public static function fastcache_persist () {
  178. // Try to be the last of the last...
  179. register_shutdown_function(array('fastcache','_fastcache_persist'));
  180. }
  181. /**
  182. * Persist the binaries.
  183. */
  184. public static function _fastcache_persist () {
  185. foreach (static::$fastcacheitems as &$cache) {
  186. if ($cache->persist == TRUE) {
  187. cache_set($cache->bin, $cache->rawdata(), 'fastcache', CACHE_TEMPORARY);
  188. if ($cache->locked) {
  189. lock_release('fastcache_' . $cache->bin);
  190. }
  191. }
  192. // The binaries might be utilized on other shutdown functions...
  193. $cache->changed = FALSE;
  194. $cache->persist = FALSE;
  195. $cache->locked = FALSE;
  196. }
  197. // Fastcache may be reutilized during shutdown, make sure
  198. // persistence triggers run again.
  199. static::$shutdown_registered = FALSE;
  200. }
  201. }