PageRenderTime 52ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/cache/classes/loaders.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 2202 lines | 1054 code | 118 blank | 1030 comment | 288 complexity | b07992dec8f7c50382d29d621c23d7c4 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Cache loaders
  18. *
  19. * This file is part of Moodle's cache API, affectionately called MUC.
  20. * It contains the components that are required in order to use caching.
  21. *
  22. * @package core
  23. * @category cache
  24. * @copyright 2012 Sam Hemelryk
  25. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26. */
  27. defined('MOODLE_INTERNAL') || die();
  28. /**
  29. * The main cache class.
  30. *
  31. * This class if the first class that any end developer will interact with.
  32. * In order to create an instance of a cache that they can work with they must call one of the static make methods belonging
  33. * to this class.
  34. *
  35. * @package core
  36. * @category cache
  37. * @copyright 2012 Sam Hemelryk
  38. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39. */
  40. class cache implements cache_loader {
  41. /**
  42. * We need a timestamp to use within the cache API.
  43. * This stamp needs to be used for all ttl and time based operations to ensure that we don't end up with
  44. * timing issues.
  45. * @var int
  46. */
  47. protected static $now;
  48. /**
  49. * The definition used when loading this cache if there was one.
  50. * @var cache_definition
  51. */
  52. private $definition = false;
  53. /**
  54. * The cache store that this loader will make use of.
  55. * @var cache_store
  56. */
  57. private $store;
  58. /**
  59. * The next cache loader in the chain if there is one.
  60. * If a cache request misses for the store belonging to this loader then the loader
  61. * stored here will be checked next.
  62. * If there is a loader here then $datasource must be false.
  63. * @var cache_loader|false
  64. */
  65. private $loader = false;
  66. /**
  67. * The data source to use if we need to load data (because if doesn't exist in the cache store).
  68. * If there is a data source here then $loader above must be false.
  69. * @var cache_data_source|false
  70. */
  71. private $datasource = false;
  72. /**
  73. * Used to quickly check if the store supports key awareness.
  74. * This is set when the cache is initialised and is used to speed up processing.
  75. * @var bool
  76. */
  77. private $supportskeyawareness = null;
  78. /**
  79. * Used to quickly check if the store supports ttl natively.
  80. * This is set when the cache is initialised and is used to speed up processing.
  81. * @var bool
  82. */
  83. private $supportsnativettl = null;
  84. /**
  85. * Gets set to true if the cache is going to be using a static array for acceleration.
  86. * The array statically caches items used during the lifetime of the request. This greatly speeds up interaction
  87. * with the cache in areas where it will be repetitively hit for the same information such as with strings.
  88. * There are several other variables to control how this static acceleration array works.
  89. * @var bool
  90. */
  91. private $staticacceleration = false;
  92. /**
  93. * The static acceleration array.
  94. * Items will be stored in this cache as they were provided. This ensure there is no unnecessary processing taking place.
  95. * @var array
  96. */
  97. private $staticaccelerationarray = array();
  98. /**
  99. * The number of items in the static acceleration array. Avoids count calls like you wouldn't believe.
  100. * @var int
  101. */
  102. private $staticaccelerationcount = 0;
  103. /**
  104. * An array containing just the keys being used in the static acceleration array.
  105. * This seems redundant perhaps but is used when managing the size of the static acceleration array.
  106. * Items are added to the end of the array and the when we need to reduce the size of the cache we use the
  107. * key that is first on this array.
  108. * @var array
  109. */
  110. private $staticaccelerationkeys = array();
  111. /**
  112. * The maximum size of the static acceleration array.
  113. *
  114. * If set to false there is no max size.
  115. * Caches that make use of static acceleration should seriously consider setting this to something reasonably small, but
  116. * still large enough to offset repetitive calls.
  117. *
  118. * @var int|false
  119. */
  120. private $staticaccelerationsize = false;
  121. /**
  122. * Gets set to true during initialisation if the definition is making use of a ttl.
  123. * Used to speed up processing.
  124. * @var bool
  125. */
  126. private $hasattl = false;
  127. /**
  128. * Gets set to the class name of the store during initialisation. This is used several times in the cache class internally
  129. * and having it here helps speed up processing.
  130. * @var strubg
  131. */
  132. protected $storetype = 'unknown';
  133. /**
  134. * Gets set to true if we want to collect performance information about the cache API.
  135. * @var bool
  136. */
  137. protected $perfdebug = false;
  138. /**
  139. * Determines if this loader is a sub loader, not the top of the chain.
  140. * @var bool
  141. */
  142. protected $subloader = false;
  143. /**
  144. * Creates a new cache instance for a pre-defined definition.
  145. *
  146. * @param string $component The component for the definition
  147. * @param string $area The area for the definition
  148. * @param array $identifiers Any additional identifiers that should be provided to the definition.
  149. * @param string $aggregate Super advanced feature. More docs later.
  150. * @return cache_application|cache_session|cache_store
  151. */
  152. public static function make($component, $area, array $identifiers = array(), $aggregate = null) {
  153. $factory = cache_factory::instance();
  154. return $factory->create_cache_from_definition($component, $area, $identifiers, $aggregate);
  155. }
  156. /**
  157. * Creates a new cache instance based upon the given params.
  158. *
  159. * @param int $mode One of cache_store::MODE_*
  160. * @param string $component The component this cache relates to.
  161. * @param string $area The area this cache relates to.
  162. * @param array $identifiers Any additional identifiers that should be provided to the definition.
  163. * @param array $options An array of options, available options are:
  164. * - simplekeys : Set to true if the keys you will use are a-zA-Z0-9_
  165. * - simpledata : Set to true if the type of the data you are going to store is scalar, or an array of scalar vars
  166. * - staticacceleration : If set to true the cache will hold onto data passing through it.
  167. * - staticaccelerationsize : The max size for the static acceleration array.
  168. * @return cache_application|cache_session|cache_store
  169. */
  170. public static function make_from_params($mode, $component, $area, array $identifiers = array(), array $options = array()) {
  171. $factory = cache_factory::instance();
  172. return $factory->create_cache_from_params($mode, $component, $area, $identifiers, $options);
  173. }
  174. /**
  175. * Constructs a new cache instance.
  176. *
  177. * You should not call this method from your code, instead you should use the cache::make methods.
  178. *
  179. * This method is public so that the cache_factory is able to instantiate cache instances.
  180. * Ideally we would make this method protected and expose its construction to the factory method internally somehow.
  181. * The factory class is responsible for this in order to centralise the storage of instances once created. This way if needed
  182. * we can force a reset of the cache API (used during unit testing).
  183. *
  184. * @param cache_definition $definition The definition for the cache instance.
  185. * @param cache_store $store The store that cache should use.
  186. * @param cache_loader|cache_data_source $loader The next loader in the chain or the data source if there is one and there
  187. * are no other cache_loaders in the chain.
  188. */
  189. public function __construct(cache_definition $definition, cache_store $store, $loader = null) {
  190. global $CFG;
  191. $this->definition = $definition;
  192. $this->store = $store;
  193. $this->storetype = get_class($store);
  194. $this->perfdebug = !empty($CFG->perfdebug);
  195. if ($loader instanceof cache_loader) {
  196. $this->loader = $loader;
  197. // Mark the loader as a sub (chained) loader.
  198. $this->loader->set_is_sub_loader(true);
  199. } else if ($loader instanceof cache_data_source) {
  200. $this->datasource = $loader;
  201. }
  202. $this->definition->generate_definition_hash();
  203. $this->staticacceleration = $this->definition->use_static_acceleration();
  204. if ($this->staticacceleration) {
  205. $this->staticaccelerationsize = $this->definition->get_static_acceleration_size();
  206. }
  207. $this->hasattl = ($this->definition->get_ttl() > 0);
  208. }
  209. /**
  210. * Used to inform the loader of its state as a sub loader, or as the top of the chain.
  211. *
  212. * This is important as it ensures that we do not have more than one loader keeping static acceleration data.
  213. * Subloaders need to be "pure" loaders in the sense that they are used to store and retrieve information from stores or the
  214. * next loader/data source in the chain.
  215. * Nothing fancy, nothing flash.
  216. *
  217. * @param bool $setting
  218. */
  219. protected function set_is_sub_loader($setting = true) {
  220. if ($setting) {
  221. $this->subloader = true;
  222. // Subloaders should not keep static acceleration data.
  223. $this->staticacceleration = false;
  224. $this->staticaccelerationsize = false;
  225. } else {
  226. $this->subloader = true;
  227. $this->staticacceleration = $this->definition->use_static_acceleration();
  228. if ($this->staticacceleration) {
  229. $this->staticaccelerationsize = $this->definition->get_static_acceleration_size();
  230. }
  231. }
  232. }
  233. /**
  234. * Alters the identifiers that have been provided to the definition.
  235. *
  236. * This is an advanced method and should not be used unless really needed.
  237. * It allows the developer to slightly alter the definition without having to re-establish the cache.
  238. * It will cause more processing as the definition will need to clear and reprepare some of its properties.
  239. *
  240. * @param array $identifiers
  241. */
  242. public function set_identifiers(array $identifiers) {
  243. $this->definition->set_identifiers($identifiers);
  244. }
  245. /**
  246. * Retrieves the value for the given key from the cache.
  247. *
  248. * @param string|int $key The key for the data being requested.
  249. * It can be any structure although using a scalar string or int is recommended in the interests of performance.
  250. * In advanced cases an array may be useful such as in situations requiring the multi-key functionality.
  251. * @param int $strictness One of IGNORE_MISSING | MUST_EXIST
  252. * @return mixed|false The data from the cache or false if the key did not exist within the cache.
  253. * @throws coding_exception
  254. */
  255. public function get($key, $strictness = IGNORE_MISSING) {
  256. // 1. Parse the key.
  257. $parsedkey = $this->parse_key($key);
  258. // 2. Get it from the static acceleration array if we can (only when it is enabled and it has already been requested/set).
  259. $result = false;
  260. if ($this->use_static_acceleration()) {
  261. $result = $this->static_acceleration_get($parsedkey);
  262. }
  263. if ($result !== false) {
  264. if (!is_scalar($result)) {
  265. // If data is an object it will be a reference.
  266. // If data is an array if may contain references.
  267. // We want to break references so that the cache cannot be modified outside of itself.
  268. // Call the function to unreference it (in the best way possible).
  269. $result = $this->unref($result);
  270. }
  271. return $result;
  272. }
  273. // 3. Get it from the store. Obviously wasn't in the static acceleration array.
  274. $result = $this->store->get($parsedkey);
  275. if ($result !== false) {
  276. if ($result instanceof cache_ttl_wrapper) {
  277. if ($result->has_expired()) {
  278. $this->store->delete($parsedkey);
  279. $result = false;
  280. } else {
  281. $result = $result->data;
  282. }
  283. }
  284. if ($result instanceof cache_cached_object) {
  285. $result = $result->restore_object();
  286. }
  287. if ($this->use_static_acceleration()) {
  288. $this->static_acceleration_set($parsedkey, $result);
  289. }
  290. }
  291. // 4. Load if from the loader/datasource if we don't already have it.
  292. $setaftervalidation = false;
  293. if ($result === false) {
  294. if ($this->perfdebug) {
  295. cache_helper::record_cache_miss($this->storetype, $this->definition->get_id());
  296. }
  297. if ($this->loader !== false) {
  298. // We must pass the original (unparsed) key to the next loader in the chain.
  299. // The next loader will parse the key as it sees fit. It may be parsed differently
  300. // depending upon the capabilities of the store associated with the loader.
  301. $result = $this->loader->get($key);
  302. } else if ($this->datasource !== false) {
  303. $result = $this->datasource->load_for_cache($key);
  304. }
  305. $setaftervalidation = ($result !== false);
  306. } else if ($this->perfdebug) {
  307. cache_helper::record_cache_hit($this->storetype, $this->definition->get_id());
  308. }
  309. // 5. Validate strictness.
  310. if ($strictness === MUST_EXIST && $result === false) {
  311. throw new coding_exception('Requested key did not exist in any cache stores and could not be loaded.');
  312. }
  313. // 6. Set it to the store if we got it from the loader/datasource.
  314. if ($setaftervalidation) {
  315. $this->set($key, $result);
  316. }
  317. // 7. Make sure we don't pass back anything that could be a reference.
  318. // We don't want people modifying the data in the cache.
  319. if (!is_scalar($result)) {
  320. // If data is an object it will be a reference.
  321. // If data is an array if may contain references.
  322. // We want to break references so that the cache cannot be modified outside of itself.
  323. // Call the function to unreference it (in the best way possible).
  324. $result = $this->unref($result);
  325. }
  326. return $result;
  327. }
  328. /**
  329. * Retrieves an array of values for an array of keys.
  330. *
  331. * Using this function comes with potential performance implications.
  332. * Not all cache stores will support get_many/set_many operations and in order to replicate this functionality will call
  333. * the equivalent singular method for each item provided.
  334. * This should not deter you from using this function as there is a performance benefit in situations where the cache store
  335. * does support it, but you should be aware of this fact.
  336. *
  337. * @param array $keys The keys of the data being requested.
  338. * Each key can be any structure although using a scalar string or int is recommended in the interests of performance.
  339. * In advanced cases an array may be useful such as in situations requiring the multi-key functionality.
  340. * @param int $strictness One of IGNORE_MISSING or MUST_EXIST.
  341. * @return array An array of key value pairs for the items that could be retrieved from the cache.
  342. * If MUST_EXIST was used and not all keys existed within the cache then an exception will be thrown.
  343. * Otherwise any key that did not exist will have a data value of false within the results.
  344. * @throws coding_exception
  345. */
  346. public function get_many(array $keys, $strictness = IGNORE_MISSING) {
  347. $keysparsed = array();
  348. $parsedkeys = array();
  349. $resultpersist = array();
  350. $resultstore = array();
  351. $keystofind = array();
  352. // First up check the persist cache for each key.
  353. $isusingpersist = $this->use_static_acceleration();
  354. foreach ($keys as $key) {
  355. $pkey = $this->parse_key($key);
  356. $keysparsed[$key] = $pkey;
  357. $parsedkeys[$pkey] = $key;
  358. $keystofind[$pkey] = $key;
  359. if ($isusingpersist) {
  360. $value = $this->static_acceleration_get($pkey);
  361. if ($value !== false) {
  362. $resultpersist[$pkey] = $value;
  363. unset($keystofind[$pkey]);
  364. }
  365. }
  366. }
  367. // Next assuming we didn't find all of the keys in the persist cache try loading them from the store.
  368. if (count($keystofind)) {
  369. $resultstore = $this->store->get_many(array_keys($keystofind));
  370. // Process each item in the result to "unwrap" it.
  371. foreach ($resultstore as $key => $value) {
  372. if ($value instanceof cache_ttl_wrapper) {
  373. if ($value->has_expired()) {
  374. $value = false;
  375. } else {
  376. $value = $value->data;
  377. }
  378. }
  379. if ($value instanceof cache_cached_object) {
  380. $value = $value->restore_object();
  381. }
  382. if ($value !== false && $this->use_static_acceleration()) {
  383. $this->static_acceleration_set($key, $value);
  384. }
  385. $resultstore[$key] = $value;
  386. }
  387. }
  388. // Merge the result from the persis cache with the results from the store load.
  389. $result = $resultpersist + $resultstore;
  390. unset($resultpersist);
  391. unset($resultstore);
  392. // Next we need to find any missing values and load them from the loader/datasource next in the chain.
  393. $usingloader = ($this->loader !== false);
  394. $usingsource = (!$usingloader && ($this->datasource !== false));
  395. if ($usingloader || $usingsource) {
  396. $missingkeys = array();
  397. foreach ($result as $key => $value) {
  398. if ($value === false) {
  399. $missingkeys[] = $parsedkeys[$key];
  400. }
  401. }
  402. if (!empty($missingkeys)) {
  403. if ($usingloader) {
  404. $resultmissing = $this->loader->get_many($missingkeys);
  405. } else {
  406. $resultmissing = $this->datasource->load_many_for_cache($missingkeys);
  407. }
  408. foreach ($resultmissing as $key => $value) {
  409. $result[$keysparsed[$key]] = $value;
  410. if ($value !== false) {
  411. $this->set($key, $value);
  412. }
  413. }
  414. unset($resultmissing);
  415. }
  416. unset($missingkeys);
  417. }
  418. // Create an array with the original keys and the found values. This will be what we return.
  419. $fullresult = array();
  420. foreach ($result as $key => $value) {
  421. $fullresult[$parsedkeys[$key]] = $value;
  422. }
  423. unset($result);
  424. // Final step is to check strictness.
  425. if ($strictness === MUST_EXIST) {
  426. foreach ($keys as $key) {
  427. if (!array_key_exists($key, $fullresult)) {
  428. throw new coding_exception('Not all the requested keys existed within the cache stores.');
  429. }
  430. }
  431. }
  432. if ($this->perfdebug) {
  433. $hits = 0;
  434. $misses = 0;
  435. foreach ($fullresult as $value) {
  436. if ($value === false) {
  437. $misses++;
  438. } else {
  439. $hits++;
  440. }
  441. }
  442. cache_helper::record_cache_hit($this->storetype, $this->definition->get_id(), $hits);
  443. cache_helper::record_cache_miss($this->storetype, $this->definition->get_id(), $misses);
  444. }
  445. // Return the result. Phew!
  446. return $fullresult;
  447. }
  448. /**
  449. * Sends a key => value pair to the cache.
  450. *
  451. * <code>
  452. * // This code will add four entries to the cache, one for each url.
  453. * $cache->set('main', 'http://moodle.org');
  454. * $cache->set('docs', 'http://docs.moodle.org');
  455. * $cache->set('tracker', 'http://tracker.moodle.org');
  456. * $cache->set('qa', 'http://qa.moodle.net');
  457. * </code>
  458. *
  459. * @param string|int $key The key for the data being requested.
  460. * It can be any structure although using a scalar string or int is recommended in the interests of performance.
  461. * In advanced cases an array may be useful such as in situations requiring the multi-key functionality.
  462. * @param mixed $data The data to set against the key.
  463. * @return bool True on success, false otherwise.
  464. */
  465. public function set($key, $data) {
  466. if ($this->perfdebug) {
  467. cache_helper::record_cache_set($this->storetype, $this->definition->get_id());
  468. }
  469. if ($this->loader !== false) {
  470. // We have a loader available set it there as well.
  471. // We have to let the loader do its own parsing of data as it may be unique.
  472. $this->loader->set($key, $data);
  473. }
  474. if (is_object($data) && $data instanceof cacheable_object) {
  475. $data = new cache_cached_object($data);
  476. } else if (!is_scalar($data)) {
  477. // If data is an object it will be a reference.
  478. // If data is an array if may contain references.
  479. // We want to break references so that the cache cannot be modified outside of itself.
  480. // Call the function to unreference it (in the best way possible).
  481. $data = $this->unref($data);
  482. }
  483. if ($this->has_a_ttl() && !$this->store_supports_native_ttl()) {
  484. $data = new cache_ttl_wrapper($data, $this->definition->get_ttl());
  485. }
  486. $parsedkey = $this->parse_key($key);
  487. if ($this->use_static_acceleration()) {
  488. $this->static_acceleration_set($parsedkey, $data);
  489. }
  490. return $this->store->set($parsedkey, $data);
  491. }
  492. /**
  493. * Removes references where required.
  494. *
  495. * @param stdClass|array $data
  496. * @return mixed What ever was put in but without any references.
  497. */
  498. protected function unref($data) {
  499. if ($this->definition->uses_simple_data()) {
  500. return $data;
  501. }
  502. // Check if it requires serialisation in order to produce a reference free copy.
  503. if ($this->requires_serialisation($data)) {
  504. // Damn, its going to have to be serialise.
  505. $data = serialize($data);
  506. // We unserialise immediately so that we don't have to do it every time on get.
  507. $data = unserialize($data);
  508. } else if (!is_scalar($data)) {
  509. // Its safe to clone, lets do it, its going to beat the pants of serialisation.
  510. $data = $this->deep_clone($data);
  511. }
  512. return $data;
  513. }
  514. /**
  515. * Checks to see if a var requires serialisation.
  516. *
  517. * @param mixed $value The value to check.
  518. * @param int $depth Used to ensure we don't enter an endless loop (think recursion).
  519. * @return bool Returns true if the value is going to require serialisation in order to ensure a reference free copy
  520. * or false if its safe to clone.
  521. */
  522. protected function requires_serialisation($value, $depth = 1) {
  523. if (is_scalar($value)) {
  524. return false;
  525. } else if (is_array($value) || $value instanceof stdClass || $value instanceof Traversable) {
  526. if ($depth > 5) {
  527. // Skrew it, mega-deep object, developer you suck, we're just going to serialise.
  528. return true;
  529. }
  530. foreach ($value as $key => $subvalue) {
  531. if ($this->requires_serialisation($subvalue, $depth++)) {
  532. return true;
  533. }
  534. }
  535. }
  536. // Its not scalar, array, or stdClass so we'll need to serialise.
  537. return true;
  538. }
  539. /**
  540. * Creates a reference free clone of the given value.
  541. *
  542. * @param mixed $value
  543. * @return mixed
  544. */
  545. protected function deep_clone($value) {
  546. if (is_object($value)) {
  547. // Objects must be cloned to begin with.
  548. $value = clone $value;
  549. }
  550. if (is_array($value) || is_object($value)) {
  551. foreach ($value as $key => $subvalue) {
  552. $value[$key] = $this->deep_clone($subvalue);
  553. }
  554. }
  555. return $value;
  556. }
  557. /**
  558. * Sends several key => value pairs to the cache.
  559. *
  560. * Using this function comes with potential performance implications.
  561. * Not all cache stores will support get_many/set_many operations and in order to replicate this functionality will call
  562. * the equivalent singular method for each item provided.
  563. * This should not deter you from using this function as there is a performance benefit in situations where the cache store
  564. * does support it, but you should be aware of this fact.
  565. *
  566. * <code>
  567. * // This code will add four entries to the cache, one for each url.
  568. * $cache->set_many(array(
  569. * 'main' => 'http://moodle.org',
  570. * 'docs' => 'http://docs.moodle.org',
  571. * 'tracker' => 'http://tracker.moodle.org',
  572. * 'qa' => ''http://qa.moodle.net'
  573. * ));
  574. * </code>
  575. *
  576. * @param array $keyvaluearray An array of key => value pairs to send to the cache.
  577. * @return int The number of items successfully set. It is up to the developer to check this matches the number of items.
  578. * ... if they care that is.
  579. */
  580. public function set_many(array $keyvaluearray) {
  581. if ($this->loader !== false) {
  582. // We have a loader available set it there as well.
  583. // We have to let the loader do its own parsing of data as it may be unique.
  584. $this->loader->set_many($keyvaluearray);
  585. }
  586. $data = array();
  587. $simulatettl = $this->has_a_ttl() && !$this->store_supports_native_ttl();
  588. $usestaticaccelerationarray = $this->use_static_acceleration();
  589. foreach ($keyvaluearray as $key => $value) {
  590. if (is_object($value) && $value instanceof cacheable_object) {
  591. $value = new cache_cached_object($value);
  592. } else if (!is_scalar($value)) {
  593. // If data is an object it will be a reference.
  594. // If data is an array if may contain references.
  595. // We want to break references so that the cache cannot be modified outside of itself.
  596. // Call the function to unreference it (in the best way possible).
  597. $value = $this->unref($value);
  598. }
  599. if ($simulatettl) {
  600. $value = new cache_ttl_wrapper($value, $this->definition->get_ttl());
  601. }
  602. $data[$key] = array(
  603. 'key' => $this->parse_key($key),
  604. 'value' => $value
  605. );
  606. if ($usestaticaccelerationarray) {
  607. $this->static_acceleration_set($data[$key]['key'], $value);
  608. }
  609. }
  610. $successfullyset = $this->store->set_many($data);
  611. if ($this->perfdebug && $successfullyset) {
  612. cache_helper::record_cache_set($this->storetype, $this->definition->get_id(), $successfullyset);
  613. }
  614. return $successfullyset;
  615. }
  616. /**
  617. * Test is a cache has a key.
  618. *
  619. * The use of the has methods is strongly discouraged. In a high load environment the cache may well change between the
  620. * test and any subsequent action (get, set, delete etc).
  621. * Instead it is recommended to write your code in such a way they it performs the following steps:
  622. * <ol>
  623. * <li>Attempt to retrieve the information.</li>
  624. * <li>Generate the information.</li>
  625. * <li>Attempt to set the information</li>
  626. * </ol>
  627. *
  628. * Its also worth mentioning that not all stores support key tests.
  629. * For stores that don't support key tests this functionality is mimicked by using the equivalent get method.
  630. * Just one more reason you should not use these methods unless you have a very good reason to do so.
  631. *
  632. * @param string|int $key
  633. * @param bool $tryloadifpossible If set to true, the cache doesn't contain the key, and there is another cache loader or
  634. * data source then the code will try load the key value from the next item in the chain.
  635. * @return bool True if the cache has the requested key, false otherwise.
  636. */
  637. public function has($key, $tryloadifpossible = false) {
  638. $parsedkey = $this->parse_key($key);
  639. if ($this->static_acceleration_has($parsedkey)) {
  640. // Hoorah, that was easy. It exists in the static acceleration array so we definitely have it.
  641. return true;
  642. }
  643. if ($this->has_a_ttl() && !$this->store_supports_native_ttl()) {
  644. // The data has a TTL and the store doesn't support it natively.
  645. // We must fetch the data and expect a ttl wrapper.
  646. $data = $this->store->get($parsedkey);
  647. $has = ($data instanceof cache_ttl_wrapper && !$data->has_expired());
  648. } else if (!$this->store_supports_key_awareness()) {
  649. // The store doesn't support key awareness, get the data and check it manually... puke.
  650. // Either no TTL is set of the store supports its handling natively.
  651. $data = $this->store->get($parsedkey);
  652. $has = ($data !== false);
  653. } else {
  654. // The store supports key awareness, this is easy!
  655. // Either no TTL is set of the store supports its handling natively.
  656. $has = $this->store->has($parsedkey);
  657. }
  658. if (!$has && $tryloadifpossible) {
  659. if ($this->loader !== false) {
  660. $result = $this->loader->get($parsedkey);
  661. } else if ($this->datasource !== null) {
  662. $result = $this->datasource->load_for_cache($key);
  663. }
  664. $has = ($result !== null);
  665. if ($has) {
  666. $this->set($key, $result);
  667. }
  668. }
  669. return $has;
  670. }
  671. /**
  672. * Test is a cache has all of the given keys.
  673. *
  674. * It is strongly recommended to avoid the use of this function if not absolutely required.
  675. * In a high load environment the cache may well change between the test and any subsequent action (get, set, delete etc).
  676. *
  677. * Its also worth mentioning that not all stores support key tests.
  678. * For stores that don't support key tests this functionality is mimicked by using the equivalent get method.
  679. * Just one more reason you should not use these methods unless you have a very good reason to do so.
  680. *
  681. * @param array $keys
  682. * @return bool True if the cache has all of the given keys, false otherwise.
  683. */
  684. public function has_all(array $keys) {
  685. if (($this->has_a_ttl() && !$this->store_supports_native_ttl()) || !$this->store_supports_key_awareness()) {
  686. foreach ($keys as $key) {
  687. if (!$this->has($key)) {
  688. return false;
  689. }
  690. }
  691. return true;
  692. }
  693. $parsedkeys = array_map(array($this, 'parse_key'), $keys);
  694. return $this->store->has_all($parsedkeys);
  695. }
  696. /**
  697. * Test if a cache has at least one of the given keys.
  698. *
  699. * It is strongly recommended to avoid the use of this function if not absolutely required.
  700. * In a high load environment the cache may well change between the test and any subsequent action (get, set, delete etc).
  701. *
  702. * Its also worth mentioning that not all stores support key tests.
  703. * For stores that don't support key tests this functionality is mimicked by using the equivalent get method.
  704. * Just one more reason you should not use these methods unless you have a very good reason to do so.
  705. *
  706. * @param array $keys
  707. * @return bool True if the cache has at least one of the given keys
  708. */
  709. public function has_any(array $keys) {
  710. if (($this->has_a_ttl() && !$this->store_supports_native_ttl()) || !$this->store_supports_key_awareness()) {
  711. foreach ($keys as $key) {
  712. if ($this->has($key)) {
  713. return true;
  714. }
  715. }
  716. return false;
  717. }
  718. if ($this->use_static_acceleration()) {
  719. $parsedkeys = array();
  720. foreach ($keys as $id => $key) {
  721. $parsedkey = $this->parse_key($key);
  722. if ($this->static_acceleration_has($parsedkey)) {
  723. return true;
  724. }
  725. $parsedkeys[] = $parsedkey;
  726. }
  727. } else {
  728. $parsedkeys = array_map(array($this, 'parse_key'), $keys);
  729. }
  730. return $this->store->has_any($parsedkeys);
  731. }
  732. /**
  733. * Delete the given key from the cache.
  734. *
  735. * @param string|int $key The key to delete.
  736. * @param bool $recurse When set to true the key will also be deleted from all stacked cache loaders and their stores.
  737. * This happens by default and ensure that all the caches are consistent. It is NOT recommended to change this.
  738. * @return bool True of success, false otherwise.
  739. */
  740. public function delete($key, $recurse = true) {
  741. $parsedkey = $this->parse_key($key);
  742. $this->static_acceleration_delete($parsedkey);
  743. if ($recurse && $this->loader !== false) {
  744. // Delete from the bottom of the stack first.
  745. $this->loader->delete($key, $recurse);
  746. }
  747. return $this->store->delete($parsedkey);
  748. }
  749. /**
  750. * Delete all of the given keys from the cache.
  751. *
  752. * @param array $keys The key to delete.
  753. * @param bool $recurse When set to true the key will also be deleted from all stacked cache loaders and their stores.
  754. * This happens by default and ensure that all the caches are consistent. It is NOT recommended to change this.
  755. * @return int The number of items successfully deleted.
  756. */
  757. public function delete_many(array $keys, $recurse = true) {
  758. $parsedkeys = array_map(array($this, 'parse_key'), $keys);
  759. if ($this->use_static_acceleration()) {
  760. foreach ($parsedkeys as $parsedkey) {
  761. $this->static_acceleration_delete($parsedkey);
  762. }
  763. }
  764. if ($recurse && $this->loader !== false) {
  765. // Delete from the bottom of the stack first.
  766. $this->loader->delete_many($keys, $recurse);
  767. }
  768. return $this->store->delete_many($parsedkeys);
  769. }
  770. /**
  771. * Purges the cache store, and loader if there is one.
  772. *
  773. * @return bool True on success, false otherwise
  774. */
  775. public function purge() {
  776. // 1. Purge the static acceleration array.
  777. $this->staticaccelerationarray = array();
  778. if ($this->staticaccelerationsize !== false) {
  779. $this->staticaccelerationkeys = array();
  780. $this->staticaccelerationcount = 0;
  781. }
  782. // 2. Purge the store.
  783. $this->store->purge();
  784. // 3. Optionally pruge any stacked loaders.
  785. if ($this->loader) {
  786. $this->loader->purge();
  787. }
  788. return true;
  789. }
  790. /**
  791. * Parses the key turning it into a string (or array is required) suitable to be passed to the cache store.
  792. *
  793. * @param string|int $key As passed to get|set|delete etc.
  794. * @return string|array String unless the store supports multi-identifiers in which case an array if returned.
  795. */
  796. protected function parse_key($key) {
  797. // First up if the store supports multiple keys we'll go with that.
  798. if ($this->store->supports_multiple_identifiers()) {
  799. $result = $this->definition->generate_multi_key_parts();
  800. $result['key'] = $key;
  801. return $result;
  802. }
  803. // If not we need to generate a hash and to for that we use the cache_helper.
  804. return cache_helper::hash_key($key, $this->definition);
  805. }
  806. /**
  807. * Returns true if the cache is making use of a ttl.
  808. * @return bool
  809. */
  810. protected function has_a_ttl() {
  811. return $this->hasattl;
  812. }
  813. /**
  814. * Returns true if the cache store supports native ttl.
  815. * @return bool
  816. */
  817. protected function store_supports_native_ttl() {
  818. if ($this->supportsnativettl === null) {
  819. $this->supportsnativettl = ($this->store->supports_native_ttl());
  820. }
  821. return $this->supportsnativettl;
  822. }
  823. /**
  824. * Returns the cache definition.
  825. *
  826. * @return cache_definition
  827. */
  828. protected function get_definition() {
  829. return $this->definition;
  830. }
  831. /**
  832. * Returns the cache store
  833. *
  834. * @return cache_store
  835. */
  836. protected function get_store() {
  837. return $this->store;
  838. }
  839. /**
  840. * Returns the loader associated with this instance.
  841. *
  842. * @since 2.4.4
  843. * @return cache|false
  844. */
  845. protected function get_loader() {
  846. return $this->loader;
  847. }
  848. /**
  849. * Returns the data source associated with this cache.
  850. *
  851. * @since 2.4.4
  852. * @return cache_data_source|false
  853. */
  854. protected function get_datasource() {
  855. return $this->datasource;
  856. }
  857. /**
  858. * Returns true if the store supports key awareness.
  859. *
  860. * @return bool
  861. */
  862. protected function store_supports_key_awareness() {
  863. if ($this->supportskeyawareness === null) {
  864. $this->supportskeyawareness = ($this->store instanceof cache_is_key_aware);
  865. }
  866. return $this->supportskeyawareness;
  867. }
  868. /**
  869. * Returns true if the store natively supports locking.
  870. *
  871. * @return bool
  872. */
  873. protected function store_supports_native_locking() {
  874. if ($this->nativelocking === null) {
  875. $this->nativelocking = ($this->store instanceof cache_is_lockable);
  876. }
  877. return $this->nativelocking;
  878. }
  879. /**
  880. * Returns true if this cache is making use of the static acceleration array.
  881. *
  882. * @deprecated since 2.6
  883. * @see cache::use_static_acceleration()
  884. * @return bool
  885. */
  886. protected function is_using_persist_cache() {
  887. debugging('This function has been deprecated. Please call use_static_acceleration instead', DEBUG_DEVELOPER);
  888. return $this->use_static_acceleration();
  889. }
  890. /**
  891. * Returns true if this cache is making use of the static acceleration array.
  892. *
  893. * @return bool
  894. */
  895. protected function use_static_acceleration() {
  896. return $this->staticacceleration;
  897. }
  898. /**
  899. * Returns true if the requested key exists within the static acceleration array.
  900. *
  901. * @see cache::static_acceleration_has
  902. * @deprecated since 2.6
  903. * @param string $key The parsed key
  904. * @return bool
  905. */
  906. protected function is_in_persist_cache($key) {
  907. debugging('This function has been deprecated. Please call static_acceleration_has instead', DEBUG_DEVELOPER);
  908. return $this->static_acceleration_has($key);
  909. }
  910. /**
  911. * Returns true if the requested key exists within the static acceleration array.
  912. *
  913. * @param string $key The parsed key
  914. * @return bool
  915. */
  916. protected function static_acceleration_has($key) {
  917. // This method of checking if an array was supplied is faster than is_array.
  918. if ($key === (array)$key) {
  919. $key = $key['key'];
  920. }
  921. // This could be written as a single line, however it has been split because the ttl check is faster than the instanceof
  922. // and has_expired calls.
  923. if (!$this->staticacceleration || !array_key_exists($key, $this->staticaccelerationarray)) {
  924. return false;
  925. }
  926. if ($this->has_a_ttl() && $this->store_supports_native_ttl()) {
  927. return !($this->staticaccelerationarray[$key] instanceof cache_ttl_wrapper &&
  928. $this->staticaccelerationarray[$key]->has_expired());
  929. }
  930. return true;
  931. }
  932. /**
  933. * Returns the item from the static acceleration array if it exists there.
  934. *
  935. * @deprecated since 2.6
  936. * @see cache::static_acceleration_get
  937. * @param string $key The parsed key
  938. * @return mixed|false The data from the static acceleration array or false if it wasn't there.
  939. */
  940. protected function get_from_persist_cache($key) {
  941. debugging('This function has been deprecated. Please call static_acceleration_get instead', DEBUG_DEVELOPER);
  942. return $this->static_acceleration_get($key);
  943. }
  944. /**
  945. * Returns the item from the static acceleration array if it exists there.
  946. *
  947. * @param string $key The parsed key
  948. * @return mixed|false The data from the static acceleration array or false if it wasn't there.
  949. */
  950. protected function static_acceleration_get($key) {
  951. // This method of checking if an array was supplied is faster than is_array.
  952. if ($key === (array)$key) {
  953. $key = $key['key'];
  954. }
  955. // This isset check is faster than array_key_exists but will return false
  956. // for null values, meaning null values will come from backing store not
  957. // the static acceleration array. We think this okay because null usage should be
  958. // very rare (see comment in MDL-39472).
  959. if (!$this->staticacceleration || !isset($this->staticaccelerationarray[$key])) {
  960. $result = false;
  961. } else {
  962. $data = $this->staticaccelerationarray[$key];
  963. if (!$this->has_a_ttl() || !$data instanceof cache_ttl_wrapper) {
  964. if ($data instanceof cache_cached_object) {
  965. $data = $data->restore_object();
  966. }
  967. $result = $data;
  968. } else if ($data->has_expired()) {
  969. $this->static_acceleration_delete($key);
  970. $result = false;
  971. } else {
  972. if ($data instanceof cache_cached_object) {
  973. $data = $data->restore_object();
  974. }
  975. $result = $data->data;
  976. }
  977. }
  978. if ($result) {
  979. if ($this->perfdebug) {
  980. cache_helper::record_cache_hit('** static acceleration **', $this->definition->get_id());
  981. }
  982. if ($this->staticaccelerationsize > 1 && $this->staticaccelerationcount > 1) {
  983. // Check to see if this is the last item on the static acceleration keys array.
  984. if (end($this->staticaccelerationkeys) !== $key) {
  985. // It isn't the last item.
  986. // Move the item to the end of the array so that it is last to be removed.
  987. unset($this->staticaccelerationkeys[$key]);
  988. $this->staticaccelerationkeys[$key] = $key;
  989. }
  990. }
  991. return $result;
  992. } else {
  993. if ($this->perfdebug) {
  994. cache_helper::record_cache_miss('** static acceleration **', $this->definition->get_id());
  995. }
  996. return false;
  997. }
  998. }
  999. /**
  1000. * Sets a key value pair into the static acceleration array.
  1001. *
  1002. * @deprecated since 2.6
  1003. * @see cache::static_acceleration_set
  1004. * @param string $key The parsed key
  1005. * @param mixed $data
  1006. * @return bool
  1007. */
  1008. protected function set_in_persist_cache($key, $data) {
  1009. debugging('This function has been deprecated. Please call static_acceleration_set instead', DEBUG_DEVELOPER);
  1010. return $this->static_acceleration_set($key, $data);
  1011. }
  1012. /**
  1013. * Sets a key value pair into the static acceleration array.
  1014. *
  1015. * @param string $key The parsed key
  1016. * @param mixed $data
  1017. * @return bool
  1018. */
  1019. protected function static_acceleration_set($key, $data) {
  1020. // This method of checking if an array was supplied is faster than is_array.
  1021. if ($key === (array)$key) {
  1022. $key = $key['key'];
  1023. }
  1024. if ($this->staticaccelerationsize !== false && isset($this->staticaccelerationkeys[$key])) {
  1025. $this->staticaccelerationcount--;
  1026. unset($this->staticaccelerationkeys[$key]);
  1027. }
  1028. $this->staticaccelerationarray[$key] = $data;
  1029. if ($this->staticaccelerationsize !== false) {
  1030. $this->staticaccelerationcount++;
  1031. $this->staticaccelerationkeys[$key] = $key;
  1032. if ($this->staticaccelerationcount > $this->staticaccelerationsize) {
  1033. $dropkey = array_shift($this->staticaccelerationkeys);
  1034. unset($this->staticaccelerationarray[$dropkey]);
  1035. $this->staticaccelerationcount--;
  1036. }
  1037. }
  1038. return true;
  1039. }
  1040. /**
  1041. * Deletes an item from the static acceleration array.
  1042. *
  1043. * @deprecated since 2.6
  1044. * @see cache::static_acceleration_delete()
  1045. * @param string|int $key As given to get|set|delete
  1046. * @return bool True on success, false otherwise.
  1047. */
  1048. protected function delete_from_persist_cache($key) {
  1049. debugging('This function has been deprecated. Please call static_acceleration_delete instead', DEBUG_DEVELOPER);
  1050. return $this->static_acceleration_delete($key);
  1051. }
  1052. /**
  1053. * Deletes an item from the static acceleration array.
  1054. *
  1055. * @param string|int $key As given to get|set|delete
  1056. * @return bool True on success, false otherwise.
  1057. */
  1058. protected function static_acceleration_delete($key) {
  1059. unset($this->staticaccelerationarray[$key]);
  1060. if ($this->staticaccelerationsize !== false) {
  1061. $dropkey = array_search($key, $this->staticaccelerationkeys);
  1062. if ($dropkey) {
  1063. unset($this->staticaccelerationkeys[$dropkey]);
  1064. $this->staticaccelerationcount--;
  1065. }
  1066. }
  1067. return true;
  1068. }
  1069. /**
  1070. * Returns the timestamp from the first request for the time from the cache API.
  1071. *
  1072. * This stamp needs to be used for all ttl and time based operations to ensure that we don't end up with
  1073. * timing issues.
  1074. *
  1075. * @return int
  1076. */
  1077. public static function now() {
  1078. if (self::$now === null) {
  1079. self::$now = time();
  1080. }
  1081. return self::$now;
  1082. }
  1083. }
  1084. /**
  1085. * An application cache.
  1086. *
  1087. * This class is used for application caches returned by the cache::make methods.
  1088. * On top of the standard functionality it also allows locking to be required and or manually operated.
  1089. *
  1090. * This cache class should never be interacted with directly. Instead you should always use the cache::make methods.
  1091. * It is technically possible to call those methods through this class however there is no guarantee that you will get an
  1092. * instance of this class back again.
  1093. *
  1094. * @internal don't use me directly.
  1095. *
  1096. * @package core
  1097. * @category cache
  1098. * @copyright 2012 Sam Hemelryk
  1099. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  1100. */
  1101. class cache_application extends cache implements cache_loader_with_locking {
  1102. /**
  1103. * Lock identifier.
  1104. * This is used to ensure the lock belongs to the cache instance + definition + user.
  1105. * @var string
  1106. */
  1107. protected $lockidentifier;
  1108. /**
  1109. * Gets set to true if the cache's primary store natively supports locking.
  1110. * If it does then we use that, otherwise we need to instantiate a second store to use for locking.
  1111. * @var cache_store
  1112. */
  1113. protected $nativelocking = null;
  1114. /**
  1115. * Gets set to true if the cache is going to be using locking.
  1116. * This isn't a requirement, it doesn't need to use locking (most won't) and this bool is used to quickly check things.
  1117. * If required then locking will be forced for the get|set|delete operation.
  1118. * @var bool
  1119. */
  1120. protected $requirelocking = false;
  1121. /**
  1122. * Gets set to true if the cache must use read locking (get|has).
  1123. * @var bool
  1124. */
  1125. protected $requirelockingread = false;
  1126. /**
  1127. * Gets set to true if the cache must use write locking (set|delete)
  1128. * @var bool
  1129. */
  1130. protected $requirelockingwrite = false;
  1131. /**
  1132. * Gets set to a cache_store to use for locking if the caches primary store doesn't support locking natively.
  1133. * @var cache_lock_interface
  1134. */
  1135. protected $cachelockinstance;
  1136. /**
  1137. * Overrides the cache construct method.
  1138. *
  1139. * You should not call this method from your code, instead you should use the cache::make methods.
  1140. *
  1141. * @param cache_definition $definition
  1142. * @param cache_store $store
  1143. * @param cache_loader|cache_data_source $loader
  1144. */
  1145. public function __construct(cache_definition $definition, cache_store $store, $loader = null) {
  1146. parent::__construct($definition, $store, $loader);
  1147. $this->nativelocking = $this->store_supports_native_locking();
  1148. if ($definition->require_locking()) {
  1149. $this->requirelocking = true;
  1150. $this->requirelockingread = $definition->require_locking_read();
  1151. $this->requirelockingwrite = $definition->require_locking_write();

Large files files are truncated, but you can click here to view the full file