PageRenderTime 29ms CodeModel.GetById 43ms RepoModel.GetById 5ms app.codeStats 0ms

/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
  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();
  1152. }
  1153. if ($definition->has_invalidation_events()) {
  1154. $lastinvalidation = $this->get('lastinvalidation');
  1155. if ($lastinvalidation === false) {
  1156. // This is a new session, there won't be anything to invalidate. Set the time of the last invalidation and
  1157. // move on.
  1158. $this->set('lastinvalidation', cache::now());
  1159. return;
  1160. } else if ($lastinvalidation == cache::now()) {
  1161. // We've already invalidated during this request.
  1162. return;
  1163. }
  1164. // Get the event invalidation cache.
  1165. $cache = cache::make('core', 'eventinvalidation');
  1166. $events = $cache->get_many($definition->get_invalidation_events());
  1167. $todelete = array();
  1168. $purgeall = false;
  1169. // Iterate the returned data for the events.
  1170. foreach ($events as $event => $keys) {
  1171. if ($keys === false) {
  1172. // No data to be invalidated yet.
  1173. continue;
  1174. }
  1175. // Look at each key and check the timestamp.
  1176. foreach ($keys as $key => $timestamp) {
  1177. // If the timestamp of the event is more than or equal to the last invalidation (happened between the last
  1178. // invalidation and now)then we need to invaliate the key.
  1179. if ($timestamp >= $lastinvalidation) {
  1180. if ($key === 'purged') {
  1181. $purgeall = true;
  1182. break;
  1183. } else {
  1184. $todelete[] = $key;
  1185. }
  1186. }
  1187. }
  1188. }
  1189. if ($purgeall) {
  1190. $this->purge();
  1191. } else if (!empty($todelete)) {
  1192. $todelete = array_unique($todelete);
  1193. $this->delete_many($todelete);
  1194. }
  1195. // Set the time of the last invalidation.
  1196. $this->set('lastinvalidation', cache::now());
  1197. }
  1198. }
  1199. /**
  1200. * Returns the identifier to use
  1201. *
  1202. * @staticvar int $instances Counts the number of instances. Used as part of the lock identifier.
  1203. * @return string
  1204. */
  1205. public function get_identifier() {
  1206. static $instances = 0;
  1207. if ($this->lockidentifier === null) {
  1208. $this->lockidentifier = md5(
  1209. $this->get_definition()->generate_definition_hash() .
  1210. sesskey() .
  1211. $instances++ .
  1212. 'cache_application'
  1213. );
  1214. }
  1215. return $this->lockidentifier;
  1216. }
  1217. /**
  1218. * Fixes the instance up after a clone.
  1219. */
  1220. public function __clone() {
  1221. // Force a new idenfitier.
  1222. $this->lockidentifier = null;
  1223. }
  1224. /**
  1225. * Acquires a lock on the given key.
  1226. *
  1227. * This is done automatically if the definition requires it.
  1228. * It is recommended to use a definition if you want to have locking although it is possible to do locking without having
  1229. * it required by the definition.
  1230. * The problem with such an approach is that you cannot ensure that code will consistently use locking. You will need to
  1231. * rely on the integrators review skills.
  1232. *
  1233. * @param string|int $key The key as given to get|set|delete
  1234. * @return bool Returns true if the lock could be acquired, false otherwise.
  1235. */
  1236. public function acquire_lock($key) {
  1237. $key = $this->parse_key($key);
  1238. if ($this->nativelocking) {
  1239. return $this->get_store()->acquire_lock($key, $this->get_identifier());
  1240. } else {
  1241. $this->ensure_cachelock_available();
  1242. return $this->cachelockinstance->lock($key, $this->get_identifier());
  1243. }
  1244. }
  1245. /**
  1246. * Checks if this cache has a lock on the given key.
  1247. *
  1248. * @param string|int $key The key as given to get|set|delete
  1249. * @return bool|null Returns true if there is a lock and this cache has it, null if no one has a lock on that key, false if
  1250. * someone else has the lock.
  1251. */
  1252. public function check_lock_state($key) {
  1253. $key = $this->parse_key($key);
  1254. if ($this->nativelocking) {
  1255. return $this->get_store()->check_lock_state($key, $this->get_identifier());
  1256. } else {
  1257. $this->ensure_cachelock_available();
  1258. return $this->cachelockinstance->check_state($key, $this->get_identifier());
  1259. }
  1260. }
  1261. /**
  1262. * Releases the lock this cache has on the given key
  1263. *
  1264. * @param string|int $key
  1265. * @return bool True if the operation succeeded, false otherwise.
  1266. */
  1267. public function release_lock($key) {
  1268. $key = $this->parse_key($key);
  1269. if ($this->nativelocking) {
  1270. return $this->get_store()->release_lock($key, $this->get_identifier());
  1271. } else {
  1272. $this->ensure_cachelock_available();
  1273. return $this->cachelockinstance->unlock($key, $this->get_identifier());
  1274. }
  1275. }
  1276. /**
  1277. * Ensure that the dedicated lock store is ready to go.
  1278. *
  1279. * This should only happen if the cache store doesn't natively support it.
  1280. */
  1281. protected function ensure_cachelock_available() {
  1282. if ($this->cachelockinstance === null) {
  1283. $this->cachelockinstance = cache_helper::get_cachelock_for_store($this->get_store());
  1284. }
  1285. }
  1286. /**
  1287. * Sends a key => value pair to the cache.
  1288. *
  1289. * <code>
  1290. * // This code will add four entries to the cache, one for each url.
  1291. * $cache->set('main', 'http://moodle.org');
  1292. * $cache->set('docs', 'http://docs.moodle.org');
  1293. * $cache->set('tracker', 'http://tracker.moodle.org');
  1294. * $cache->set('qa', 'http://qa.moodle.net');
  1295. * </code>
  1296. *
  1297. * @param string|int $key The key for the data being requested.
  1298. * @param mixed $data The data to set against the key.
  1299. * @return bool True on success, false otherwise.
  1300. */
  1301. public function set($key, $data) {
  1302. if ($this->requirelockingwrite && !$this->acquire_lock($key)) {
  1303. return false;
  1304. }
  1305. $result = parent::set($key, $data);
  1306. if ($this->requirelockingwrite && !$this->release_lock($key)) {
  1307. debugging('Failed to release cache lock on set operation... this should not happen.', DEBUG_DEVELOPER);
  1308. }
  1309. return $result;
  1310. }
  1311. /**
  1312. * Sends several key => value pairs to the cache.
  1313. *
  1314. * Using this function comes with potential performance implications.
  1315. * Not all cache stores will support get_many/set_many operations and in order to replicate this functionality will call
  1316. * the equivalent singular method for each item provided.
  1317. * This should not deter you from using this function as there is a performance benefit in situations where the cache store
  1318. * does support it, but you should be aware of this fact.
  1319. *
  1320. * <code>
  1321. * // This code will add four entries to the cache, one for each url.
  1322. * $cache->set_many(array(
  1323. * 'main' => 'http://moodle.org',
  1324. * 'docs' => 'http://docs.moodle.org',
  1325. * 'tracker' => 'http://tracker.moodle.org',
  1326. * 'qa' => ''http://qa.moodle.net'
  1327. * ));
  1328. * </code>
  1329. *
  1330. * @param array $keyvaluearray An array of key => value pairs to send to the cache.
  1331. * @return int The number of items successfully set. It is up to the developer to check this matches the number of items.
  1332. * ... if they care that is.
  1333. */
  1334. public function set_many(array $keyvaluearray) {
  1335. if ($this->requirelockingwrite) {
  1336. $locks = array();
  1337. foreach ($keyvaluearray as $id => $pair) {
  1338. $key = $pair['key'];
  1339. if ($this->acquire_lock($key)) {
  1340. $locks[] = $key;
  1341. } else {
  1342. unset($keyvaluearray[$id]);
  1343. }
  1344. }
  1345. }
  1346. $result = parent::set_many($keyvaluearray);
  1347. if ($this->requirelockingwrite) {
  1348. foreach ($locks as $key) {
  1349. if ($this->release_lock($key)) {
  1350. debugging('Failed to release cache lock on set_many operation... this should not happen.', DEBUG_DEVELOPER);
  1351. }
  1352. }
  1353. }
  1354. return $result;
  1355. }
  1356. /**
  1357. * Retrieves the value for the given key from the cache.
  1358. *
  1359. * @param string|int $key The key for the data being requested.
  1360. * @param int $strictness One of IGNORE_MISSING | MUST_EXIST
  1361. * @return mixed|false The data from the cache or false if the key did not exist within the cache.
  1362. */
  1363. public function get($key, $strictness = IGNORE_MISSING) {
  1364. if ($this->requirelockingread && $this->check_lock_state($key) === false) {
  1365. // Read locking required and someone else has the read lock.
  1366. return false;
  1367. }
  1368. return parent::get($key, $strictness);
  1369. }
  1370. /**
  1371. * Retrieves an array of values for an array of keys.
  1372. *
  1373. * Using this function comes with potential performance implications.
  1374. * Not all cache stores will support get_many/set_many operations and in order to replicate this functionality will call
  1375. * the equivalent singular method for each item provided.
  1376. * This should not deter you from using this function as there is a performance benefit in situations where the cache store
  1377. * does support it, but you should be aware of this fact.
  1378. *
  1379. * @param array $keys The keys of the data being requested.
  1380. * @param int $strictness One of IGNORE_MISSING or MUST_EXIST.
  1381. * @return array An array of key value pairs for the items that could be retrieved from the cache.
  1382. * If MUST_EXIST was used and not all keys existed within the cache then an exception will be thrown.
  1383. * Otherwise any key that did not exist will have a data value of false within the results.
  1384. * @throws coding_exception
  1385. */
  1386. public function get_many(array $keys, $strictness = IGNORE_MISSING) {
  1387. if ($this->requirelockingread) {
  1388. foreach ($keys as $id => $key) {
  1389. $lock =$this->acquire_lock($key);
  1390. if (!$lock) {
  1391. if ($strictness === MUST_EXIST) {
  1392. throw new coding_exception('Could not acquire read locks for all of the items being requested.');
  1393. } else {
  1394. // Can't return this as we couldn't get a read lock.
  1395. unset($keys[$id]);
  1396. }
  1397. }
  1398. }
  1399. }
  1400. return parent::get_many($keys, $strictness);
  1401. }
  1402. /**
  1403. * Delete the given key from the cache.
  1404. *
  1405. * @param string|int $key The key to delete.
  1406. * @param bool $recurse When set to true the key will also be deleted from all stacked cache loaders and their stores.
  1407. * This happens by default and ensure that all the caches are consistent. It is NOT recommended to change this.
  1408. * @return bool True of success, false otherwise.
  1409. */
  1410. public function delete($key, $recurse = true) {
  1411. if ($this->requirelockingwrite && !$this->acquire_lock($key)) {
  1412. return false;
  1413. }
  1414. $result = parent::delete($key, $recurse);
  1415. if ($this->requirelockingwrite && !$this->release_lock($key)) {
  1416. debugging('Failed to release cache lock on delete operation... this should not happen.', DEBUG_DEVELOPER);
  1417. }
  1418. return $result;
  1419. }
  1420. /**
  1421. * Delete all of the given keys from the cache.
  1422. *
  1423. * @param array $keys The key to delete.
  1424. * @param bool $recurse When set to true the key will also be deleted from all stacked cache loaders and their stores.
  1425. * This happens by default and ensure that all the caches are consistent. It is NOT recommended to change this.
  1426. * @return int The number of items successfully deleted.
  1427. */
  1428. public function delete_many(array $keys, $recurse = true) {
  1429. if ($this->requirelockingwrite) {
  1430. $locks = array();
  1431. foreach ($keys as $id => $key) {
  1432. if ($this->acquire_lock($key)) {
  1433. $locks[] = $key;
  1434. } else {
  1435. unset($keys[$id]);
  1436. }
  1437. }
  1438. }
  1439. $result = parent::delete_many($keys, $recurse);
  1440. if ($this->requirelockingwrite) {
  1441. foreach ($locks as $key) {
  1442. if ($this->release_lock($key)) {
  1443. debugging('Failed to release cache lock on delete_many operation... this should not happen.', DEBUG_DEVELOPER);
  1444. }
  1445. }
  1446. }
  1447. return $result;
  1448. }
  1449. }
  1450. /**
  1451. * A session cache.
  1452. *
  1453. * This class is used for session caches returned by the cache::make methods.
  1454. *
  1455. * It differs from the application loader in a couple of noteable ways:
  1456. * 1. Sessions are always expected to exist.
  1457. * Because of this we don't ever use the static acceleration array.
  1458. * 2. Session data for a loader instance (store + definition) is consolidate into a
  1459. * single array for storage within the store.
  1460. * Along with this we embed a lastaccessed time with the data. This way we can
  1461. * check sessions for a last access time.
  1462. * 3. Session stores are required to support key searching and must
  1463. * implement cache_is_searchable. This ensures stores used for the cache can be
  1464. * targetted for garbage collection of session data.
  1465. *
  1466. * This cache class should never be interacted with directly. Instead you should always use the cache::make methods.
  1467. * It is technically possible to call those methods through this class however there is no guarantee that you will get an
  1468. * instance of this class back again.
  1469. *
  1470. * @todo we should support locking in the session as well. Should be pretty simple to set up.
  1471. *
  1472. * @internal don't use me directly.
  1473. * @method cache_store|cache_is_searchable get_store() Returns the cache store which must implement both cache_is_searchable.
  1474. *
  1475. * @package core
  1476. * @category cache
  1477. * @copyright 2012 Sam Hemelryk
  1478. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  1479. */
  1480. class cache_session extends cache {
  1481. /**
  1482. * The user the session has been established for.
  1483. * @var int
  1484. */
  1485. protected static $loadeduserid = null;
  1486. /**
  1487. * The userid this cache is currently using.
  1488. * @var int
  1489. */
  1490. protected $currentuserid = null;
  1491. /**
  1492. * The session id we are currently using.
  1493. * @var array
  1494. */
  1495. protected $sessionid = null;
  1496. /**
  1497. * The session data for the above session id.
  1498. * @var array
  1499. */
  1500. protected $session = null;
  1501. /**
  1502. * Constant used to prefix keys.
  1503. */
  1504. const KEY_PREFIX = 'sess_';
  1505. /**
  1506. * This is the key used to track last access.
  1507. */
  1508. const LASTACCESS = '__lastaccess__';
  1509. /**
  1510. * Override the cache::construct method.
  1511. *
  1512. * This function gets overriden so that we can process any invalidation events if need be.
  1513. * If the definition doesn't have any invalidation events then this occurs exactly as it would for the cache class.
  1514. * Otherwise we look at the last invalidation time and then check the invalidation data for events that have occured
  1515. * between then now.
  1516. *
  1517. * You should not call this method from your code, instead you should use the cache::make methods.
  1518. *
  1519. * @param cache_definition $definition
  1520. * @param cache_store $store
  1521. * @param cache_loader|cache_data_source $loader
  1522. */
  1523. public function __construct(cache_definition $definition, cache_store $store, $loader = null) {
  1524. // First up copy the loadeduserid to the current user id.
  1525. $this->currentuserid = self::$loadeduserid;
  1526. parent::__construct($definition, $store, $loader);
  1527. // This will trigger check tracked user. If this gets removed a call to that will need to be added here in its place.
  1528. $this->set(self::LASTACCESS, cache::now());
  1529. if ($definition->has_invalidation_events()) {
  1530. $lastinvalidation = $this->get('lastsessioninvalidation');
  1531. if ($lastinvalidation === false) {
  1532. // This is a new session, there won't be anything to invalidate. Set the time of the last invalidation and
  1533. // move on.
  1534. $this->set('lastsessioninvalidation', cache::now());
  1535. return;
  1536. } else if ($lastinvalidation == cache::now()) {
  1537. // We've already invalidated during this request.
  1538. return;
  1539. }
  1540. // Get the event invalidation cache.
  1541. $cache = cache::make('core', 'eventinvalidation');
  1542. $events = $cache->get_many($definition->get_invalidation_events());
  1543. $todelete = array();
  1544. $purgeall = false;
  1545. // Iterate the returned data for the events.
  1546. foreach ($events as $event => $keys) {
  1547. if ($keys === false) {
  1548. // No data to be invalidated yet.
  1549. continue;
  1550. }
  1551. // Look at each key and check the timestamp.
  1552. foreach ($keys as $key => $timestamp) {
  1553. // If the timestamp of the event is more than or equal to the last invalidation (happened between the last
  1554. // invalidation and now)then we need to invaliate the key.
  1555. if ($timestamp >= $lastinvalidation) {
  1556. if ($key === 'purged') {
  1557. $purgeall = true;
  1558. break;
  1559. } else {
  1560. $todelete[] = $key;
  1561. }
  1562. }
  1563. }
  1564. }
  1565. if ($purgeall) {
  1566. $this->purge();
  1567. } else if (!empty($todelete)) {
  1568. $todelete = array_unique($todelete);
  1569. $this->delete_many($todelete);
  1570. }
  1571. // Set the time of the last invalidation.
  1572. $this->set('lastsessioninvalidation', cache::now());
  1573. }
  1574. }
  1575. /**
  1576. * Sets the session id for the loader.
  1577. */
  1578. protected function set_session_id() {
  1579. $this->sessionid = preg_replace('#[^a-zA-Z0-9_]#', '_', session_id());
  1580. }
  1581. /**
  1582. * Returns the prefix used for all keys.
  1583. * @return string
  1584. */
  1585. protected function get_key_prefix() {
  1586. return 'u'.$this->currentuserid.'_'.$this->sessionid;
  1587. }
  1588. /**
  1589. * Parses the key turning it into a string (or array is required) suitable to be passed to the cache store.
  1590. *
  1591. * This function is called for every operation that uses keys. For this reason we use this function to also check
  1592. * that the current user is the same as the user who last used this cache.
  1593. *
  1594. * On top of that if prepends the string 'sess_' to the start of all keys. The _ ensures things are easily identifiable.
  1595. *
  1596. * @param string|int $key As passed to get|set|delete etc.
  1597. * @return string|array String unless the store supports multi-identifiers in which case an array if returned.
  1598. */
  1599. protected function parse_key($key) {
  1600. $prefix = $this->get_key_prefix();
  1601. if ($key === self::LASTACCESS) {
  1602. return $key.$prefix;
  1603. }
  1604. return $prefix.'_'.parent::parse_key($key);
  1605. }
  1606. /**
  1607. * Check that this cache instance is tracking the current user.
  1608. */
  1609. protected function check_tracked_user() {
  1610. if (isset($_SESSION['USER']->id) && $_SESSION['USER']->id !== null) {
  1611. // Get the id of the current user.
  1612. $new = $_SESSION['USER']->id;
  1613. } else {
  1614. // No user set up yet.
  1615. $new = 0;
  1616. }
  1617. if ($new !== self::$loadeduserid) {
  1618. // The current user doesn't match the tracked userid for this request.
  1619. if (!is_null(self::$loadeduserid)) {
  1620. // Purge the data we have for the old user.
  1621. // This way we don't bloat the session.
  1622. $this->purge();
  1623. // Update the session id just in case!
  1624. $this->set_session_id();
  1625. }
  1626. self::$loadeduserid = $new;
  1627. $this->currentuserid = $new;
  1628. } else if ($new !== $this->currentuserid) {
  1629. // The current user matches the loaded user but not the user last used by this cache.
  1630. $this->purge_current_user();
  1631. $this->currentuserid = $new;
  1632. // Update the session id just in case!
  1633. $this->set_session_id();
  1634. }
  1635. }
  1636. /**
  1637. * Purges the session cache of all data belonging to the current user.
  1638. */
  1639. public function purge_current_user() {
  1640. $keys = $this->get_store()->find_by_prefix($this->get_key_prefix());
  1641. $this->get_store()->delete_many($keys);
  1642. }
  1643. /**
  1644. * Retrieves the value for the given key from the cache.
  1645. *
  1646. * @param string|int $key The key for the data being requested.
  1647. * It can be any structure although using a scalar string or int is recommended in the interests of performance.
  1648. * In advanced cases an array may be useful such as in situations requiring the multi-key functionality.
  1649. * @param int $strictness One of IGNORE_MISSING | MUST_EXIST
  1650. * @return mixed|false The data from the cache or false if the key did not exist within the cache.
  1651. * @throws coding_exception
  1652. */
  1653. public function get($key, $strictness = IGNORE_MISSING) {
  1654. // Check the tracked user.
  1655. $this->check_tracked_user();
  1656. // 2. Parse the key.
  1657. $parsedkey = $this->parse_key($key);
  1658. // 3. Get it from the store.
  1659. $result = $this->get_store()->get($parsedkey);
  1660. if ($result !== false) {
  1661. if ($result instanceof cache_ttl_wrapper) {
  1662. if ($result->has_expired()) {
  1663. $this->get_store()->delete($parsedkey);
  1664. $result = false;
  1665. } else {
  1666. $result = $result->data;
  1667. }
  1668. }
  1669. if ($result instanceof cache_cached_object) {
  1670. $result = $result->restore_object();
  1671. }
  1672. }
  1673. // 4. Load if from the loader/datasource if we don't already have it.
  1674. if ($result === false) {
  1675. if ($this->perfdebug) {
  1676. cache_helper::record_cache_miss($this->storetype, $this->get_definition()->get_id());
  1677. }
  1678. if ($this->get_loader() !== false) {
  1679. // We must pass the original (unparsed) key to the next loader in the chain.
  1680. // The next loader will parse the key as it sees fit. It may be parsed differently
  1681. // depending upon the capabilities of the store associated with the loader.
  1682. $result = $this->get_loader()->get($key);
  1683. } else if ($this->get_datasource() !== false) {
  1684. $result = $this->get_datasource()->load_for_cache($key);
  1685. }
  1686. // 5. Set it to the store if we got it from the loader/datasource.
  1687. if ($result !== false) {
  1688. $this->set($key, $result);
  1689. }
  1690. } else if ($this->perfdebug) {
  1691. cache_helper::record_cache_hit($this->storetype, $this->get_definition()->get_id());
  1692. }
  1693. // 5. Validate strictness.
  1694. if ($strictness === MUST_EXIST && $result === false) {
  1695. throw new coding_exception('Requested key did not exist in any cache stores and could not be loaded.');
  1696. }
  1697. // 6. Make sure we don't pass back anything that could be a reference.
  1698. // We don't want people modifying the data in the cache.
  1699. if (!is_scalar($result)) {
  1700. // If data is an object it will be a reference.
  1701. // If data is an array if may contain references.
  1702. // We want to break references so that the cache cannot be modified outside of itself.
  1703. // Call the function to unreference it (in the best way possible).
  1704. $result = $this->unref($result);
  1705. }
  1706. return $result;
  1707. }
  1708. /**
  1709. * Sends a key => value pair to the cache.
  1710. *
  1711. * <code>
  1712. * // This code will add four entries to the cache, one for each url.
  1713. * $cache->set('main', 'http://moodle.org');
  1714. * $cache->set('docs', 'http://docs.moodle.org');
  1715. * $cache->set('tracker', 'http://tracker.moodle.org');
  1716. * $cache->set('qa', 'http://qa.moodle.net');
  1717. * </code>
  1718. *
  1719. * @param string|int $key The key for the data being requested.
  1720. * It can be any structure although using a scalar string or int is recommended in the interests of performance.
  1721. * In advanced cases an array may be useful such as in situations requiring the multi-key functionality.
  1722. * @param mixed $data The data to set against the key.
  1723. * @return bool True on success, false otherwise.
  1724. */
  1725. public function set($key, $data) {
  1726. $this->check_tracked_user();
  1727. $loader = $this->get_loader();
  1728. if ($loader !== false) {
  1729. // We have a loader available set it there as well.
  1730. // We have to let the loader do its own parsing of data as it may be unique.
  1731. $loader->set($key, $data);
  1732. }
  1733. if ($this->perfdebug) {
  1734. cache_helper::record_cache_set($this->storetype, $this->get_definition()->get_id());
  1735. }
  1736. if (is_object($data) && $data instanceof cacheable_object) {
  1737. $data = new cache_cached_object($data);
  1738. } else if (!is_scalar($data)) {
  1739. // If data is an object it will be a reference.
  1740. // If data is an array if may contain references.
  1741. // We want to break references so that the cache cannot be modified outside of itself.
  1742. // Call the function to unreference it (in the best way possible).
  1743. $data = $this->unref($data);
  1744. }
  1745. // We dont' support native TTL here as we consolidate data for sessions.
  1746. if ($this->has_a_ttl() && !$this->store_supports_native_ttl()) {
  1747. $data = new cache_ttl_wrapper($data, $this->get_definition()->get_ttl());
  1748. }
  1749. return $this->get_store()->set($this->parse_key($key), $data);
  1750. }
  1751. /**
  1752. * Delete the given key from the cache.
  1753. *
  1754. * @param string|int $key The key to delete.
  1755. * @param bool $recurse When set to true the key will also be deleted from all stacked cache loaders and their stores.
  1756. * This happens by default and ensure that all the caches are consistent. It is NOT recommended to change this.
  1757. * @return bool True of success, false otherwise.
  1758. */
  1759. public function delete($key, $recurse = true) {
  1760. $parsedkey = $this->parse_key($key);
  1761. if ($recurse && $this->get_loader() !== false) {
  1762. // Delete from the bottom of the stack first.
  1763. $this->get_loader()->delete($key, $recurse);
  1764. }
  1765. return $this->get_store()->delete($parsedkey);
  1766. }
  1767. /**
  1768. * Retrieves an array of values for an array of keys.
  1769. *
  1770. * Using this function comes with potential performance implications.
  1771. * Not all cache stores will support get_many/set_many operations and in order to replicate this functionality will call
  1772. * the equivalent singular method for each item provided.
  1773. * This should not deter you from using this function as there is a performance benefit in situations where the cache store
  1774. * does support it, but you should be aware of this fact.
  1775. *
  1776. * @param array $keys The keys of the data being requested.
  1777. * Each key can be any structure although using a scalar string or int is recommended in the interests of performance.
  1778. * In advanced cases an array may be useful such as in situations requiring the multi-key functionality.
  1779. * @param int $strictness One of IGNORE_MISSING or MUST_EXIST.
  1780. * @return array An array of key value pairs for the items that could be retrieved from the cache.
  1781. * If MUST_EXIST was used and not all keys existed within the cache then an exception will be thrown.
  1782. * Otherwise any key that did not exist will have a data value of false within the results.
  1783. * @throws coding_exception
  1784. */
  1785. public function get_many(array $keys, $strictness = IGNORE_MISSING) {
  1786. $this->check_tracked_user();
  1787. $parsedkeys = array();
  1788. $keymap = array();
  1789. foreach ($keys as $key) {
  1790. $parsedkey = $this->parse_key($key);
  1791. $parsedkeys[$key] = $parsedkey;
  1792. $keymap[$parsedkey] = $key;
  1793. }
  1794. $result = $this->get_store()->get_many($parsedkeys);
  1795. $return = array();
  1796. $missingkeys = array();
  1797. $hasmissingkeys = false;
  1798. foreach ($result as $parsedkey => $value) {
  1799. $key = $keymap[$parsedkey];
  1800. if ($value instanceof cache_ttl_wrapper) {
  1801. /* @var cache_ttl_wrapper $value */
  1802. if ($value->has_expired()) {
  1803. $this->delete($keymap[$parsedkey]);
  1804. $value = false;
  1805. } else {
  1806. $value = $value->data;
  1807. }
  1808. }
  1809. if ($value instanceof cache_cached_object) {
  1810. /* @var cache_cached_object $value */
  1811. $value = $value->restore_object();
  1812. }
  1813. $return[$key] = $value;
  1814. if ($value === false) {
  1815. $hasmissingkeys = true;
  1816. $missingkeys[$parsedkey] = $key;
  1817. }
  1818. }
  1819. if ($hasmissingkeys) {
  1820. // We've got missing keys - we've got to check any loaders or data sources.
  1821. $loader = $this->get_loader();
  1822. $datasource = $this->get_datasource();
  1823. if ($loader !== false) {
  1824. foreach ($loader->get_many($missingkeys) as $key => $value) {
  1825. if ($value !== false) {
  1826. $return[$key] = $value;
  1827. unset($missingkeys[$parsedkeys[$key]]);
  1828. }
  1829. }
  1830. }
  1831. $hasmissingkeys = count($missingkeys) > 0;
  1832. if ($datasource !== false && $hasmissingkeys) {
  1833. // We're still missing keys but we've got a datasource.
  1834. foreach ($datasource->load_many_for_cache($missingkeys) as $key => $value) {
  1835. if ($value !== false) {
  1836. $return[$key] = $value;
  1837. unset($missingkeys[$parsedkeys[$key]]);
  1838. }
  1839. }
  1840. $hasmissingkeys = count($missingkeys) > 0;
  1841. }
  1842. }
  1843. if ($hasmissingkeys && $strictness === MUST_EXIST) {
  1844. throw new coding_exception('Requested key did not exist in any cache stores and could not be loaded.');
  1845. }
  1846. if ($this->perfdebug) {
  1847. $hits = 0;
  1848. $misses = 0;
  1849. foreach ($return as $value) {
  1850. if ($value === false) {
  1851. $misses++;
  1852. } else {
  1853. $hits++;
  1854. }
  1855. }
  1856. cache_helper::record_cache_hit($this->storetype, $this->get_definition()->get_id(), $hits);
  1857. cache_helper::record_cache_miss($this->storetype, $this->get_definition()->get_id(), $misses);
  1858. }
  1859. return $return;
  1860. }
  1861. /**
  1862. * Delete all of the given keys from the cache.
  1863. *
  1864. * @param array $keys The key to delete.
  1865. * @param bool $recurse When set to true the key will also be deleted from all stacked cache loaders and their stores.
  1866. * This happens by default and ensure that all the caches are consistent. It is NOT recommended to change this.
  1867. * @return int The number of items successfully deleted.
  1868. */
  1869. public function delete_many(array $keys, $recurse = true) {
  1870. $parsedkeys = array_map(array($this, 'parse_key'), $keys);
  1871. if ($recurse && $this->get_loader() !== false) {
  1872. // Delete from the bottom of the stack first.
  1873. $this->get_loader()->delete_many($keys, $recurse);
  1874. }
  1875. return $this->get_store()->delete_many($parsedkeys);
  1876. }
  1877. /**
  1878. * Sends several key => value pairs to the cache.
  1879. *
  1880. * Using this function comes with potential performance implications.
  1881. * Not all cache stores will support get_many/set_many operations and in order to replicate this functionality will call
  1882. * the equivalent singular method for each item provided.
  1883. * This should not deter you from using this function as there is a performance benefit in situations where the cache store
  1884. * does support it, but you should be aware of this fact.
  1885. *
  1886. * <code>
  1887. * // This code will add four entries to the cache, one for each url.
  1888. * $cache->set_many(array(
  1889. * 'main' => 'http://moodle.org',
  1890. * 'docs' => 'http://docs.moodle.org',
  1891. * 'tracker' => 'http://tracker.moodle.org',
  1892. * 'qa' => ''http://qa.moodle.net'
  1893. * ));
  1894. * </code>
  1895. *
  1896. * @param array $keyvaluearray An array of key => value pairs to send to the cache.
  1897. * @return int The number of items successfully set. It is up to the developer to check this matches the number of items.
  1898. * ... if they care that is.
  1899. */
  1900. public function set_many(array $keyvaluearray) {
  1901. $this->check_tracked_user();
  1902. $loader = $this->get_loader();
  1903. if ($loader !== false) {
  1904. // We have a loader available set it there as well.
  1905. // We have to let the loader do its own parsing of data as it may be unique.
  1906. $loader->set_many($keyvaluearray);
  1907. }
  1908. $data = array();
  1909. $definitionid = $this->get_definition()->get_ttl();
  1910. $simulatettl = $this->has_a_ttl() && !$this->store_supports_native_ttl();
  1911. foreach ($keyvaluearray as $key => $value) {
  1912. if (is_object($value) && $value instanceof cacheable_object) {
  1913. $value = new cache_cached_object($value);
  1914. } else if (!is_scalar($value)) {
  1915. // If data is an object it will be a reference.
  1916. // If data is an array if may contain references.
  1917. // We want to break references so that the cache cannot be modified outside of itself.
  1918. // Call the function to unreference it (in the best way possible).
  1919. $value = $this->unref($value);
  1920. }
  1921. if ($simulatettl) {
  1922. $value = new cache_ttl_wrapper($value, $definitionid);
  1923. }
  1924. $data[$key] = array(
  1925. 'key' => $this->parse_key($key),
  1926. 'value' => $value
  1927. );
  1928. }
  1929. $successfullyset = $this->get_store()->set_many($data);
  1930. if ($this->perfdebug && $successfullyset) {
  1931. cache_helper::record_cache_set($this->storetype, $definitionid, $successfullyset);
  1932. }
  1933. return $successfullyset;
  1934. }
  1935. /**
  1936. * Purges the cache store, and loader if there is one.
  1937. *
  1938. * @return bool True on success, false otherwise
  1939. */
  1940. public function purge() {
  1941. $this->get_store()->purge();
  1942. if ($this->get_loader()) {
  1943. $this->get_loader()->purge();
  1944. }
  1945. return true;
  1946. }
  1947. /**
  1948. * Test is a cache has a key.
  1949. *
  1950. * The use of the has methods is strongly discouraged. In a high load environment the cache may well change between the
  1951. * test and any subsequent action (get, set, delete etc).
  1952. * Instead it is recommended to write your code in such a way they it performs the following steps:
  1953. * <ol>
  1954. * <li>Attempt to retrieve the information.</li>
  1955. * <li>Generate the information.</li>
  1956. * <li>Attempt to set the information</li>
  1957. * </ol>
  1958. *
  1959. * Its also worth mentioning that not all stores support key tests.
  1960. * For stores that don't support key tests this functionality is mimicked by using the equivalent get method.
  1961. * Just one more reason you should not use these methods unless you have a very good reason to do so.
  1962. *
  1963. * @param string|int $key
  1964. * @param bool $tryloadifpossible If set to true, the cache doesn't contain the key, and there is another cache loader or
  1965. * data source then the code will try load the key value from the next item in the chain.
  1966. * @return bool True if the cache has the requested key, false otherwise.
  1967. */
  1968. public function has($key, $tryloadifpossible = false) {
  1969. $this->check_tracked_user();
  1970. $parsedkey = $this->parse_key($key);
  1971. $store = $this->get_store();
  1972. if ($this->has_a_ttl() && !$this->store_supports_native_ttl()) {
  1973. // The data has a TTL and the store doesn't support it natively.
  1974. // We must fetch the data and expect a ttl wrapper.
  1975. $data = $store->get($parsedkey);
  1976. $has = ($data instanceof cache_ttl_wrapper && !$data->has_expired());
  1977. } else if (!$this->store_supports_key_awareness()) {
  1978. // The store doesn't support key awareness, get the data and check it manually... puke.
  1979. // Either no TTL is set of the store supports its handling natively.
  1980. $data = $store->get($parsedkey);
  1981. $has = ($data !== false);
  1982. } else {
  1983. // The store supports key awareness, this is easy!
  1984. // Either no TTL is set of the store supports its handling natively.
  1985. /* @var cache_store|cache_is_key_aware $store */
  1986. $has = $store->has($parsedkey);
  1987. }
  1988. if (!$has && $tryloadifpossible) {
  1989. $result = null;
  1990. if ($this->get_loader() !== false) {
  1991. $result = $this->get_loader()->get($parsedkey);
  1992. } else if ($this->get_datasource() !== null) {
  1993. $result = $this->get_datasource()->load_for_cache($key);
  1994. }
  1995. $has = ($result !== null);
  1996. if ($has) {
  1997. $this->set($key, $result);
  1998. }
  1999. }
  2000. return $has;
  2001. }
  2002. /**
  2003. * Test is a cache has all of the given keys.
  2004. *
  2005. * It is strongly recommended to avoid the use of this function if not absolutely required.
  2006. * In a high load environment the cache may well change between the test and any subsequent action (get, set, delete etc).
  2007. *
  2008. * Its also worth mentioning that not all stores support key tests.
  2009. * For stores that don't support key tests this functionality is mimicked by using the equivalent get method.
  2010. * Just one more reason you should not use these methods unless you have a very good reason to do so.
  2011. *
  2012. * @param array $keys
  2013. * @return bool True if the cache has all of the given keys, false otherwise.
  2014. */
  2015. public function has_all(array $keys) {
  2016. $this->check_tracked_user();
  2017. if (($this->has_a_ttl() && !$this->store_supports_native_ttl()) || !$this->store_supports_key_awareness()) {
  2018. foreach ($keys as $key) {
  2019. if (!$this->has($key)) {
  2020. return false;
  2021. }
  2022. }
  2023. return true;
  2024. }
  2025. // The cache must be key aware and if support native ttl if it a ttl is set.
  2026. /* @var cache_store|cache_is_key_aware $store */
  2027. $store = $this->get_store();
  2028. return $store->has_all(array_map(array($this, 'parse_key'), $keys));
  2029. }
  2030. /**
  2031. * Test if a cache has at least one of the given keys.
  2032. *
  2033. * It is strongly recommended to avoid the use of this function if not absolutely required.
  2034. * In a high load environment the cache may well change between the test and any subsequent action (get, set, delete etc).
  2035. *
  2036. * Its also worth mentioning that not all stores support key tests.
  2037. * For stores that don't support key tests this functionality is mimicked by using the equivalent get method.
  2038. * Just one more reason you should not use these methods unless you have a very good reason to do so.
  2039. *
  2040. * @param array $keys
  2041. * @return bool True if the cache has at least one of the given keys
  2042. */
  2043. public function has_any(array $keys) {
  2044. if (($this->has_a_ttl() && !$this->store_supports_native_ttl()) || !$this->store_supports_key_awareness()) {
  2045. foreach ($keys as $key) {
  2046. if ($this->has($key)) {
  2047. return true;
  2048. }
  2049. }
  2050. return false;
  2051. }
  2052. /* @var cache_store|cache_is_key_aware $store */
  2053. $store = $this->get_store();
  2054. return $store->has_any(array_map(array($this, 'parse_key'), $keys));
  2055. }
  2056. /**
  2057. * The session loader never uses static acceleration.
  2058. * Instead it stores things in the static $session variable. Shared between all session loaders.
  2059. *
  2060. * @return bool
  2061. */
  2062. protected function use_static_acceleration() {
  2063. return false;
  2064. }
  2065. }
  2066. /**
  2067. * An request cache.
  2068. *
  2069. * This class is used for request caches returned by the cache::make methods.
  2070. *
  2071. * This cache class should never be interacted with directly. Instead you should always use the cache::make methods.
  2072. * It is technically possible to call those methods through this class however there is no guarantee that you will get an
  2073. * instance of this class back again.
  2074. *
  2075. * @internal don't use me directly.
  2076. *
  2077. * @package core
  2078. * @category cache
  2079. * @copyright 2012 Sam Hemelryk
  2080. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  2081. */
  2082. class cache_request extends cache {
  2083. // This comment appeases code pre-checker ;) !
  2084. }