Skip to content

Commit

Permalink
MDL-72837 core_cache: Remove duplicated code in cache_session
Browse files Browse the repository at this point in the history
I checked using a diff tool and the difference between 'get' in cache
and cache_session is only:

a) cache_session: call to check_tracked_user at the start
b) cache: Includes code to use static acceleration (but this would
   not run anyway in cache_session because use_static_acceleration
   returns false)
c) cache: $setaftervalidation logic is used so that it sets it slightly
   later (I can't see how this would make any difference)
d) cache_session: uses functions eg get_store() instead of variables
   $store; those functions aren't overridden.

This seems like a ridiculous duplication of very complicated code, so
I'm going to remove the duplication before I change it.
  • Loading branch information
sammarshallou committed Feb 23, 2022
1 parent 416e0bc commit 9777852
Showing 1 changed file with 3 additions and 54 deletions.
57 changes: 3 additions & 54 deletions cache/classes/loaders.php
Expand Up @@ -1837,60 +1837,9 @@ public function purge_current_user() {
public function get($key, $strictness = IGNORE_MISSING) {
// Check the tracked user.
$this->check_tracked_user();
// 2. Parse the key.
$parsedkey = $this->parse_key($key);
// 3. Get it from the store.
$result = $this->get_store()->get($parsedkey);
if ($result !== false) {
if ($result instanceof cache_ttl_wrapper) {
if ($result->has_expired()) {
$this->get_store()->delete($parsedkey);
$result = false;
} else {
$result = $result->data;
}
}
if ($result instanceof cache_cached_object) {
$result = $result->restore_object();
}
if ($this->perfdebug) {
$readbytes = $this->get_store()->get_last_io_bytes();
}
}
// 4. Load if from the loader/datasource if we don't already have it.
if ($result === false) {
if ($this->perfdebug) {
cache_helper::record_cache_miss($this->get_store(), $this->get_definition());
}
if ($this->get_loader() !== false) {
// We must pass the original (unparsed) key to the next loader in the chain.
// The next loader will parse the key as it sees fit. It may be parsed differently
// depending upon the capabilities of the store associated with the loader.
$result = $this->get_loader()->get($key);
} else if ($this->get_datasource() !== false) {
$result = $this->get_datasource()->load_for_cache($key);
}
// 5. Set it to the store if we got it from the loader/datasource.
if ($result !== false) {
$this->set($key, $result);
}
} else if ($this->perfdebug) {
cache_helper::record_cache_hit($this->get_store(), $this->get_definition(), 1, $readbytes);
}
// 5. Validate strictness.
if ($strictness === MUST_EXIST && $result === false) {
throw new coding_exception('Requested key did not exist in any cache stores and could not be loaded.');
}
// 6. Make sure we don't pass back anything that could be a reference.
// We don't want people modifying the data in the cache.
if (!$this->get_store()->supports_dereferencing_objects() && !is_scalar($result)) {
// If data is an object it will be a reference.
// If data is an array if may contain references.
// We want to break references so that the cache cannot be modified outside of itself.
// Call the function to unreference it (in the best way possible).
$result = $this->unref($result);
}
return $result;

// Use parent code.
return parent::get($key, $strictness);
}

/**
Expand Down

0 comments on commit 9777852

Please sign in to comment.