[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 /** 18 * Cache definition class 19 * 20 * This file is part of Moodle's cache API, affectionately called MUC. 21 * It contains the components that are requried in order to use caching. 22 * 23 * @package core 24 * @category cache 25 * @copyright 2012 Sam Hemelryk 26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 27 */ 28 29 defined('MOODLE_INTERNAL') || die(); 30 31 /** 32 * The cache definition class. 33 * 34 * Cache definitions need to be defined in db/caches.php files. 35 * They can be constructed with the following options. 36 * 37 * Required settings: 38 * + mode 39 * [int] Sets the mode for the definition. Must be one of cache_store::MODE_* 40 * 41 * Optional settings: 42 * + simplekeys 43 * [bool] Set to true if your cache will only use simple keys for its items. 44 * Simple keys consist of digits, underscores and the 26 chars of the english language. a-zA-Z0-9_ 45 * If true the keys won't be hashed before being passed to the cache store for gets/sets/deletes. It will be 46 * better for performance and possible only becase we know the keys are safe. 47 * + simpledata 48 * [bool] If set to true we know that the data is scalar or array of scalar. 49 * + requireidentifiers 50 * [array] An array of identifiers that must be provided to the cache when it is created. 51 * + requiredataguarantee 52 * [bool] If set to true then only stores that can guarantee data will remain available once set will be used. 53 * + requiremultipleidentifiers 54 * [bool] If set to true then only stores that support multiple identifiers will be used. 55 * + requirelockingread 56 * [bool] If set to true then a lock will be gained before reading from the cache store. It is recommended not to use 57 * this setting unless 100% absolutely positively required. Remember 99.9% of caches will NOT need this setting. 58 * This setting will only be used for application caches presently. 59 * + requirelockingwrite 60 * [bool] If set to true then a lock will be gained before writing to the cache store. As above this is not recommended 61 * unless truly needed. Please think about the order of your code and deal with race conditions there first. 62 * This setting will only be used for application caches presently. 63 * + maxsize 64 * [int] If set this will be used as the maximum number of entries within the cache store for this definition. 65 * Its important to note that cache stores don't actually have to acknowledge this setting or maintain it as a hard limit. 66 * + overrideclass 67 * [string] A class to use as the loader for this cache. This is an advanced setting and will allow the developer of the 68 * definition to take 100% control of the caching solution. 69 * Any class used here must inherit the cache_loader interface and must extend default cache loader for the mode they are 70 * using. 71 * + overrideclassfile 72 * [string] Suplements the above setting indicated the file containing the class to be used. This file is included when 73 * required. 74 * + datasource 75 * [string] A class to use as the data loader for this definition. 76 * Any class used here must inherit the cache_data_loader interface. 77 * + datasourcefile 78 * [string] Supplements the above setting indicating the file containing the class to be used. This file is included when 79 * required. 80 * + staticacceleration 81 * The cache loader will keep an array of the items set and retrieved to the cache during the request. 82 * Consider using this setting when you know that there are going to be many calls to the cache for the same information. 83 * Requests for data in this array will be ultra fast, but it will cost memory. 84 * + staticaccelerationsize 85 * [int] This supplements the above setting by limiting the number of items in the static acceleration array. 86 * Tweaking this setting lower will allow you to minimise the memory implications above while hopefully still managing to 87 * offset calls to the cache store. 88 * + ttl 89 * [int] A time to live for the data (in seconds). It is strongly recommended that you don't make use of this and 90 * instead try to create an event driven invalidation system. 91 * Not all cache stores will support this natively and there are undesired performance impacts if the cache store does not. 92 * + mappingsonly 93 * [bool] If set to true only the mapped cache store(s) will be used and the default mode store will not. This is a super 94 * advanced setting and should not be used unless absolutely required. It allows you to avoid the default stores for one 95 * reason or another. 96 * + invalidationevents 97 * [array] An array of events that should cause this cache to invalidate some or all of the items within it. 98 * + sharingoptions 99 * [int] The sharing options that are appropriate for this definition. Should be the sum of the possible options. 100 * + defaultsharing 101 * [int] The default sharing option to use. It's highly recommended that you don't set this unless there is a very 102 * specific reason not to use the system default. 103 * + canuselocalstore 104 * [bool] The cache is able to safely run with multiple copies on different webservers without any need for administrator 105 * intervention to ensure that data stays in sync across nodes. This is usually managed by a revision 106 * system as seen in modinfo cache or language cache. Requiring purge on upgrade is not sufficient as 107 * it requires administrator intervention on each node to make it work. 108 * 109 * For examples take a look at lib/db/caches.php 110 * 111 * @package core 112 * @category cache 113 * @copyright 2012 Sam Hemelryk 114 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 115 */ 116 class cache_definition { 117 118 /** The cache can be shared with everyone */ 119 const SHARING_ALL = 1; 120 /** The cache can be shared with other sites using the same siteid. */ 121 const SHARING_SITEID = 2; 122 /** The cache can be shared with other sites of the same version. */ 123 const SHARING_VERSION = 4; 124 /** The cache can be shared with other sites using the same key */ 125 const SHARING_INPUT = 8; 126 127 /** 128 * The default sharing options available. 129 * All + SiteID + Version + Input. 130 */ 131 const SHARING_DEFAULTOPTIONS = 15; 132 /** 133 * The default sharing option that gets used if none have been selected. 134 * SiteID. It is the most restrictive. 135 */ 136 const SHARING_DEFAULT = 2; 137 138 /** 139 * The identifier for the definition 140 * @var string 141 */ 142 protected $id; 143 144 /** 145 * The mode for the defintion. One of cache_store::MODE_* 146 * @var int 147 */ 148 protected $mode; 149 150 /** 151 * The component this definition is associated with. 152 * @var string 153 */ 154 protected $component; 155 156 /** 157 * The area this definition is associated with. 158 * @var string 159 */ 160 protected $area; 161 162 /** 163 * If set to true we know the keys are simple. a-zA-Z0-9_ 164 * @var bool 165 */ 166 protected $simplekeys = false; 167 168 /** 169 * Set to true if we know the data is scalar or array of scalar. 170 * @var bool 171 */ 172 protected $simpledata = false; 173 174 /** 175 * An array of identifiers that must be provided when the definition is used to create a cache. 176 * @var array 177 */ 178 protected $requireidentifiers = array(); 179 180 /** 181 * If set to true then only stores that guarantee data may be used with this definition. 182 * @var bool 183 */ 184 protected $requiredataguarantee = false; 185 186 /** 187 * If set to true then only stores that support multple identifiers may be used with this definition. 188 * @var bool 189 */ 190 protected $requiremultipleidentifiers = false; 191 192 /** 193 * If set to true then we know that this definition requires the locking functionality. 194 * This gets set during construction based upon the settings requirelockingread and requirelockingwrite. 195 * @var bool 196 */ 197 protected $requirelocking = false; 198 199 /** 200 * Set to true if this definition requires read locking. 201 * @var bool 202 */ 203 protected $requirelockingread = false; 204 205 /** 206 * Gets set to true if this definition requires write locking. 207 * @var bool 208 */ 209 protected $requirelockingwrite = false; 210 211 /** 212 * Gets set to true if this definition requires searchable stores. 213 * @since Moodle 2.4.4 214 * @var bool 215 */ 216 protected $requiresearchable = false; 217 218 /** 219 * Sets the maximum number of items that can exist in the cache. 220 * Please note this isn't a hard limit, and doesn't need to be enforced by the caches. They can choose to do so optionally. 221 * @var int 222 */ 223 protected $maxsize = null; 224 225 /** 226 * The class to use as the cache loader for this definition. 227 * @var string 228 */ 229 protected $overrideclass = null; 230 231 /** 232 * The file in which the override class exists. This will be included if required. 233 * @var string Absolute path 234 */ 235 protected $overrideclassfile = null; 236 237 /** 238 * The data source class to use with this definition. 239 * @var string 240 */ 241 protected $datasource = null; 242 243 /** 244 * The file in which the data source class exists. This will be included if required. 245 * @var string 246 */ 247 protected $datasourcefile = null; 248 249 /** 250 * Deprecated - this is completely unused. 251 * @deprecated since 2.9 252 * @todo MDL-55267 This will be deleted in Moodle 3.3. 253 * @var string 254 */ 255 protected $datasourceaggregate = null; 256 257 /** 258 * Set to true if the cache should hold onto items passing through it to speed up subsequent requests. 259 * @var bool 260 */ 261 protected $staticacceleration = false; 262 263 /** 264 * The maximum number of items that static acceleration cache should hold onto. 265 * @var int 266 */ 267 protected $staticaccelerationsize = false; 268 269 /** 270 * The TTL for data in this cache. Please don't use this, instead use event driven invalidation. 271 * @var int 272 */ 273 protected $ttl = 0; 274 275 /** 276 * Set to true if this cache should only use mapped cache stores and not the default mode cache store. 277 * @var bool 278 */ 279 protected $mappingsonly = false; 280 281 /** 282 * An array of events that should cause this cache to invalidate. 283 * @var array 284 */ 285 protected $invalidationevents = array(); 286 287 /** 288 * An array of identifiers provided to this cache when it was initialised. 289 * @var array 290 */ 291 protected $identifiers = null; 292 293 /** 294 * Key prefix for use with single key cache stores 295 * @var string 296 */ 297 protected $keyprefixsingle = null; 298 299 /** 300 * Key prefix to use with cache stores that support multi keys. 301 * @var array 302 */ 303 protected $keyprefixmulti = null; 304 305 /** 306 * A hash identifier of this definition. 307 * @var string 308 */ 309 protected $definitionhash = null; 310 311 /** 312 * The selected sharing mode for this definition. 313 * @var int 314 */ 315 protected $sharingoptions; 316 317 /** 318 * Whether this cache supports local storages. 319 * @var bool 320 */ 321 protected $canuselocalstore = false; 322 323 /** 324 * The selected sharing option. 325 * @var int One of self::SHARING_* 326 */ 327 protected $selectedsharingoption = self::SHARING_DEFAULT; 328 329 /** 330 * The user input key to use if the SHARING_INPUT option has been selected. 331 * @var string Must be ALPHANUMEXT 332 */ 333 protected $userinputsharingkey = ''; 334 335 /** 336 * Creates a cache definition given a definition from the cache configuration or from a caches.php file. 337 * 338 * @param string $id 339 * @param array $definition 340 * @param string $unused Used to be datasourceaggregate but that was removed and this is now unused. 341 * @return cache_definition 342 * @throws coding_exception 343 */ 344 public static function load($id, array $definition, $unused = null) { 345 global $CFG; 346 347 if (!array_key_exists('mode', $definition)) { 348 throw new coding_exception('You must provide a mode when creating a cache definition'); 349 } 350 if (!array_key_exists('component', $definition)) { 351 throw new coding_exception('You must provide a component when creating a cache definition'); 352 } 353 if (!array_key_exists('area', $definition)) { 354 throw new coding_exception('You must provide an area when creating a cache definition'); 355 } 356 $mode = (int)$definition['mode']; 357 $component = (string)$definition['component']; 358 $area = (string)$definition['area']; 359 360 // Set the defaults. 361 $simplekeys = false; 362 $simpledata = false; 363 $requireidentifiers = array(); 364 $requiredataguarantee = false; 365 $requiremultipleidentifiers = false; 366 $requirelockingread = false; 367 $requirelockingwrite = false; 368 $requiresearchable = ($mode === cache_store::MODE_SESSION) ? true : false; 369 $maxsize = null; 370 $overrideclass = null; 371 $overrideclassfile = null; 372 $datasource = null; 373 $datasourcefile = null; 374 $staticacceleration = false; 375 $staticaccelerationsize = false; 376 $ttl = 0; 377 $mappingsonly = false; 378 $invalidationevents = array(); 379 $sharingoptions = self::SHARING_DEFAULT; 380 $selectedsharingoption = self::SHARING_DEFAULT; 381 $userinputsharingkey = ''; 382 $canuselocalstore = false; 383 384 if (array_key_exists('simplekeys', $definition)) { 385 $simplekeys = (bool)$definition['simplekeys']; 386 } 387 if (array_key_exists('simpledata', $definition)) { 388 $simpledata = (bool)$definition['simpledata']; 389 } 390 if (array_key_exists('requireidentifiers', $definition)) { 391 $requireidentifiers = (array)$definition['requireidentifiers']; 392 } 393 if (array_key_exists('requiredataguarantee', $definition)) { 394 $requiredataguarantee = (bool)$definition['requiredataguarantee']; 395 } 396 if (array_key_exists('requiremultipleidentifiers', $definition)) { 397 $requiremultipleidentifiers = (bool)$definition['requiremultipleidentifiers']; 398 } 399 400 if (array_key_exists('requirelockingread', $definition)) { 401 $requirelockingread = (bool)$definition['requirelockingread']; 402 } 403 if (array_key_exists('requirelockingwrite', $definition)) { 404 $requirelockingwrite = (bool)$definition['requirelockingwrite']; 405 } 406 $requirelocking = $requirelockingwrite || $requirelockingread; 407 408 if (array_key_exists('requiresearchable', $definition)) { 409 $requiresearchable = (bool)$definition['requiresearchable']; 410 } 411 412 if (array_key_exists('maxsize', $definition)) { 413 $maxsize = (int)$definition['maxsize']; 414 } 415 416 if (array_key_exists('overrideclass', $definition)) { 417 $overrideclass = $definition['overrideclass']; 418 } 419 if (array_key_exists('overrideclassfile', $definition)) { 420 $overrideclassfile = $definition['overrideclassfile']; 421 } 422 423 if (array_key_exists('datasource', $definition)) { 424 $datasource = $definition['datasource']; 425 } 426 if (array_key_exists('datasourcefile', $definition)) { 427 $datasourcefile = $definition['datasourcefile']; 428 } 429 430 if (array_key_exists('persistent', $definition)) { 431 // Ahhh this is the legacy persistent option. 432 $staticacceleration = (bool)$definition['persistent']; 433 } 434 if (array_key_exists('staticacceleration', $definition)) { 435 $staticacceleration = (bool)$definition['staticacceleration']; 436 } 437 if (array_key_exists('persistentmaxsize', $definition)) { 438 // Ahhh this is the legacy persistentmaxsize option. 439 $staticaccelerationsize = (int)$definition['persistentmaxsize']; 440 } 441 if (array_key_exists('staticaccelerationsize', $definition)) { 442 $staticaccelerationsize = (int)$definition['staticaccelerationsize']; 443 } 444 if (array_key_exists('ttl', $definition)) { 445 $ttl = (int)$definition['ttl']; 446 } 447 if (array_key_exists('mappingsonly', $definition)) { 448 $mappingsonly = (bool)$definition['mappingsonly']; 449 } 450 if (array_key_exists('invalidationevents', $definition)) { 451 $invalidationevents = (array)$definition['invalidationevents']; 452 } 453 if (array_key_exists('sharingoptions', $definition)) { 454 $sharingoptions = (int)$definition['sharingoptions']; 455 } 456 if (array_key_exists('selectedsharingoption', $definition)) { 457 $selectedsharingoption = (int)$definition['selectedsharingoption']; 458 } else if (array_key_exists('defaultsharing', $definition)) { 459 $selectedsharingoption = (int)$definition['defaultsharing']; 460 } else if ($sharingoptions ^ $selectedsharingoption) { 461 if ($sharingoptions & self::SHARING_SITEID) { 462 $selectedsharingoption = self::SHARING_SITEID; 463 } else if ($sharingoptions & self::SHARING_VERSION) { 464 $selectedsharingoption = self::SHARING_VERSION; 465 } else { 466 $selectedsharingoption = self::SHARING_ALL; 467 } 468 } 469 if (array_key_exists('canuselocalstore', $definition)) { 470 $canuselocalstore = (bool)$definition['canuselocalstore']; 471 } 472 473 if (array_key_exists('userinputsharingkey', $definition) && !empty($definition['userinputsharingkey'])) { 474 $userinputsharingkey = (string)$definition['userinputsharingkey']; 475 } 476 477 if (!is_null($overrideclass)) { 478 if (!is_null($overrideclassfile)) { 479 if (strpos($overrideclassfile, $CFG->dirroot) !== 0) { 480 $overrideclassfile = $CFG->dirroot.'/'.$overrideclassfile; 481 } 482 if (strpos($overrideclassfile, '../') !== false) { 483 throw new coding_exception('No path craziness allowed within override class file path.'); 484 } 485 if (!file_exists($overrideclassfile)) { 486 throw new coding_exception('The override class file does not exist.'); 487 } 488 require_once($overrideclassfile); 489 } 490 if (!class_exists($overrideclass)) { 491 throw new coding_exception('The override class does not exist.'); 492 } 493 494 // Make sure that the provided class extends the default class for the mode. 495 if (get_parent_class($overrideclass) !== cache_helper::get_class_for_mode($mode)) { 496 throw new coding_exception('The override class does not immediately extend the relevant cache class.'); 497 } 498 } 499 500 if (!is_null($datasource)) { 501 if (!is_null($datasourcefile)) { 502 if (strpos($datasourcefile, $CFG->dirroot) !== 0) { 503 $datasourcefile = $CFG->dirroot.'/'.$datasourcefile; 504 } 505 if (strpos($datasourcefile, '../') !== false) { 506 throw new coding_exception('No path craziness allowed within data source file path.'); 507 } 508 if (!file_exists($datasourcefile)) { 509 throw new coding_exception('The data source class file does not exist.'); 510 } 511 require_once($datasourcefile); 512 } 513 if (!class_exists($datasource)) { 514 throw new coding_exception('The data source class does not exist.'); 515 } 516 if (!array_key_exists('cache_data_source', class_implements($datasource))) { 517 throw new coding_exception('Cache data source classes must implement the cache_data_source interface'); 518 } 519 } 520 521 $cachedefinition = new cache_definition(); 522 $cachedefinition->id = $id; 523 $cachedefinition->mode = $mode; 524 $cachedefinition->component = $component; 525 $cachedefinition->area = $area; 526 $cachedefinition->simplekeys = $simplekeys; 527 $cachedefinition->simpledata = $simpledata; 528 $cachedefinition->requireidentifiers = $requireidentifiers; 529 $cachedefinition->requiredataguarantee = $requiredataguarantee; 530 $cachedefinition->requiremultipleidentifiers = $requiremultipleidentifiers; 531 $cachedefinition->requirelocking = $requirelocking; 532 $cachedefinition->requirelockingread = $requirelockingread; 533 $cachedefinition->requirelockingwrite = $requirelockingwrite; 534 $cachedefinition->requiresearchable = $requiresearchable; 535 $cachedefinition->maxsize = $maxsize; 536 $cachedefinition->overrideclass = $overrideclass; 537 $cachedefinition->overrideclassfile = $overrideclassfile; 538 $cachedefinition->datasource = $datasource; 539 $cachedefinition->datasourcefile = $datasourcefile; 540 $cachedefinition->staticacceleration = $staticacceleration; 541 $cachedefinition->staticaccelerationsize = $staticaccelerationsize; 542 $cachedefinition->ttl = $ttl; 543 $cachedefinition->mappingsonly = $mappingsonly; 544 $cachedefinition->invalidationevents = $invalidationevents; 545 $cachedefinition->sharingoptions = $sharingoptions; 546 $cachedefinition->selectedsharingoption = $selectedsharingoption; 547 $cachedefinition->userinputsharingkey = $userinputsharingkey; 548 $cachedefinition->canuselocalstore = $canuselocalstore; 549 550 return $cachedefinition; 551 } 552 553 /** 554 * Creates an ah-hoc cache definition given the required params. 555 * 556 * Please note that when using an adhoc definition you cannot set any of the optional params. 557 * This is because we cannot guarantee consistent access and we don't want to mislead people into thinking that. 558 * 559 * @param int $mode One of cache_store::MODE_* 560 * @param string $component The component this definition relates to. 561 * @param string $area The area this definition relates to. 562 * @param array $options An array of options, available options are: 563 * - simplekeys : Set to true if the keys you will use are a-zA-Z0-9_ 564 * - simpledata : Set to true if the type of the data you are going to store is scalar, or an array of scalar vars 565 * - overrideclass : The class to use as the loader. 566 * - staticacceleration : If set to true the cache will hold onto data passing through it. 567 * - staticaccelerationsize : Set it to an int to limit the size of the staticacceleration cache. 568 * @return cache_application|cache_session|cache_request 569 */ 570 public static function load_adhoc($mode, $component, $area, array $options = array()) { 571 $id = 'adhoc/'.$component.'_'.$area; 572 $definition = array( 573 'mode' => $mode, 574 'component' => $component, 575 'area' => $area, 576 ); 577 if (!empty($options['simplekeys'])) { 578 $definition['simplekeys'] = $options['simplekeys']; 579 } 580 if (!empty($options['simpledata'])) { 581 $definition['simpledata'] = $options['simpledata']; 582 } 583 if (!empty($options['persistent'])) { 584 // Ahhh this is the legacy persistent option. 585 $definition['staticacceleration'] = (bool)$options['persistent']; 586 } 587 if (!empty($options['staticacceleration'])) { 588 $definition['staticacceleration'] = (bool)$options['staticacceleration']; 589 } 590 if (!empty($options['staticaccelerationsize'])) { 591 $definition['staticaccelerationsize'] = (int)$options['staticaccelerationsize']; 592 } 593 if (!empty($options['overrideclass'])) { 594 $definition['overrideclass'] = $options['overrideclass']; 595 } 596 if (!empty($options['sharingoptions'])) { 597 $definition['sharingoptions'] = $options['sharingoptions']; 598 } 599 return self::load($id, $definition, null); 600 } 601 602 /** 603 * Returns the cache loader class that should be used for this definition. 604 * @return string 605 */ 606 public function get_cache_class() { 607 if (!is_null($this->overrideclass)) { 608 return $this->overrideclass; 609 } 610 return cache_helper::get_class_for_mode($this->mode); 611 } 612 613 /** 614 * Returns the id of this definition. 615 * @return string 616 */ 617 public function get_id() { 618 return $this->id; 619 } 620 621 /** 622 * Returns the name for this definition 623 * @return string 624 */ 625 public function get_name() { 626 $identifier = 'cachedef_'.clean_param($this->area, PARAM_STRINGID); 627 $component = $this->component; 628 if ($component === 'core') { 629 $component = 'cache'; 630 } 631 return new lang_string($identifier, $component); 632 } 633 634 /** 635 * Returns the mode of this definition 636 * @return int One more cache_store::MODE_ 637 */ 638 public function get_mode() { 639 return $this->mode; 640 } 641 642 /** 643 * Returns the area this definition is associated with. 644 * @return string 645 */ 646 public function get_area() { 647 return $this->area; 648 } 649 650 /** 651 * Returns the component this definition is associated with. 652 * @return string 653 */ 654 public function get_component() { 655 return $this->component; 656 } 657 658 /** 659 * Returns true if this definition is using simple keys. 660 * 661 * Simple keys contain only a-zA-Z0-9_ 662 * 663 * @return bool 664 */ 665 public function uses_simple_keys() { 666 return $this->simplekeys; 667 } 668 669 /** 670 * Returns the identifiers that are being used for this definition. 671 * @return array 672 */ 673 public function get_identifiers() { 674 if (!isset($this->identifiers)) { 675 return array(); 676 } 677 return $this->identifiers; 678 } 679 680 /** 681 * Returns the ttl in seconds for this definition if there is one, or null if not. 682 * @return int|null 683 */ 684 public function get_ttl() { 685 return $this->ttl; 686 } 687 688 /** 689 * Returns the maximum number of items allowed in this cache. 690 * @return int 691 */ 692 public function get_maxsize() { 693 return $this->maxsize; 694 } 695 696 /** 697 * Returns true if this definition should only be used with mappings. 698 * @return bool 699 */ 700 public function is_for_mappings_only() { 701 return $this->mappingsonly; 702 } 703 704 /** 705 * Returns true if the data is known to be scalar or array of scalar. 706 * @return bool 707 */ 708 public function uses_simple_data() { 709 return $this->simpledata; 710 } 711 712 /** 713 * Returns true if this definition requires a data guarantee from the cache stores being used. 714 * @return bool 715 */ 716 public function require_data_guarantee() { 717 return $this->requiredataguarantee; 718 } 719 720 /** 721 * Returns true if this definition requires that the cache stores support multiple identifiers 722 * @return bool 723 */ 724 public function require_multiple_identifiers() { 725 return $this->requiremultipleidentifiers; 726 } 727 728 /** 729 * Returns true if this definition requires locking functionality. Either read or write locking. 730 * @return bool 731 */ 732 public function require_locking() { 733 return $this->requirelocking; 734 } 735 736 /** 737 * Returns true if this definition requires read locking. 738 * @return bool 739 */ 740 public function require_locking_read() { 741 return $this->requirelockingread; 742 } 743 744 /** 745 * Returns true if this definition requires write locking. 746 * @return bool 747 */ 748 public function require_locking_write() { 749 return $this->requirelockingwrite; 750 } 751 752 /** 753 * Returns true if this definition allows local storage to be used for caching. 754 * @since Moodle 3.1.0 755 * @return bool 756 */ 757 public function can_use_localstore() { 758 return $this->canuselocalstore; 759 } 760 761 /** 762 * Returns true if this definition requires a searchable cache. 763 * @since Moodle 2.4.4 764 * @return bool 765 */ 766 public function require_searchable() { 767 return $this->requiresearchable; 768 } 769 770 /** 771 * Returns true if this definition has an associated data source. 772 * @return bool 773 */ 774 public function has_data_source() { 775 return !is_null($this->datasource); 776 } 777 778 /** 779 * Returns an instance of the data source class used for this definition. 780 * 781 * @return cache_data_source 782 * @throws coding_exception 783 */ 784 public function get_data_source() { 785 if (!$this->has_data_source()) { 786 throw new coding_exception('This cache does not use a data source.'); 787 } 788 return forward_static_call(array($this->datasource, 'get_instance_for_cache'), $this); 789 } 790 791 /** 792 * Sets the identifiers for this definition, or updates them if they have already been set. 793 * 794 * @param array $identifiers 795 * @return bool false if no identifiers where changed, true otherwise. 796 * @throws coding_exception 797 */ 798 public function set_identifiers(array $identifiers = array()) { 799 // If we are setting the exact same identifiers then just return as nothing really changed. 800 // We don't care about order as cache::make will use the same definition order all the time. 801 if ($identifiers === $this->identifiers) { 802 return false; 803 } 804 805 foreach ($this->requireidentifiers as $identifier) { 806 if (!isset($identifiers[$identifier])) { 807 throw new coding_exception('Identifier required for cache has not been provided: '.$identifier); 808 } 809 } 810 811 if ($this->identifiers === null) { 812 // Initialize identifiers if they have not been. 813 $this->identifiers = array(); 814 } 815 foreach ($identifiers as $name => $value) { 816 $this->identifiers[$name] = (string)$value; 817 } 818 // Reset the key prefix's they need updating now. 819 $this->keyprefixsingle = null; 820 $this->keyprefixmulti = null; 821 822 return true; 823 } 824 825 /** 826 * Returns the requirements of this definition as a binary flag. 827 * @return int 828 */ 829 public function get_requirements_bin() { 830 $requires = 0; 831 if ($this->require_data_guarantee()) { 832 $requires += cache_store::SUPPORTS_DATA_GUARANTEE; 833 } 834 if ($this->require_multiple_identifiers()) { 835 $requires += cache_store::SUPPORTS_MULTIPLE_IDENTIFIERS; 836 } 837 if ($this->require_searchable()) { 838 $requires += cache_store::IS_SEARCHABLE; 839 } 840 return $requires; 841 } 842 843 /** 844 * Please call {@link cache_definition::use_static_acceleration()} instead. 845 * 846 * @see cache_definition::use_static_acceleration() 847 * @deprecated since 2.6 848 */ 849 public function should_be_persistent() { 850 throw new coding_exception('cache_definition::should_be_persistent() can not be used anymore.' . 851 ' Please use cache_definition::use_static_acceleration() instead.'); 852 } 853 854 /** 855 * Returns true if we should hold onto the data flowing through the cache. 856 * 857 * If set to true data flowing through the cache will be stored in a static variable 858 * to make subsequent requests for the data much faster. 859 * 860 * @return bool 861 */ 862 public function use_static_acceleration() { 863 if ($this->mode === cache_store::MODE_REQUEST) { 864 // Request caches should never use static acceleration - it just doesn't make sense. 865 return false; 866 } 867 return $this->staticacceleration; 868 } 869 870 /** 871 * Please call {@link cache_definition::get_static_acceleration_size()} instead. 872 * 873 * @see cache_definition::get_static_acceleration_size() 874 * @deprecated since 2.6 875 */ 876 public function get_persistent_max_size() { 877 throw new coding_exception('cache_definition::get_persistent_max_size() can not be used anymore.' . 878 ' Please use cache_definition::get_static_acceleration_size() instead.'); 879 } 880 881 /** 882 * Returns the max size for the static acceleration array. 883 * @return int 884 */ 885 public function get_static_acceleration_size() { 886 return $this->staticaccelerationsize; 887 } 888 889 /** 890 * Generates a hash of this definition and returns it. 891 * @return string 892 */ 893 public function generate_definition_hash() { 894 if ($this->definitionhash === null) { 895 $this->definitionhash = md5("{$this->mode} {$this->component} {$this->area}"); 896 } 897 return $this->definitionhash; 898 } 899 900 /** 901 * Generates a single key prefix for this definition 902 * 903 * @return string 904 */ 905 public function generate_single_key_prefix() { 906 if ($this->keyprefixsingle === null) { 907 $this->keyprefixsingle = $this->mode.'/'.$this->component.'/'.$this->area; 908 $this->keyprefixsingle .= '/'.$this->get_cache_identifier(); 909 $identifiers = $this->get_identifiers(); 910 if ($identifiers) { 911 foreach ($identifiers as $key => $value) { 912 $this->keyprefixsingle .= '/'.$key.'='.$value; 913 } 914 } 915 $this->keyprefixsingle = md5($this->keyprefixsingle); 916 } 917 return $this->keyprefixsingle; 918 } 919 920 /** 921 * Generates a multi key prefix for this definition 922 * 923 * @return array 924 */ 925 public function generate_multi_key_parts() { 926 if ($this->keyprefixmulti === null) { 927 $this->keyprefixmulti = array( 928 'mode' => $this->mode, 929 'component' => $this->component, 930 'area' => $this->area, 931 'siteidentifier' => $this->get_cache_identifier() 932 ); 933 if (isset($this->identifiers) && !empty($this->identifiers)) { 934 $identifiers = array(); 935 foreach ($this->identifiers as $key => $value) { 936 $identifiers[] = htmlentities($key, ENT_QUOTES, 'UTF-8').'='.htmlentities($value, ENT_QUOTES, 'UTF-8'); 937 } 938 $this->keyprefixmulti['identifiers'] = join('&', $identifiers); 939 } 940 } 941 return $this->keyprefixmulti; 942 } 943 944 /** 945 * Check if this definition should invalidate on the given event. 946 * 947 * @param string $event 948 * @return bool True if the definition should invalidate on the event. False otherwise. 949 */ 950 public function invalidates_on_event($event) { 951 return (in_array($event, $this->invalidationevents)); 952 } 953 954 /** 955 * Check if the definition has any invalidation events. 956 * 957 * @return bool True if it does, false otherwise 958 */ 959 public function has_invalidation_events() { 960 return !empty($this->invalidationevents); 961 } 962 963 /** 964 * Returns all of the invalidation events for this definition. 965 * 966 * @return array 967 */ 968 public function get_invalidation_events() { 969 return $this->invalidationevents; 970 } 971 972 /** 973 * Returns a cache identification string. 974 * 975 * @return string A string to be used as part of keys. 976 */ 977 protected function get_cache_identifier() { 978 $identifiers = array(); 979 if ($this->selectedsharingoption & self::SHARING_ALL) { 980 // Nothing to do here. 981 } else { 982 if ($this->selectedsharingoption & self::SHARING_SITEID) { 983 $identifiers[] = cache_helper::get_site_identifier(); 984 } 985 if ($this->selectedsharingoption & self::SHARING_VERSION) { 986 $identifiers[] = cache_helper::get_site_version(); 987 } 988 if ($this->selectedsharingoption & self::SHARING_INPUT && !empty($this->userinputsharingkey)) { 989 $identifiers[] = $this->userinputsharingkey; 990 } 991 } 992 return join('/', $identifiers); 993 } 994 995 /** 996 * Returns true if this definition requires identifiers. 997 * 998 * @param bool 999 */ 1000 public function has_required_identifiers() { 1001 return (count($this->requireidentifiers) > 0); 1002 } 1003 1004 /** 1005 * Returns the possible sharing options that can be used with this defintion. 1006 * 1007 * @return int 1008 */ 1009 public function get_sharing_options() { 1010 return $this->sharingoptions; 1011 } 1012 1013 /** 1014 * Returns the user entered sharing key for this definition. 1015 * 1016 * @return string 1017 */ 1018 public function get_user_input_sharing_key() { 1019 return $this->userinputsharingkey; 1020 } 1021 1022 /** 1023 * Returns the user selected sharing option for this definition. 1024 * 1025 * @return int 1026 */ 1027 public function get_selected_sharing_option() { 1028 return $this->selectedsharingoption; 1029 } 1030 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |