mirror of
https://github.com/moodle/moodle.git
synced 2025-08-06 09:26:35 +02:00
Merge branch 'wip-MDL-41106-m26' of git://github.com/samhemelryk/moodle
This commit is contained in:
commit
3415e1ca59
5 changed files with 513 additions and 200 deletions
333
cache/classes/loaders.php
vendored
333
cache/classes/loaders.php
vendored
|
@ -271,7 +271,7 @@ class cache implements cache_loader {
|
|||
* In advanced cases an array may be useful such as in situations requiring the multi-key functionality.
|
||||
* @param int $strictness One of IGNORE_MISSING | MUST_EXIST
|
||||
* @return mixed|false The data from the cache or false if the key did not exist within the cache.
|
||||
* @throws moodle_exception
|
||||
* @throws coding_exception
|
||||
*/
|
||||
public function get($key, $strictness = IGNORE_MISSING) {
|
||||
// 1. Parse the key.
|
||||
|
@ -329,7 +329,7 @@ class cache implements cache_loader {
|
|||
}
|
||||
// 5. Validate strictness.
|
||||
if ($strictness === MUST_EXIST && $result === false) {
|
||||
throw new moodle_exception('Requested key did not exist in any cache stores and could not be loaded.');
|
||||
throw new coding_exception('Requested key did not exist in any cache stores and could not be loaded.');
|
||||
}
|
||||
// 6. Set it to the store if we got it from the loader/datasource.
|
||||
if ($setaftervalidation) {
|
||||
|
@ -363,7 +363,7 @@ class cache implements cache_loader {
|
|||
* @return array An array of key value pairs for the items that could be retrieved from the cache.
|
||||
* If MUST_EXIST was used and not all keys existed within the cache then an exception will be thrown.
|
||||
* Otherwise any key that did not exist will have a data value of false within the results.
|
||||
* @throws moodle_exception
|
||||
* @throws coding_exception
|
||||
*/
|
||||
public function get_many(array $keys, $strictness = IGNORE_MISSING) {
|
||||
|
||||
|
@ -420,7 +420,7 @@ class cache implements cache_loader {
|
|||
$missingkeys = array();
|
||||
foreach ($result as $key => $value) {
|
||||
if ($value === false) {
|
||||
$missingkeys[] = ($usingloader) ? $key : $parsedkeys[$key];
|
||||
$missingkeys[] = $parsedkeys[$key];
|
||||
}
|
||||
}
|
||||
if (!empty($missingkeys)) {
|
||||
|
@ -430,11 +430,9 @@ class cache implements cache_loader {
|
|||
$resultmissing = $this->datasource->load_many_for_cache($missingkeys);
|
||||
}
|
||||
foreach ($resultmissing as $key => $value) {
|
||||
$pkey = ($usingloader) ? $key : $keysparsed[$key];
|
||||
$realkey = ($usingloader) ? $parsedkeys[$key] : $key;
|
||||
$result[$pkey] = $value;
|
||||
$result[$keysparsed[$key]] = $value;
|
||||
if ($value !== false) {
|
||||
$this->set($realkey, $value);
|
||||
$this->set($key, $value);
|
||||
}
|
||||
}
|
||||
unset($resultmissing);
|
||||
|
@ -453,7 +451,7 @@ class cache implements cache_loader {
|
|||
if ($strictness === MUST_EXIST) {
|
||||
foreach ($keys as $key) {
|
||||
if (!array_key_exists($key, $fullresult)) {
|
||||
throw new moodle_exception('Not all the requested keys existed within the cache stores.');
|
||||
throw new coding_exception('Not all the requested keys existed within the cache stores.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -483,6 +481,11 @@ class cache implements cache_loader {
|
|||
if ($this->perfdebug) {
|
||||
cache_helper::record_cache_set($this->storetype, $this->definition->get_id());
|
||||
}
|
||||
if ($this->loader !== false) {
|
||||
// We have a loader available set it there as well.
|
||||
// We have to let the loader do its own parsing of data as it may be unique.
|
||||
$this->loader->set($key, $data);
|
||||
}
|
||||
if (is_object($data) && $data instanceof cacheable_object) {
|
||||
$data = new cache_cached_object($data);
|
||||
} else if (!is_scalar($data)) {
|
||||
|
@ -506,6 +509,7 @@ class cache implements cache_loader {
|
|||
* Removes references where required.
|
||||
*
|
||||
* @param stdClass|array $data
|
||||
* @return mixed What ever was put in but without any references.
|
||||
*/
|
||||
protected function unref($data) {
|
||||
if ($this->definition->uses_simple_data()) {
|
||||
|
@ -593,6 +597,11 @@ class cache implements cache_loader {
|
|||
* ... if they care that is.
|
||||
*/
|
||||
public function set_many(array $keyvaluearray) {
|
||||
if ($this->loader !== false) {
|
||||
// We have a loader available set it there as well.
|
||||
// We have to let the loader do its own parsing of data as it may be unique.
|
||||
$this->loader->set_many($keyvaluearray);
|
||||
}
|
||||
$data = array();
|
||||
$simulatettl = $this->has_a_ttl() && !$this->store_supports_native_ttl();
|
||||
$usepersistcache = $this->is_using_persist_cache();
|
||||
|
@ -858,7 +867,7 @@ class cache implements cache_loader {
|
|||
* Returns the loader associated with this instance.
|
||||
*
|
||||
* @since 2.4.4
|
||||
* @return cache_loader|false
|
||||
* @return cache|false
|
||||
*/
|
||||
protected function get_loader() {
|
||||
return $this->loader;
|
||||
|
@ -1340,7 +1349,6 @@ class cache_application extends cache implements cache_loader_with_locking {
|
|||
* @param string|int $key The key for the data being requested.
|
||||
* @param int $strictness One of IGNORE_MISSING | MUST_EXIST
|
||||
* @return mixed|false The data from the cache or false if the key did not exist within the cache.
|
||||
* @throws moodle_exception
|
||||
*/
|
||||
public function get($key, $strictness = IGNORE_MISSING) {
|
||||
if ($this->requirelockingread && $this->check_lock_state($key) === false) {
|
||||
|
@ -1364,7 +1372,7 @@ class cache_application extends cache implements cache_loader_with_locking {
|
|||
* @return array An array of key value pairs for the items that could be retrieved from the cache.
|
||||
* If MUST_EXIST was used and not all keys existed within the cache then an exception will be thrown.
|
||||
* Otherwise any key that did not exist will have a data value of false within the results.
|
||||
* @throws moodle_exception
|
||||
* @throws coding_exception
|
||||
*/
|
||||
public function get_many(array $keys, $strictness = IGNORE_MISSING) {
|
||||
if ($this->requirelockingread) {
|
||||
|
@ -1458,6 +1466,7 @@ class cache_application extends cache implements cache_loader_with_locking {
|
|||
* @todo we should support locking in the session as well. Should be pretty simple to set up.
|
||||
*
|
||||
* @internal don't use me directly.
|
||||
* @method cache_store|cache_is_searchable get_store() Returns the cache store which must implement both cache_is_searchable.
|
||||
*
|
||||
* @package core
|
||||
* @category cache
|
||||
|
@ -1494,6 +1503,11 @@ class cache_session extends cache {
|
|||
*/
|
||||
const KEY_PREFIX = 'sess_';
|
||||
|
||||
/**
|
||||
* This is the key used to track last access.
|
||||
*/
|
||||
const LASTACCESS = '__lastaccess__';
|
||||
|
||||
/**
|
||||
* Override the cache::construct method.
|
||||
*
|
||||
|
@ -1507,12 +1521,15 @@ class cache_session extends cache {
|
|||
* @param cache_definition $definition
|
||||
* @param cache_store $store
|
||||
* @param cache_loader|cache_data_source $loader
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(cache_definition $definition, cache_store $store, $loader = null) {
|
||||
// First up copy the loadeduserid to the current user id.
|
||||
$this->currentuserid = self::$loadeduserid;
|
||||
parent::__construct($definition, $store, $loader);
|
||||
|
||||
// This will trigger check tracked user. If this gets removed a call to that will need to be added here in its place.
|
||||
$this->set(self::LASTACCESS, cache::now());
|
||||
|
||||
if ($definition->has_invalidation_events()) {
|
||||
$lastinvalidation = $this->get('lastsessioninvalidation');
|
||||
if ($lastinvalidation === false) {
|
||||
|
@ -1561,6 +1578,21 @@ class cache_session extends cache {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the session id for the loader.
|
||||
*/
|
||||
protected function set_session_id() {
|
||||
$this->sessionid = preg_replace('#[^a-zA-Z0-9_]#', '_', session_id());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the prefix used for all keys.
|
||||
* @return string
|
||||
*/
|
||||
protected function get_key_prefix() {
|
||||
return 'u'.$this->currentuserid.'_'.$this->sessionid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the key turning it into a string (or array is required) suitable to be passed to the cache store.
|
||||
*
|
||||
|
@ -1573,17 +1605,18 @@ class cache_session extends cache {
|
|||
* @return string|array String unless the store supports multi-identifiers in which case an array if returned.
|
||||
*/
|
||||
protected function parse_key($key) {
|
||||
if ($key === 'lastaccess') {
|
||||
$key = '__lastaccess__';
|
||||
$prefix = $this->get_key_prefix();
|
||||
if ($key === self::LASTACCESS) {
|
||||
return $key.$prefix;
|
||||
}
|
||||
return 'sess_'.parent::parse_key($key);
|
||||
return $prefix.'_'.parent::parse_key($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that this cache instance is tracking the current user.
|
||||
*/
|
||||
protected function check_tracked_user() {
|
||||
if (isset($_SESSION['USER']->id)) {
|
||||
if (isset($_SESSION['USER']->id) && $_SESSION['USER']->id !== null) {
|
||||
// Get the id of the current user.
|
||||
$new = $_SESSION['USER']->id;
|
||||
} else {
|
||||
|
@ -1597,55 +1630,25 @@ class cache_session extends cache {
|
|||
// This way we don't bloat the session.
|
||||
$this->purge();
|
||||
// Update the session id just in case!
|
||||
$this->sessionid = session_id();
|
||||
$this->set_session_id();
|
||||
}
|
||||
self::$loadeduserid = $new;
|
||||
$this->currentuserid = $new;
|
||||
} else if ($new !== $this->currentuserid) {
|
||||
// The current user matches the loaded user but not the user last used by this cache.
|
||||
$this->purge();
|
||||
$this->purge_current_user();
|
||||
$this->currentuserid = $new;
|
||||
// Update the session id just in case!
|
||||
$this->sessionid = session_id();
|
||||
$this->set_session_id();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the session data.
|
||||
*
|
||||
* @param bool $force If true the session data will be loaded from the store again.
|
||||
* @return array An array of session data.
|
||||
* Purges the session cache of all data belonging to the current user.
|
||||
*/
|
||||
protected function get_session_data($force = false) {
|
||||
if ($this->sessionid === null) {
|
||||
$this->sessionid = session_id();
|
||||
}
|
||||
if (is_array($this->session) && !$force) {
|
||||
return $this->session;
|
||||
}
|
||||
$session = parent::get($this->sessionid);
|
||||
if ($session === false) {
|
||||
$session = array();
|
||||
}
|
||||
// We have to write here to ensure that the lastaccess time is recorded.
|
||||
// And also in order to ensure the session entry exists as when we save it on __destruct
|
||||
// $CFG is likely to have already been destroyed.
|
||||
$this->save_session($session);
|
||||
return $this->session;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the session data.
|
||||
*
|
||||
* This function also updates the last access time.
|
||||
*
|
||||
* @param array $session
|
||||
* @return bool
|
||||
*/
|
||||
protected function save_session(array $session) {
|
||||
$session['lastaccess'] = time();
|
||||
$this->session = $session;
|
||||
return parent::set($this->sessionid, $this->session);
|
||||
public function purge_current_user() {
|
||||
$keys = $this->get_store()->find_all($this->get_key_prefix());
|
||||
$this->get_store()->delete_many($keys);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1656,7 +1659,7 @@ class cache_session extends cache {
|
|||
* In advanced cases an array may be useful such as in situations requiring the multi-key functionality.
|
||||
* @param int $strictness One of IGNORE_MISSING | MUST_EXIST
|
||||
* @return mixed|false The data from the cache or false if the key did not exist within the cache.
|
||||
* @throws moodle_exception
|
||||
* @throws coding_exception
|
||||
*/
|
||||
public function get($key, $strictness = IGNORE_MISSING) {
|
||||
// Check the tracked user.
|
||||
|
@ -1664,10 +1667,8 @@ class cache_session extends cache {
|
|||
// 2. Parse the key.
|
||||
$parsedkey = $this->parse_key($key);
|
||||
// 3. Get it from the store.
|
||||
$result = false;
|
||||
$session = $this->get_session_data();
|
||||
if (array_key_exists($parsedkey, $session)) {
|
||||
$result = $session[$parsedkey];
|
||||
$result = $this->get_store()->get($parsedkey);
|
||||
if ($result !== false) {
|
||||
if ($result instanceof cache_ttl_wrapper) {
|
||||
if ($result->has_expired()) {
|
||||
$this->get_store()->delete($parsedkey);
|
||||
|
@ -1681,10 +1682,9 @@ class cache_session extends cache {
|
|||
}
|
||||
}
|
||||
// 4. Load if from the loader/datasource if we don't already have it.
|
||||
$setaftervalidation = false;
|
||||
if ($result === false) {
|
||||
if ($this->perfdebug) {
|
||||
cache_helper::record_cache_miss('**static session**', $this->get_definition()->get_id());
|
||||
cache_helper::record_cache_miss($this->storetype, $this->get_definition()->get_id());
|
||||
}
|
||||
if ($this->get_loader() !== false) {
|
||||
// We must pass the original (unparsed) key to the next loader in the chain.
|
||||
|
@ -1694,19 +1694,18 @@ class cache_session extends cache {
|
|||
} else if ($this->get_datasource() !== false) {
|
||||
$result = $this->get_datasource()->load_for_cache($key);
|
||||
}
|
||||
$setaftervalidation = ($result !== false);
|
||||
// 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('**static session**', $this->get_definition()->get_id());
|
||||
cache_helper::record_cache_hit($this->storetype, $this->get_definition()->get_id());
|
||||
}
|
||||
// 5. Validate strictness.
|
||||
if ($strictness === MUST_EXIST && $result === false) {
|
||||
throw new moodle_exception('Requested key did not exist in any cache stores and could not be loaded.');
|
||||
throw new coding_exception('Requested key did not exist in any cache stores and could not be loaded.');
|
||||
}
|
||||
// 6. Set it to the store if we got it from the loader/datasource.
|
||||
if ($setaftervalidation) {
|
||||
$this->set($key, $result);
|
||||
}
|
||||
// 7. Make sure we don't pass back anything that could be a reference.
|
||||
// 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 (!is_scalar($result)) {
|
||||
// If data is an object it will be a reference.
|
||||
|
@ -1737,8 +1736,14 @@ class cache_session extends cache {
|
|||
*/
|
||||
public function set($key, $data) {
|
||||
$this->check_tracked_user();
|
||||
$loader = $this->get_loader();
|
||||
if ($loader !== false) {
|
||||
// We have a loader available set it there as well.
|
||||
// We have to let the loader do its own parsing of data as it may be unique.
|
||||
$loader->set($key, $data);
|
||||
}
|
||||
if ($this->perfdebug) {
|
||||
cache_helper::record_cache_set('**static session**', $this->get_definition()->get_id());
|
||||
cache_helper::record_cache_set($this->storetype, $this->get_definition()->get_id());
|
||||
}
|
||||
if (is_object($data) && $data instanceof cacheable_object) {
|
||||
$data = new cache_cached_object($data);
|
||||
|
@ -1750,12 +1755,10 @@ class cache_session extends cache {
|
|||
$data = $this->unref($data);
|
||||
}
|
||||
// We dont' support native TTL here as we consolidate data for sessions.
|
||||
if ($this->has_a_ttl()) {
|
||||
if ($this->has_a_ttl() && !$this->store_supports_native_ttl()) {
|
||||
$data = new cache_ttl_wrapper($data, $this->get_definition()->get_ttl());
|
||||
}
|
||||
$session = $this->get_session_data();
|
||||
$session[$this->parse_key($key)] = $data;
|
||||
return $this->save_session($session);
|
||||
return $this->get_store()->set($this->parse_key($key), $data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1767,15 +1770,12 @@ class cache_session extends cache {
|
|||
* @return bool True of success, false otherwise.
|
||||
*/
|
||||
public function delete($key, $recurse = true) {
|
||||
$this->check_tracked_user();
|
||||
$parsedkey = $this->parse_key($key);
|
||||
if ($recurse && $this->get_loader() !== false) {
|
||||
// Delete from the bottom of the stack first.
|
||||
$this->get_loader()->delete($key, $recurse);
|
||||
}
|
||||
$session = $this->get_session_data();
|
||||
unset($session[$parsedkey]);
|
||||
return $this->save_session($session);
|
||||
return $this->get_store()->delete($parsedkey);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1794,15 +1794,72 @@ class cache_session extends cache {
|
|||
* @return array An array of key value pairs for the items that could be retrieved from the cache.
|
||||
* If MUST_EXIST was used and not all keys existed within the cache then an exception will be thrown.
|
||||
* Otherwise any key that did not exist will have a data value of false within the results.
|
||||
* @throws moodle_exception
|
||||
* @throws coding_exception
|
||||
*/
|
||||
public function get_many(array $keys, $strictness = IGNORE_MISSING) {
|
||||
$this->check_tracked_user();
|
||||
$return = array();
|
||||
$parsedkeys = array();
|
||||
$keymap = array();
|
||||
foreach ($keys as $key) {
|
||||
$return[$key] = $this->get($key, $strictness);
|
||||
$parsedkey = $this->parse_key($key);
|
||||
$parsedkeys[$key] = $parsedkey;
|
||||
$keymap[$parsedkey] = $key;
|
||||
}
|
||||
$result = $this->get_store()->get_many($parsedkeys);
|
||||
$return = array();
|
||||
$missingkeys = array();
|
||||
$hasmissingkeys = false;
|
||||
foreach ($result as $parsedkey => $value) {
|
||||
$key = $keymap[$parsedkey];
|
||||
if ($value instanceof cache_ttl_wrapper) {
|
||||
/* @var cache_ttl_wrapper $value */
|
||||
if ($value->has_expired()) {
|
||||
$this->delete($keymap[$parsedkey]);
|
||||
$value = false;
|
||||
} else {
|
||||
$value = $value->data;
|
||||
}
|
||||
}
|
||||
if ($value instanceof cache_cached_object) {
|
||||
/* @var cache_cached_object $value */
|
||||
$value = $value->restore_object();
|
||||
}
|
||||
$return[$key] = $value;
|
||||
if ($value === false) {
|
||||
$hasmissingkeys = true;
|
||||
$missingkeys[$parsedkey] = $key;
|
||||
}
|
||||
}
|
||||
if ($hasmissingkeys) {
|
||||
// We've got missing keys - we've got to check any loaders or data sources.
|
||||
$loader = $this->get_loader();
|
||||
$datasource = $this->get_datasource();
|
||||
if ($loader !== false) {
|
||||
foreach ($loader->get_many($missingkeys) as $key => $value) {
|
||||
if ($value !== false) {
|
||||
$return[$key] = $value;
|
||||
unset($missingkeys[$parsedkeys[$key]]);
|
||||
}
|
||||
}
|
||||
}
|
||||
$hasmissingkeys = count($missingkeys) > 0;
|
||||
if ($datasource !== false && $hasmissingkeys) {
|
||||
// We're still missing keys but we've got a datasource.
|
||||
foreach ($datasource->load_many_for_cache($missingkeys) as $key => $value) {
|
||||
if ($value !== false) {
|
||||
$return[$key] = $value;
|
||||
unset($missingkeys[$parsedkeys[$key]]);
|
||||
}
|
||||
}
|
||||
$hasmissingkeys = count($missingkeys) > 0;
|
||||
}
|
||||
}
|
||||
if ($hasmissingkeys && $strictness === MUST_EXIST) {
|
||||
throw new coding_exception('Requested key did not exist in any cache stores and could not be loaded.');
|
||||
}
|
||||
|
||||
return $return;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1814,18 +1871,12 @@ class cache_session extends cache {
|
|||
* @return int The number of items successfully deleted.
|
||||
*/
|
||||
public function delete_many(array $keys, $recurse = true) {
|
||||
$this->check_tracked_user();
|
||||
$parsedkeys = array_map(array($this, 'parse_key'), $keys);
|
||||
if ($recurse && $this->get_loader() !== false) {
|
||||
// Delete from the bottom of the stack first.
|
||||
$this->get_loader()->delete_many($keys, $recurse);
|
||||
}
|
||||
$session = $this->get_session_data();
|
||||
foreach ($parsedkeys as $parsedkey) {
|
||||
unset($session[$parsedkey]);
|
||||
}
|
||||
$this->save_session($session);
|
||||
return count($keys);
|
||||
return $this->get_store()->delete_many($parsedkeys);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1853,8 +1904,15 @@ class cache_session extends cache {
|
|||
*/
|
||||
public function set_many(array $keyvaluearray) {
|
||||
$this->check_tracked_user();
|
||||
$session = $this->get_session_data();
|
||||
$simulatettl = $this->has_a_ttl();
|
||||
$loader = $this->get_loader();
|
||||
if ($loader !== false) {
|
||||
// We have a loader available set it there as well.
|
||||
// We have to let the loader do its own parsing of data as it may be unique.
|
||||
$loader->set_many($keyvaluearray);
|
||||
}
|
||||
$data = array();
|
||||
$definitionid = $this->get_definition()->get_ttl();
|
||||
$simulatettl = $this->has_a_ttl() && !$this->store_supports_native_ttl();
|
||||
foreach ($keyvaluearray as $key => $value) {
|
||||
if (is_object($value) && $value instanceof cacheable_object) {
|
||||
$value = new cache_cached_object($value);
|
||||
|
@ -1866,16 +1924,17 @@ class cache_session extends cache {
|
|||
$value = $this->unref($value);
|
||||
}
|
||||
if ($simulatettl) {
|
||||
$value = new cache_ttl_wrapper($value, $this->get_definition()->get_ttl());
|
||||
$value = new cache_ttl_wrapper($value, $definitionid);
|
||||
}
|
||||
$parsedkey = $this->parse_key($key);
|
||||
$session[$parsedkey] = $value;
|
||||
$data[$key] = array(
|
||||
'key' => $this->parse_key($key),
|
||||
'value' => $value
|
||||
);
|
||||
}
|
||||
if ($this->perfdebug) {
|
||||
cache_helper::record_cache_set($this->storetype, $this->get_definition()->get_id());
|
||||
cache_helper::record_cache_set($this->storetype, $definitionid);
|
||||
}
|
||||
$this->save_session($session);
|
||||
return count($keyvaluearray);
|
||||
return $this->get_store()->set_many($data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1884,13 +1943,9 @@ class cache_session extends cache {
|
|||
* @return bool True on success, false otherwise
|
||||
*/
|
||||
public function purge() {
|
||||
// 1. Purge the session object.
|
||||
$this->session = array();
|
||||
// 2. Delete the record for this users session from the store.
|
||||
$this->get_store()->delete($this->sessionid);
|
||||
// 3. Optionally purge any stacked loaders in the same way.
|
||||
$this->get_store()->purge();
|
||||
if ($this->get_loader()) {
|
||||
$this->get_loader()->delete($this->sessionid);
|
||||
$this->get_loader()->purge();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -1919,21 +1974,27 @@ class cache_session extends cache {
|
|||
public function has($key, $tryloadifpossible = false) {
|
||||
$this->check_tracked_user();
|
||||
$parsedkey = $this->parse_key($key);
|
||||
$session = $this->get_session_data();
|
||||
$has = false;
|
||||
if ($this->has_a_ttl()) {
|
||||
$store = $this->get_store();
|
||||
if ($this->has_a_ttl() && !$this->store_supports_native_ttl()) {
|
||||
// The data has a TTL and the store doesn't support it natively.
|
||||
// We must fetch the data and expect a ttl wrapper.
|
||||
if (array_key_exists($parsedkey, $session)) {
|
||||
$data = $session[$parsedkey];
|
||||
$has = ($data instanceof cache_ttl_wrapper && !$data->has_expired());
|
||||
}
|
||||
$data = $store->get($parsedkey);
|
||||
$has = ($data instanceof cache_ttl_wrapper && !$data->has_expired());
|
||||
} else if (!$this->store_supports_key_awareness()) {
|
||||
// The store doesn't support key awareness, get the data and check it manually... puke.
|
||||
// Either no TTL is set of the store supports its handling natively.
|
||||
$data = $store->get($parsedkey);
|
||||
$has = ($data !== false);
|
||||
} else {
|
||||
$has = array_key_exists($parsedkey, $session);
|
||||
// The store supports key awareness, this is easy!
|
||||
// Either no TTL is set of the store supports its handling natively.
|
||||
/* @var cache_store|cache_is_key_aware $store */
|
||||
$has = $store->has($parsedkey);
|
||||
}
|
||||
if (!$has && $tryloadifpossible) {
|
||||
$result = null;
|
||||
if ($this->get_loader() !== false) {
|
||||
$result = $this->get_loader()->get($key);
|
||||
$result = $this->get_loader()->get($parsedkey);
|
||||
} else if ($this->get_datasource() !== null) {
|
||||
$result = $this->get_datasource()->load_for_cache($key);
|
||||
}
|
||||
|
@ -1960,25 +2021,18 @@ class cache_session extends cache {
|
|||
*/
|
||||
public function has_all(array $keys) {
|
||||
$this->check_tracked_user();
|
||||
$session = $this->get_session_data();
|
||||
foreach ($keys as $key) {
|
||||
$has = false;
|
||||
$parsedkey = $this->parse_key($key);
|
||||
if ($this->has_a_ttl()) {
|
||||
// The data has a TTL and the store doesn't support it natively.
|
||||
// We must fetch the data and expect a ttl wrapper.
|
||||
if (array_key_exists($parsedkey, $session)) {
|
||||
$data = $session[$parsedkey];
|
||||
$has = ($data instanceof cache_ttl_wrapper && !$data->has_expired());
|
||||
if (($this->has_a_ttl() && !$this->store_supports_native_ttl()) || !$this->store_supports_key_awareness()) {
|
||||
foreach ($keys as $key) {
|
||||
if (!$this->has($key)) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
$has = array_key_exists($parsedkey, $session);
|
||||
}
|
||||
if (!$has) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
// The cache must be key aware and if support native ttl if it a ttl is set.
|
||||
/* @var cache_store|cache_is_key_aware $store */
|
||||
$store = $this->get_store();
|
||||
return $store->has_all(array_map(array($this, 'parse_key'), $keys));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1995,26 +2049,17 @@ class cache_session extends cache {
|
|||
* @return bool True if the cache has at least one of the given keys
|
||||
*/
|
||||
public function has_any(array $keys) {
|
||||
$this->check_tracked_user();
|
||||
$session = $this->get_session_data();
|
||||
foreach ($keys as $key) {
|
||||
$has = false;
|
||||
$parsedkey = $this->parse_key($key);
|
||||
if ($this->has_a_ttl()) {
|
||||
// The data has a TTL and the store doesn't support it natively.
|
||||
// We must fetch the data and expect a ttl wrapper.
|
||||
if (array_key_exists($parsedkey, $session)) {
|
||||
$data = $session[$parsedkey];
|
||||
$has = ($data instanceof cache_ttl_wrapper && !$data->has_expired());
|
||||
if (($this->has_a_ttl() && !$this->store_supports_native_ttl()) || !$this->store_supports_key_awareness()) {
|
||||
foreach ($keys as $key) {
|
||||
if ($this->has($key)) {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
$has = array_key_exists($parsedkey, $session);
|
||||
}
|
||||
if ($has) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
/* @var cache_store|cache_is_key_aware $store */
|
||||
$store = $this->get_store();
|
||||
return $store->has_any(array_map(array($this, 'parse_key'), $keys));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
119
cache/stores/session/lib.php
vendored
119
cache/stores/session/lib.php
vendored
|
@ -118,7 +118,7 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
|
|||
|
||||
/**
|
||||
* The maximum size for the store, or false if there isn't one.
|
||||
* @var bool
|
||||
* @var bool|int
|
||||
*/
|
||||
protected $maxsize = false;
|
||||
|
||||
|
@ -202,7 +202,7 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
|
|||
*/
|
||||
public function initialise(cache_definition $definition) {
|
||||
$this->storeid = $definition->generate_definition_hash();
|
||||
$this->store = &self::register_store_id($definition->get_id());
|
||||
$this->store = &self::register_store_id($this->name.'-'.$definition->get_id());
|
||||
$this->ttl = $definition->get_ttl();
|
||||
$maxsize = $definition->get_maxsize();
|
||||
if ($maxsize !== null) {
|
||||
|
@ -210,6 +210,7 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
|
|||
$this->maxsize = abs((int)$maxsize);
|
||||
$this->storecount = count($this->store);
|
||||
}
|
||||
$this->check_ttl();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -238,9 +239,17 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
|
|||
public function get($key) {
|
||||
if (isset($this->store[$key])) {
|
||||
if ($this->ttl == 0) {
|
||||
return $this->store[$key][0];
|
||||
$value = $this->store[$key][0];
|
||||
if ($this->maxsize !== false) {
|
||||
// Make sure the element is now in the end of array.
|
||||
$this->set($key, $value);
|
||||
}
|
||||
return $value;
|
||||
} else if ($this->store[$key][1] >= (cache::now() - $this->ttl)) {
|
||||
return $this->store[$key][0];
|
||||
} else {
|
||||
// Element is present but has expired.
|
||||
$this->check_ttl();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
@ -257,20 +266,32 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
|
|||
*/
|
||||
public function get_many($keys) {
|
||||
$return = array();
|
||||
$maxtime = 0;
|
||||
if ($this->ttl != 0) {
|
||||
$maxtime = cache::now() - $this->ttl;
|
||||
}
|
||||
|
||||
$hasexpiredelements = false;
|
||||
foreach ($keys as $key) {
|
||||
$return[$key] = false;
|
||||
if (isset($this->store[$key])) {
|
||||
if ($this->ttl == 0) {
|
||||
$return[$key] = $this->store[$key][0];
|
||||
if ($this->maxsize !== false) {
|
||||
// Make sure the element is now in the end of array.
|
||||
$this->set($key, $return[$key], false);
|
||||
}
|
||||
} else if ($this->store[$key][1] >= $maxtime) {
|
||||
$return[$key] = $this->store[$key][0];
|
||||
} else {
|
||||
$hasexpiredelements = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($hasexpiredelements) {
|
||||
// There are some elements that are present but have expired.
|
||||
$this->check_ttl();
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
|
@ -279,24 +300,27 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
|
|||
*
|
||||
* @param string $key The key to use.
|
||||
* @param mixed $data The data to set.
|
||||
* @param bool $testmaxsize If set to true then we test the maxsize arg and reduce if required.
|
||||
* @param bool $testmaxsize If set to true then we test the maxsize arg and reduce if required. If this is set to false you will
|
||||
* need to perform these checks yourself. This allows for bulk set's to be performed and maxsize tests performed once.
|
||||
* @return bool True if the operation was a success false otherwise.
|
||||
*/
|
||||
public function set($key, $data, $testmaxsize = true) {
|
||||
$testmaxsize = ($testmaxsize && $this->maxsize !== false);
|
||||
if ($testmaxsize) {
|
||||
$increment = (!isset($this->store[$key]));
|
||||
$increment = $this->maxsize !== false && !isset($this->store[$key]);
|
||||
if (($this->maxsize !== false && !$increment) || $this->ttl != 0) {
|
||||
// Make sure the element is added to the end of $this->store array.
|
||||
unset($this->store[$key]);
|
||||
}
|
||||
if ($this->ttl == 0) {
|
||||
$this->store[$key][0] = $data;
|
||||
if ($this->ttl === 0) {
|
||||
$this->store[$key] = array($data, 0);
|
||||
} else {
|
||||
$this->store[$key] = array($data, cache::now());
|
||||
}
|
||||
if ($testmaxsize && $increment) {
|
||||
if ($increment) {
|
||||
$this->storecount++;
|
||||
if ($this->storecount > $this->maxsize) {
|
||||
$this->reduce_for_maxsize();
|
||||
}
|
||||
}
|
||||
if ($testmaxsize && $this->storecount > $this->maxsize) {
|
||||
$this->reduce_for_maxsize();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -311,12 +335,26 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
|
|||
*/
|
||||
public function set_many(array $keyvaluearray) {
|
||||
$count = 0;
|
||||
$increment = 0;
|
||||
foreach ($keyvaluearray as $pair) {
|
||||
$this->set($pair['key'], $pair['value'], false);
|
||||
$key = $pair['key'];
|
||||
$data = $pair['value'];
|
||||
$count++;
|
||||
if ($this->maxsize !== false || $this->ttl !== 0) {
|
||||
// Make sure the element is added to the end of $this->store array.
|
||||
$this->delete($key);
|
||||
$increment++;
|
||||
} else if (!isset($this->store[$key])) {
|
||||
$increment++;
|
||||
}
|
||||
if ($this->ttl === 0) {
|
||||
$this->store[$key] = array($data, 0);
|
||||
} else {
|
||||
$this->store[$key] = array($data, cache::now());
|
||||
}
|
||||
}
|
||||
if ($this->maxsize !== false) {
|
||||
$this->storecount += $count;
|
||||
$this->storecount += $increment;
|
||||
if ($this->storecount > $this->maxsize) {
|
||||
$this->reduce_for_maxsize();
|
||||
}
|
||||
|
@ -348,6 +386,7 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
|
|||
* @return bool
|
||||
*/
|
||||
public function has_all(array $keys) {
|
||||
$maxtime = 0;
|
||||
if ($this->ttl != 0) {
|
||||
$maxtime = cache::now() - $this->ttl;
|
||||
}
|
||||
|
@ -370,6 +409,7 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
|
|||
* @return bool
|
||||
*/
|
||||
public function has_any(array $keys) {
|
||||
$maxtime = 0;
|
||||
if ($this->ttl != 0) {
|
||||
$maxtime = cache::now() - $this->ttl;
|
||||
}
|
||||
|
@ -389,12 +429,14 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
|
|||
* @return bool Returns true if the operation was a success, false otherwise.
|
||||
*/
|
||||
public function delete($key) {
|
||||
$result = isset($this->store[$key]);
|
||||
if (!isset($this->store[$key])) {
|
||||
return true;
|
||||
}
|
||||
unset($this->store[$key]);
|
||||
if ($this->maxsize !== false) {
|
||||
$this->storecount--;
|
||||
}
|
||||
return $result;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -404,15 +446,19 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
|
|||
* @return int The number of items successfully deleted.
|
||||
*/
|
||||
public function delete_many(array $keys) {
|
||||
// The number of items that have been successfully deleted.
|
||||
$count = 0;
|
||||
// The number of items that have actually being removed (how much to decrement maxsize by).
|
||||
$reduction = 0;
|
||||
foreach ($keys as $key) {
|
||||
if (isset($this->store[$key])) {
|
||||
$count++;
|
||||
$reduction++;
|
||||
}
|
||||
$count++;
|
||||
unset($this->store[$key]);
|
||||
}
|
||||
if ($this->maxsize !== false) {
|
||||
$this->storecount -= $count;
|
||||
$this->storecount -= $reduction;
|
||||
}
|
||||
return $count;
|
||||
}
|
||||
|
@ -489,12 +535,40 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
|
|||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes expired elements.
|
||||
* @return int number of removed elements
|
||||
*/
|
||||
protected function check_ttl() {
|
||||
if ($this->ttl === 0) {
|
||||
return 0;
|
||||
}
|
||||
$maxtime = cache::now() - $this->ttl;
|
||||
$count = 0;
|
||||
for ($value = reset($this->store); $value !== false; $value = next($this->store)) {
|
||||
if ($value[1] >= $maxtime) {
|
||||
// We know that elements are sorted by ttl so no need to continue.
|
||||
break;
|
||||
}
|
||||
$count++;
|
||||
}
|
||||
if ($count) {
|
||||
// Remove first $count elements as they are expired.
|
||||
$this->store = array_slice($this->store, $count, null, true);
|
||||
if ($this->maxsize !== false) {
|
||||
$this->storecount -= $count;
|
||||
}
|
||||
}
|
||||
return $count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds all of the keys being stored in the cache store instance.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function find_all() {
|
||||
$this->check_ttl();
|
||||
return array_keys($this->store);
|
||||
}
|
||||
|
||||
|
@ -502,6 +576,7 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
|
|||
* Finds all of the keys whose keys start with the given prefix.
|
||||
*
|
||||
* @param string $prefix
|
||||
* @return array An array of keys.
|
||||
*/
|
||||
public function find_by_prefix($prefix) {
|
||||
$return = array();
|
||||
|
@ -512,4 +587,12 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
|
|||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* This store supports native TTL handling.
|
||||
* @return bool
|
||||
*/
|
||||
public function store_supports_native_ttl() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
150
cache/stores/session/tests/session_test.php
vendored
150
cache/stores/session/tests/session_test.php
vendored
|
@ -49,60 +49,66 @@ class cachestore_session_test extends cachestore_tests {
|
|||
* Test the maxsize option.
|
||||
*/
|
||||
public function test_maxsize() {
|
||||
$defid = 'phpunit/testmaxsize';
|
||||
$config = cache_config_phpunittest::instance();
|
||||
$config->phpunit_add_definition($defid, array(
|
||||
$config->phpunit_add_definition('phpunit/one', array(
|
||||
'mode' => cache_store::MODE_SESSION,
|
||||
'component' => 'phpunit',
|
||||
'area' => 'testmaxsize',
|
||||
'area' => 'one',
|
||||
'maxsize' => 3
|
||||
));
|
||||
$definition = cache_definition::load($defid, $config->get_definition_by_id($defid));
|
||||
$instance = cachestore_session::initialise_test_instance($definition);
|
||||
|
||||
$this->assertTrue($instance->set('key1', 'value1'));
|
||||
$this->assertTrue($instance->set('key2', 'value2'));
|
||||
$this->assertTrue($instance->set('key3', 'value3'));
|
||||
$config->phpunit_add_definition('phpunit/two', array(
|
||||
'mode' => cache_store::MODE_SESSION,
|
||||
'component' => 'phpunit',
|
||||
'area' => 'two',
|
||||
'maxsize' => 3
|
||||
));
|
||||
|
||||
$this->assertTrue($instance->has('key1'));
|
||||
$this->assertTrue($instance->has('key2'));
|
||||
$this->assertTrue($instance->has('key3'));
|
||||
$cacheone = cache::make('phpunit', 'one');
|
||||
|
||||
$this->assertTrue($instance->set('key4', 'value4'));
|
||||
$this->assertTrue($instance->set('key5', 'value5'));
|
||||
$this->assertTrue($cacheone->set('key1', 'value1'));
|
||||
$this->assertTrue($cacheone->set('key2', 'value2'));
|
||||
$this->assertTrue($cacheone->set('key3', 'value3'));
|
||||
|
||||
$this->assertFalse($instance->has('key1'));
|
||||
$this->assertFalse($instance->has('key2'));
|
||||
$this->assertTrue($instance->has('key3'));
|
||||
$this->assertTrue($instance->has('key4'));
|
||||
$this->assertTrue($instance->has('key5'));
|
||||
$this->assertTrue($cacheone->has('key1'));
|
||||
$this->assertTrue($cacheone->has('key2'));
|
||||
$this->assertTrue($cacheone->has('key3'));
|
||||
|
||||
$this->assertFalse($instance->get('key1'));
|
||||
$this->assertFalse($instance->get('key2'));
|
||||
$this->assertEquals('value3', $instance->get('key3'));
|
||||
$this->assertEquals('value4', $instance->get('key4'));
|
||||
$this->assertEquals('value5', $instance->get('key5'));
|
||||
$this->assertTrue($cacheone->set('key4', 'value4'));
|
||||
$this->assertTrue($cacheone->set('key5', 'value5'));
|
||||
|
||||
$this->assertFalse($cacheone->has('key1'));
|
||||
$this->assertFalse($cacheone->has('key2'));
|
||||
$this->assertTrue($cacheone->has('key3'));
|
||||
$this->assertTrue($cacheone->has('key4'));
|
||||
$this->assertTrue($cacheone->has('key5'));
|
||||
|
||||
$this->assertFalse($cacheone->get('key1'));
|
||||
$this->assertFalse($cacheone->get('key2'));
|
||||
$this->assertEquals('value3', $cacheone->get('key3'));
|
||||
$this->assertEquals('value4', $cacheone->get('key4'));
|
||||
$this->assertEquals('value5', $cacheone->get('key5'));
|
||||
|
||||
// Test adding one more.
|
||||
$this->assertTrue($instance->set('key6', 'value6'));
|
||||
$this->assertFalse($instance->get('key3'));
|
||||
$this->assertTrue($cacheone->set('key6', 'value6'));
|
||||
$this->assertFalse($cacheone->get('key3'));
|
||||
|
||||
// Test reducing and then adding to make sure we don't lost one.
|
||||
$this->assertTrue($instance->delete('key6'));
|
||||
$this->assertTrue($instance->set('key7', 'value7'));
|
||||
$this->assertEquals('value4', $instance->get('key4'));
|
||||
$this->assertTrue($cacheone->delete('key6'));
|
||||
$this->assertTrue($cacheone->set('key7', 'value7'));
|
||||
$this->assertEquals('value4', $cacheone->get('key4'));
|
||||
|
||||
// Set the same key three times to make sure it doesn't count overrides.
|
||||
for ($i = 0; $i < 3; $i++) {
|
||||
$this->assertTrue($instance->set('key8', 'value8'));
|
||||
$this->assertTrue($cacheone->set('key8', 'value8'));
|
||||
}
|
||||
$this->assertEquals('value7', $instance->get('key7'), 'Overrides are incorrectly incrementing size');
|
||||
$this->assertEquals('value7', $cacheone->get('key7'), 'Overrides are incorrectly incrementing size');
|
||||
|
||||
// Test adding many.
|
||||
$this->assertEquals(3, $instance->set_many(array(
|
||||
array('key' => 'keyA', 'value' => 'valueA'),
|
||||
array('key' => 'keyB', 'value' => 'valueB'),
|
||||
array('key' => 'keyC', 'value' => 'valueC')
|
||||
$this->assertEquals(3, $cacheone->set_many(array(
|
||||
'keyA' => 'valueA',
|
||||
'keyB' => 'valueB',
|
||||
'keyC' => 'valueC'
|
||||
)));
|
||||
$this->assertEquals(array(
|
||||
'key4' => false,
|
||||
|
@ -112,8 +118,82 @@ class cachestore_session_test extends cachestore_tests {
|
|||
'keyA' => 'valueA',
|
||||
'keyB' => 'valueB',
|
||||
'keyC' => 'valueC'
|
||||
), $instance->get_many(array(
|
||||
), $cacheone->get_many(array(
|
||||
'key4', 'key5', 'key6', 'key7', 'keyA', 'keyB', 'keyC'
|
||||
)));
|
||||
|
||||
$cachetwo = cache::make('phpunit', 'two');
|
||||
|
||||
// Test adding many.
|
||||
$this->assertEquals(3, $cacheone->set_many(array(
|
||||
'keyA' => 'valueA',
|
||||
'keyB' => 'valueB',
|
||||
'keyC' => 'valueC'
|
||||
)));
|
||||
|
||||
$this->assertEquals(3, $cachetwo->set_many(array(
|
||||
'key1' => 'value1',
|
||||
'key2' => 'value2',
|
||||
'key3' => 'value3'
|
||||
)));
|
||||
|
||||
$this->assertEquals(array(
|
||||
'keyA' => 'valueA',
|
||||
'keyB' => 'valueB',
|
||||
'keyC' => 'valueC'
|
||||
), $cacheone->get_many(array(
|
||||
'keyA', 'keyB', 'keyC'
|
||||
)));
|
||||
|
||||
$this->assertEquals(array(
|
||||
'key1' => 'value1',
|
||||
'key2' => 'value2',
|
||||
'key3' => 'value3'
|
||||
), $cachetwo->get_many(array(
|
||||
'key1', 'key2', 'key3'
|
||||
)));
|
||||
|
||||
// Test that that cache deletes element that was least recently accessed.
|
||||
$this->assertEquals('valueA', $cacheone->get('keyA'));
|
||||
$cacheone->set('keyD', 'valueD');
|
||||
$this->assertEquals('valueA', $cacheone->get('keyA'));
|
||||
$this->assertFalse($cacheone->get('keyB'));
|
||||
$this->assertEquals(array('keyD' => 'valueD', 'keyC' => 'valueC'), $cacheone->get_many(array('keyD', 'keyC')));
|
||||
$cacheone->set('keyE', 'valueE');
|
||||
$this->assertFalse($cacheone->get('keyB'));
|
||||
$this->assertFalse($cacheone->get('keyA'));
|
||||
$this->assertEquals(array('keyA' => false, 'keyE' => 'valueE', 'keyD' => 'valueD', 'keyC' => 'valueC'),
|
||||
$cacheone->get_many(array('keyA', 'keyE', 'keyD', 'keyC')));
|
||||
// Overwrite keyE (moves it to the end of array), and set keyF.
|
||||
$cacheone->set_many(array('keyE' => 'valueE', 'keyF' => 'valueF'));
|
||||
$this->assertEquals(array('keyC' => 'valueC', 'keyE' => 'valueE', 'keyD' => false, 'keyF' => 'valueF'),
|
||||
$cacheone->get_many(array('keyC', 'keyE', 'keyD', 'keyF')));
|
||||
}
|
||||
|
||||
public function test_ttl() {
|
||||
$config = cache_config_phpunittest::instance();
|
||||
$config->phpunit_add_definition('phpunit/three', array(
|
||||
'mode' => cache_store::MODE_SESSION,
|
||||
'component' => 'phpunit',
|
||||
'area' => 'three',
|
||||
'maxsize' => 3,
|
||||
'ttl' => 3
|
||||
));
|
||||
|
||||
$cachethree = cache::make('phpunit', 'three');
|
||||
|
||||
// Make sure that when cache with ttl is full the elements that were added first are deleted first regardless of access time.
|
||||
$cachethree->set('key1', 'value1');
|
||||
$cachethree->set('key2', 'value2');
|
||||
$cachethree->set('key3', 'value3');
|
||||
$cachethree->set('key4', 'value4');
|
||||
$this->assertFalse($cachethree->get('key1'));
|
||||
$this->assertEquals('value4', $cachethree->get('key4'));
|
||||
$cachethree->set('key5', 'value5');
|
||||
$this->assertFalse($cachethree->get('key2'));
|
||||
$this->assertEquals('value4', $cachethree->get('key4'));
|
||||
$cachethree->set_many(array('key6' => 'value6', 'key7' => 'value7'));
|
||||
$this->assertEquals(array('key3' => false, 'key4' => false, 'key5' => 'value5', 'key6' => 'value6', 'key7' => 'value7'),
|
||||
$cachethree->get_many(array('key3', 'key4', 'key5', 'key6', 'key7')));
|
||||
}
|
||||
}
|
93
cache/tests/cache_test.php
vendored
93
cache/tests/cache_test.php
vendored
|
@ -1126,9 +1126,9 @@ class core_cache_testcase extends advanced_testcase {
|
|||
}
|
||||
|
||||
/**
|
||||
* Test that multiple loaders work ok.
|
||||
* Test that multiple application loaders work ok.
|
||||
*/
|
||||
public function test_multiple_loaders() {
|
||||
public function test_multiple_application_loaders() {
|
||||
$instance = cache_config_phpunittest::instance(true);
|
||||
$instance->phpunit_add_file_store('phpunittest1');
|
||||
$instance->phpunit_add_file_store('phpunittest2');
|
||||
|
@ -1173,6 +1173,93 @@ class core_cache_testcase extends advanced_testcase {
|
|||
$this->assertFalse($result['a']);
|
||||
$this->assertEquals('B', $result['b']);
|
||||
$this->assertFalse($result['c']);
|
||||
|
||||
// Test non-recursive deletes.
|
||||
$this->assertTrue($cache->set('test', 'test'));
|
||||
$this->assertSame('test', $cache->get('test'));
|
||||
$this->assertTrue($cache->delete('test', false));
|
||||
// We should still have it on a deeper loader.
|
||||
$this->assertSame('test', $cache->get('test'));
|
||||
// Test non-recusive with many functions.
|
||||
$this->assertSame(3, $cache->set_many(array(
|
||||
'one' => 'one',
|
||||
'two' => 'two',
|
||||
'three' => 'three'
|
||||
)));
|
||||
$this->assertSame('one', $cache->get('one'));
|
||||
$this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
|
||||
$this->assertSame(3, $cache->delete_many(array('one', 'two', 'three'), false));
|
||||
$this->assertSame('one', $cache->get('one'));
|
||||
$this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that multiple application loaders work ok.
|
||||
*/
|
||||
public function test_multiple_session_loaders() {
|
||||
/* @var cache_config_phpunittest $instance */
|
||||
$instance = cache_config_phpunittest::instance(true);
|
||||
$instance->phpunit_add_session_store('phpunittest1');
|
||||
$instance->phpunit_add_session_store('phpunittest2');
|
||||
$instance->phpunit_add_definition('phpunit/multi_loader', array(
|
||||
'mode' => cache_store::MODE_SESSION,
|
||||
'component' => 'phpunit',
|
||||
'area' => 'multi_loader'
|
||||
));
|
||||
$instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest1', 3);
|
||||
$instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest2', 2);
|
||||
|
||||
$cache = cache::make('phpunit', 'multi_loader');
|
||||
$this->assertInstanceOf('cache_session', $cache);
|
||||
$this->assertFalse($cache->get('test'));
|
||||
$this->assertTrue($cache->set('test', 'test'));
|
||||
$this->assertEquals('test', $cache->get('test'));
|
||||
$this->assertTrue($cache->delete('test'));
|
||||
$this->assertFalse($cache->get('test'));
|
||||
$this->assertTrue($cache->set('test', 'test'));
|
||||
$this->assertTrue($cache->purge());
|
||||
$this->assertFalse($cache->get('test'));
|
||||
|
||||
// Test the many commands.
|
||||
$this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
|
||||
$result = $cache->get_many(array('a', 'b', 'c'));
|
||||
$this->assertInternalType('array', $result);
|
||||
$this->assertCount(3, $result);
|
||||
$this->assertArrayHasKey('a', $result);
|
||||
$this->assertArrayHasKey('b', $result);
|
||||
$this->assertArrayHasKey('c', $result);
|
||||
$this->assertEquals('A', $result['a']);
|
||||
$this->assertEquals('B', $result['b']);
|
||||
$this->assertEquals('C', $result['c']);
|
||||
$this->assertEquals($result, $cache->get_many(array('a', 'b', 'c')));
|
||||
$this->assertEquals(2, $cache->delete_many(array('a', 'c')));
|
||||
$result = $cache->get_many(array('a', 'b', 'c'));
|
||||
$this->assertInternalType('array', $result);
|
||||
$this->assertCount(3, $result);
|
||||
$this->assertArrayHasKey('a', $result);
|
||||
$this->assertArrayHasKey('b', $result);
|
||||
$this->assertArrayHasKey('c', $result);
|
||||
$this->assertFalse($result['a']);
|
||||
$this->assertEquals('B', $result['b']);
|
||||
$this->assertFalse($result['c']);
|
||||
|
||||
// Test non-recursive deletes.
|
||||
$this->assertTrue($cache->set('test', 'test'));
|
||||
$this->assertSame('test', $cache->get('test'));
|
||||
$this->assertTrue($cache->delete('test', false));
|
||||
// We should still have it on a deeper loader.
|
||||
$this->assertSame('test', $cache->get('test'));
|
||||
// Test non-recusive with many functions.
|
||||
$this->assertSame(3, $cache->set_many(array(
|
||||
'one' => 'one',
|
||||
'two' => 'two',
|
||||
'three' => 'three'
|
||||
)));
|
||||
$this->assertSame('one', $cache->get('one'));
|
||||
$this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
|
||||
$this->assertSame(3, $cache->delete_many(array('one', 'two', 'three'), false));
|
||||
$this->assertSame('one', $cache->get('one'));
|
||||
$this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1448,4 +1535,4 @@ class core_cache_testcase extends advanced_testcase {
|
|||
$this->assertInstanceOf('cache_request', $cache);
|
||||
$this->assertArrayHasKey('cache_is_searchable', $cache->phpunit_get_store_implements());
|
||||
}
|
||||
}
|
||||
}
|
18
cache/tests/fixtures/lib.php
vendored
18
cache/tests/fixtures/lib.php
vendored
|
@ -94,6 +94,24 @@ class cache_config_phpunittest extends cache_config_writer {
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Forcefully adds a session store.
|
||||
*
|
||||
* @param string $name
|
||||
*/
|
||||
public function phpunit_add_session_store($name) {
|
||||
$this->configstores[$name] = array(
|
||||
'name' => $name,
|
||||
'plugin' => 'session',
|
||||
'configuration' => array(),
|
||||
'features' => 14,
|
||||
'modes' => 2,
|
||||
'default' => true,
|
||||
'class' => 'cachestore_session',
|
||||
'lock' => 'cachelock_file_default',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Forcefully injects a definition => store mapping.
|
||||
*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue