Merge branch 'MDL-60630-master' of git://github.com/andrewnicols/moodle

This commit is contained in:
Eloy Lafuente (stronk7) 2018-09-12 23:14:54 +02:00
commit 0e05bac861
11 changed files with 12 additions and 1268 deletions

View File

@ -1,106 +0,0 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* The library file for the memcache cache store.
*
* This file is part of the memcache cache store, it contains the API for interacting with an instance of the store.
*
* @package cachestore_memcache
* @copyright 2012 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot.'/cache/forms.php');
/**
* Form for adding a memcache instance.
*
* @copyright 2012 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cachestore_memcache_addinstance_form extends cachestore_addinstance_form {
/**
* Add the desired form elements.
*/
protected function configuration_definition() {
$form = $this->_form;
$form->addElement('textarea', 'servers', get_string('servers', 'cachestore_memcache'), array('cols' => 75, 'rows' => 5));
$form->addHelpButton('servers', 'servers', 'cachestore_memcache');
$form->addRule('servers', get_string('required'), 'required');
$form->setType('servers', PARAM_RAW);
$form->addElement('text', 'prefix', get_string('prefix', 'cachestore_memcache'),
array('maxlength' => 5, 'size' => 5));
$form->addHelpButton('prefix', 'prefix', 'cachestore_memcache');
$form->setType('prefix', PARAM_TEXT); // We set to text but we have a rule to limit to alphanumext.
$form->setDefault('prefix', 'mdl_');
$form->addRule('prefix', get_string('prefixinvalid', 'cachestore_memcache'), 'regex', '#^[a-zA-Z0-9\-_]+$#');
$form->setForceLtr('prefix');
$form->addElement('header', 'clusteredheader', get_string('clustered', 'cachestore_memcache'));
$form->addElement('checkbox', 'clustered', get_string('clustered', 'cachestore_memcache'));
$form->setDefault('checkbox', false);
$form->addHelpButton('clustered', 'clustered', 'cachestore_memcache');
$form->addElement('textarea', 'setservers', get_string('setservers', 'cachestore_memcache'),
array('cols' => 75, 'rows' => 5));
$form->addHelpButton('setservers', 'setservers', 'cachestore_memcache');
$form->disabledIf('setservers', 'clustered');
$form->setType('setservers', PARAM_RAW);
}
/**
* Perform minimal validation on the settings form.
*
* @param array $data
* @param array $files
*/
public function validation($data, $files) {
$errors = parent::validation($data, $files);
if (isset($data['clustered']) && ($data['clustered'] == 1)) {
// Set servers is required with in cluster mode.
if (!isset($data['setservers'])) {
$errors['setservers'] = get_string('required');
} else {
$trimmed = trim($data['setservers']);
if (empty($trimmed)) {
$errors['setservers'] = get_string('required');
}
}
$validservers = false;
if (isset($data['servers'])) {
$servers = trim($data['servers']);
$servers = explode("\n", $servers);
if (count($servers) === 1) {
$validservers = true;
}
}
if (!$validservers) {
$errors['servers'] = get_string('serversclusterinvalid', 'cachestore_memcache');
}
}
return $errors;
}
}

View File

@ -1,87 +0,0 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Privacy Subsystem implementation for cachestore_memcache.
*
* @package cachestore_memcache
* @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace cachestore_memcache\privacy;
use core_privacy\local\metadata\collection;
use core_privacy\local\request\contextlist;
use core_privacy\local\request\approved_contextlist;
defined('MOODLE_INTERNAL') || die();
/**
* Privacy Subsystem for cachestore_memcache.
*
* @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements \core_privacy\local\metadata\provider, \core_privacy\local\request\plugin\provider {
/**
* Returns meta data about this system.
*
* @param collection $collection The initialised collection to add items to.
* @return collection A listing of user data stored through this system.
*/
public static function get_metadata(collection $collection) : collection {
$collection->add_external_location_link('memcache', [
'data' => 'privacy:metadata:memcache:data',
], 'privacy:metadata:memcache');
return $collection;
}
/**
* Get the list of contexts that contain user information for the specified user.
*
* @param int $userid The user to search.
* @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
*/
public static function get_contexts_for_userid(int $userid) : contextlist {
return new contextlist();
}
/**
* Export all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts to export information for.
*/
public static function export_user_data(approved_contextlist $contextlist) {
}
/**
* Delete all use data which matches the specified deletion_criteria.
*
* @param \context $context A user context.
*/
public static function delete_data_for_all_users_in_context(\context $context) {
}
/**
* Delete all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
*/
public static function delete_data_for_user(approved_contextlist $contextlist) {
}
}

View File

@ -1,72 +0,0 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* The library file for the memcache cache store.
*
* This file is part of the memcache cache store, it contains the API for interacting with an instance of the store.
*
* @package cachestore_memcache
* @copyright 2012 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$string['clustered'] = 'Enable clustered servers';
$string['clustered_help'] = 'This is used to allow read-one, set-multi functionality.
The intended use case is to create an improved store for load-balanced configurations. The store will fetch from one server (usually localhost), but set to many (all the servers in the load-balance pool). For caches with very high read to set ratios, this saves a significant amount of network overhead.
When this setting is enabled, the server listed above will be used for fetching.';
$string['clusteredheader'] = 'Split servers';
$string['pluginname'] = 'Memcache';
$string['prefix'] = 'Key prefix';
$string['prefix_help'] = 'This prefix is used for all key names on the memcache server.
* If you only have one Moodle instance using this server, you can leave this value default.
* Due to key length restrictions, a maximum of 5 characters is permitted.';
$string['prefixinvalid'] = 'Invalid prefix. You can only use a-z A-Z 0-9-_.';
$string['privacy:metadata:memcache'] = 'The Memcache cachestore plugin stores data briefly as part of its caching functionality. This data is stored on an Memcache server where data is regularly removed.';
$string['privacy:metadata:memcache:data'] = 'The various data stored in the cache';
$string['servers'] = 'Servers';
$string['servers_help'] = 'This sets the servers that should be utilised by this memcache adapter.
Servers should be defined one per line and consist of a server address and optionally a port and weight.
If no port is provided then the default port (11211) is used.
For example:
<pre>
server.url.com
ipaddress:port
servername:port:weight
</pre>
If *Enable clustered servers* is enabled below, there must be only one server listed here. This would usually be a name that always resolves to the local machine, like 127.0.0.1 or localhost.';
$string['serversclusterinvalid'] = 'Exactly one server is required when clustering is enabled.';
$string['setservers'] = 'Set Servers';
$string['setservers_help'] = 'This is the list of servers that will updated when data is modified in the cache. Generally the fully qualified name of each server in the pool.
It **must** include the server listed in *Servers* above, even if by a different hostname.
Servers should be defined one per line and consist of a server address and optionally a port.
If no port is provided then the default port (11211) is used.
For example:
<pre>
server.url.com
ipaddress:port
</pre>';
$string['sessionhandlerconflict'] = 'Warning: A memcache instance ({$a}) has being configured to use the same memcache server as sessions. Purging all caches will lead to sessions also being purged.';
$string['testservers'] = 'Test servers';
$string['testservers_desc'] = 'One or more connection strings for memcache servers to test against. If a test server has been specified then memcache performance can be tested using the cache performance page in the administration block.
As an example: 127.0.0.1:11211';

View File

@ -1,642 +0,0 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* The library file for the memcache cache store.
*
* This file is part of the memcache cache store, it contains the API for interacting with an instance of the store.
*
* @package cachestore_memcache
* @copyright 2012 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* The memcache store class.
*
* (Not to be confused with memcached store)
*
* Configuration options:
* servers: string: host:port:weight , ...
*
* @copyright 2012 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cachestore_memcache extends cache_store implements cache_is_configurable {
/**
* The name of the store
* @var store
*/
protected $name;
/**
* The memcache connection once established.
* @var Memcache
*/
protected $connection;
/**
* Key prefix for this memcache.
* @var string
*/
protected $prefix;
/**
* An array of servers to use in the connection args.
* @var array
*/
protected $servers = array();
/**
* An array of options used when establishing the connection.
* @var array
*/
protected $options = array();
/**
* Set to true when things are ready to be initialised.
* @var bool
*/
protected $isready = false;
/**
* Set to true once this store instance has been initialised.
* @var bool
*/
protected $isinitialised = false;
/**
* The cache definition this store was initialised for.
* @var cache_definition
*/
protected $definition;
/**
* Set to true when this store is clustered.
* @var bool
*/
protected $clustered = false;
/**
* Array of servers to set when in clustered mode.
* @var array
*/
protected $setservers = array();
/**
* The an array of memcache connections for the set servers, once established.
* @var array
*/
protected $setconnections = array();
/**
* If true data going in and out will be encoded.
* @var bool
*/
protected $encode = true;
/**
* Default prefix for key names.
* @var string
*/
const DEFAULT_PREFIX = 'mdl_';
/**
* Constructs the store instance.
*
* Noting that this function is not an initialisation. It is used to prepare the store for use.
* The store will be initialised when required and will be provided with a cache_definition at that time.
*
* @param string $name
* @param array $configuration
*/
public function __construct($name, array $configuration = array()) {
$this->name = $name;
if (!array_key_exists('servers', $configuration) || empty($configuration['servers'])) {
// Nothing configured.
return;
}
if (!is_array($configuration['servers'])) {
$configuration['servers'] = array($configuration['servers']);
}
foreach ($configuration['servers'] as $server) {
if (!is_array($server)) {
$server = explode(':', $server, 3);
}
if (!array_key_exists(1, $server)) {
$server[1] = 11211;
$server[2] = 100;
} else if (!array_key_exists(2, $server)) {
$server[2] = 100;
}
$this->servers[] = $server;
}
$this->clustered = array_key_exists('clustered', $configuration) ? (bool)$configuration['clustered'] : false;
if ($this->clustered) {
if (!array_key_exists('setservers', $configuration) || (count($configuration['setservers']) < 1)) {
// Can't setup clustering without set servers.
return;
}
if (count($this->servers) !== 1) {
// Can only setup cluster with exactly 1 get server.
return;
}
foreach ($configuration['setservers'] as $server) {
// We do not use weights (3rd part) on these servers.
if (!is_array($server)) {
$server = explode(':', $server, 3);
}
if (!array_key_exists(1, $server)) {
$server[1] = 11211;
}
$this->setservers[] = $server;
}
}
if (empty($configuration['prefix'])) {
$this->prefix = self::DEFAULT_PREFIX;
} else {
$this->prefix = $configuration['prefix'];
}
$this->connection = new Memcache;
foreach ($this->servers as $server) {
$this->connection->addServer($server[0], (int) $server[1], true, (int) $server[2]);
}
if ($this->clustered) {
foreach ($this->setservers as $setserver) {
// Since we will have a number of them with the same name, append server and port.
$connection = new Memcache;
$connection->addServer($setserver[0], $setserver[1]);
$this->setconnections[] = $connection;
}
}
// Test the connection to the pool of servers.
$this->isready = @$this->connection->set($this->parse_key('ping'), 'ping', MEMCACHE_COMPRESSED, 1);
}
/**
* Initialises the cache.
*
* Once this has been done the cache is all set to be used.
*
* @param cache_definition $definition
*/
public function initialise(cache_definition $definition) {
if ($this->is_initialised()) {
throw new coding_exception('This memcache instance has already been initialised.');
}
$this->definition = $definition;
$this->isinitialised = true;
$this->encode = self::require_encoding();
}
/**
* Tests if encoding is going to be required.
*
* Prior to memcache 3.0.3 scalar data types were not preserved.
* For earlier versions of the memcache extension we need to encode and decode scalar types
* to ensure that it is preserved.
*
* @param string $version The version to check, if null it is fetched from PHP.
* @return bool
*/
public static function require_encoding($version = null) {
if (!$version) {
$version = phpversion('memcache');
}
return (version_compare($version, '3.0.3', '<'));
}
/**
* Returns true once this instance has been initialised.
*
* @return bool
*/
public function is_initialised() {
return ($this->isinitialised);
}
/**
* Returns true if this store instance is ready to be used.
* @return bool
*/
public function is_ready() {
return $this->isready;
}
/**
* Returns true if the store requirements are met.
*
* @return bool
*/
public static function are_requirements_met() {
return class_exists('Memcache');
}
/**
* Returns true if the given mode is supported by this store.
*
* @param int $mode One of cache_store::MODE_*
* @return bool
*/
public static function is_supported_mode($mode) {
return ($mode === self::MODE_APPLICATION || $mode === self::MODE_SESSION);
}
/**
* Returns the supported features as a combined int.
*
* @param array $configuration
* @return int
*/
public static function get_supported_features(array $configuration = array()) {
return self::SUPPORTS_NATIVE_TTL + self::DEREFERENCES_OBJECTS;
}
/**
* Returns false as this store does not support multiple identifiers.
* (This optional function is a performance optimisation; it must be
* consistent with the value from get_supported_features.)
*
* @return bool False
*/
public function supports_multiple_identifiers() {
return false;
}
/**
* Returns the supported modes as a combined int.
*
* @param array $configuration
* @return int
*/
public static function get_supported_modes(array $configuration = array()) {
return self::MODE_APPLICATION;
}
/**
* Parses the given key to make it work for this memcache backend.
*
* @param string $key The raw key.
* @return string The resulting key.
*/
protected function parse_key($key) {
if (strlen($key) > 245) {
$key = '_sha1_'.sha1($key);
}
$key = $this->prefix . $key;
return $key;
}
/**
* Retrieves an item from the cache store given its key.
*
* @param string $key The key to retrieve
* @return mixed The data that was associated with the key, or false if the key did not exist.
*/
public function get($key) {
$result = $this->connection->get($this->parse_key($key));
if ($this->encode && $result !== false) {
return @unserialize($result);
}
return $result;
}
/**
* Retrieves several items from the cache store in a single transaction.
*
* If not all of the items are available in the cache then the data value for those that are missing will be set to false.
*
* @param array $keys The array of keys to retrieve
* @return array An array of items from the cache. There will be an item for each key, those that were not in the store will
* be set to false.
*/
public function get_many($keys) {
$mkeys = array();
foreach ($keys as $key) {
$mkeys[$key] = $this->parse_key($key);
}
$result = $this->connection->get($mkeys);
if (!is_array($result)) {
$result = array();
}
$return = array();
foreach ($mkeys as $key => $mkey) {
if (!array_key_exists($mkey, $result)) {
$return[$key] = false;
} else {
$return[$key] = $result[$mkey];
if ($this->encode && $return[$key] !== false) {
$return[$key] = @unserialize($return[$key]);
}
}
}
return $return;
}
/**
* Sets an item in the cache given its key and data value.
*
* @param string $key The key to use.
* @param mixed $data The data to set.
* @return bool True if the operation was a success false otherwise.
*/
public function set($key, $data) {
if ($this->encode) {
// We must serialise this data.
$data = serialize($data);
}
if ($this->clustered) {
$status = true;
foreach ($this->setconnections as $connection) {
$status = $connection->set($this->parse_key($key), $data, MEMCACHE_COMPRESSED, $this->definition->get_ttl())
&& $status;
}
return $status;
}
return $this->connection->set($this->parse_key($key), $data, MEMCACHE_COMPRESSED, $this->definition->get_ttl());
}
/**
* Sets many items in the cache in a single transaction.
*
* @param array $keyvaluearray An array of key value pairs. Each item in the array will be an associative array with two
* keys, 'key' and 'value'.
* @return int The number of items successfully set. It is up to the developer to check this matches the number of items
* sent ... if they care that is.
*/
public function set_many(array $keyvaluearray) {
$count = 0;
foreach ($keyvaluearray as $pair) {
if ($this->set($pair['key'], $pair['value'])) {
$count++;
}
}
return $count;
}
/**
* Deletes an item from the cache store.
*
* @param string $key The key to delete.
* @return bool Returns true if the operation was a success, false otherwise.
*/
public function delete($key) {
if ($this->clustered) {
$status = true;
foreach ($this->setconnections as $connection) {
$status = $connection->delete($this->parse_key($key)) && $status;
}
return $status;
}
return $this->connection->delete($this->parse_key($key));
}
/**
* Deletes several keys from the cache in a single action.
*
* @param array $keys The keys to delete
* @return int The number of items successfully deleted.
*/
public function delete_many(array $keys) {
$count = 0;
foreach ($keys as $key) {
if ($this->delete($key)) {
$count++;
}
}
return $count;
}
/**
* Purges the cache deleting all items within it.
*
* @return boolean True on success. False otherwise.
*/
public function purge() {
if ($this->isready) {
if ($this->clustered) {
foreach ($this->setconnections as $connection) {
$connection->flush();
}
} else {
$this->connection->flush();
}
}
return true;
}
/**
* Given the data from the add instance form this function creates a configuration array.
*
* @param stdClass $data
* @return array
*/
public static function config_get_configuration_array($data) {
$lines = explode("\n", $data->servers);
$servers = array();
foreach ($lines as $line) {
// Trim surrounding colons and default whitespace.
$line = trim(trim($line), ":");
// Skip blank lines.
if ($line === '') {
continue;
}
$servers[] = explode(':', $line, 3);
}
$clustered = false;
$setservers = array();
if (isset($data->clustered)) {
$clustered = true;
$lines = explode("\n", $data->setservers);
foreach ($lines as $line) {
// Trim surrounding colons and default whitespace.
$line = trim(trim($line), ":");
if ($line === '') {
continue;
}
$setserver = explode(':', $line, 3);
// We don't use weights, so display a debug message.
if (count($setserver) > 2) {
debugging('Memcache Set Server '.$setserver[0].' has too many parameters.');
}
$setservers[] = $setserver;
}
}
return array(
'servers' => $servers,
'prefix' => $data->prefix,
'clustered' => $clustered,
'setservers' => $setservers
);
}
/**
* Allows the cache store to set its data against the edit form before it is shown to the user.
*
* @param moodleform $editform
* @param array $config
*/
public static function config_set_edit_form_data(moodleform $editform, array $config) {
$data = array();
if (!empty($config['servers'])) {
$servers = array();
foreach ($config['servers'] as $server) {
$servers[] = join(":", $server);
}
$data['servers'] = join("\n", $servers);
}
if (!empty($config['prefix'])) {
$data['prefix'] = $config['prefix'];
} else {
$data['prefix'] = self::DEFAULT_PREFIX;
}
if (isset($config['clustered'])) {
$data['clustered'] = (bool)$config['clustered'];
}
if (!empty($config['setservers'])) {
$servers = array();
foreach ($config['setservers'] as $server) {
$servers[] = join(":", $server);
}
$data['setservers'] = join("\n", $servers);
}
$editform->set_data($data);
}
/**
* Performs any necessary clean up when the store instance is being deleted.
*/
public function instance_deleted() {
if ($this->connection) {
$connection = $this->connection;
} else {
$connection = new Memcache;
foreach ($this->servers as $server) {
$connection->addServer($server[0], $server[1], true, $server[2]);
}
}
@$connection->flush();
unset($connection);
unset($this->connection);
}
/**
* Generates an instance of the cache store that can be used for testing.
*
* @param cache_definition $definition
* @return cachestore_memcache|false
*/
public static function initialise_test_instance(cache_definition $definition) {
if (!self::are_requirements_met()) {
return false;
}
$config = get_config('cachestore_memcache');
if (empty($config->testservers)) {
return false;
}
$configuration = array();
$configuration['servers'] = explode("\n", $config->testservers);
if (!empty($config->testclustered)) {
$configuration['clustered'] = $config->testclustered;
}
if (!empty($config->testsetservers)) {
$configuration['setservers'] = explode("\n", $config->testsetservers);
}
$store = new cachestore_memcache('Test memcache', $configuration);
if ($store->is_ready()) {
$store->initialise($definition);
}
return $store;
}
/**
* Generates the appropriate configuration required for unit testing.
*
* @return array Array of unit test configuration data to be used by initialise().
*/
public static function unit_test_configuration() {
// If the configuration is not defined correctly, return only the configuration know about.
if (!defined('TEST_CACHESTORE_MEMCACHE_TESTSERVERS')) {
return [];
}
return ['servers' => explode("\n", TEST_CACHESTORE_MEMCACHE_TESTSERVERS)];
}
/**
* Returns the name of this instance.
* @return string
*/
public function my_name() {
return $this->name;
}
/**
* Used to notify of configuration conflicts.
*
* The warnings returned here will be displayed on the cache configuration screen.
*
* @return string[] Returns an array of warnings (strings)
*/
public function get_warnings() {
global $CFG;
$warnings = array();
if (isset($CFG->session_memcached_save_path) && count($this->servers)) {
$bits = explode(':', $CFG->session_memcached_save_path, 3);
$host = array_shift($bits);
$port = (count($bits)) ? array_shift($bits) : '11211';
foreach ($this->servers as $server) {
if ($server[0] === $host && $server[1] == $port) {
$warnings[] = get_string('sessionhandlerconflict', 'cachestore_memcache', $this->my_name());
break;
}
}
}
return $warnings;
}
/**
* Returns true if this cache store instance is both suitable for testing, and ready for testing.
*
* Cache stores that support being used as the default store for unit and acceptance testing should
* override this function and return true if there requirements have been met.
*
* @return bool
*/
public static function ready_to_be_used_for_testing() {
return defined('TEST_CACHESTORE_MEMCACHE_TESTSERVERS');
}
}

View File

@ -1,33 +0,0 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* The settings for the memcache store.
*
* This file is part of the memcache cache store, it contains the API for interacting with an instance of the store.
*
* @package cachestore_memcache
* @copyright 2012 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
$settings->add(new admin_setting_configtextarea(
'cachestore_memcache/testservers',
new lang_string('testservers', 'cachestore_memcache'),
new lang_string('testservers_desc', 'cachestore_memcache'),
'', PARAM_RAW, 60, 3));

View File

@ -1,295 +0,0 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Memcache unit tests.
*
* If you wish to use these unit tests all you need to do is add the following definition to
* your config.php file.
*
* define('TEST_CACHESTORE_MEMCACHE_TESTSERVERS', '127.0.0.1:11211');
*
* @package cachestore_memcache
* @copyright 2013 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
// Include the necessary evils.
global $CFG;
require_once($CFG->dirroot.'/cache/tests/fixtures/stores.php');
require_once($CFG->dirroot.'/cache/stores/memcache/lib.php');
/**
* Memcache unit test class.
*
* @package cachestore_memcache
* @copyright 2013 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cachestore_memcache_test extends cachestore_tests {
/**
* Returns the memcache class name
* @return string
*/
protected function get_class_name() {
return 'cachestore_memcache';
}
/**
* Tests the valid keys to ensure they work.
*/
public function test_valid_keys() {
$this->resetAfterTest(true);
$definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_memcache', 'phpunit_test');
$instance = new cachestore_memcache('Memcache Test', cachestore_memcache::unit_test_configuration());
if (!$instance->is_ready()) {
// Something prevented memcache store to be inited (extension, TEST_CACHESTORE_MEMCACHE_TESTSERVERS...).
$this->markTestSkipped();
}
$instance->initialise($definition);
$keys = array(
// Alphanumeric.
'abc', 'ABC', '123', 'aB1', '1aB',
// Hyphens.
'a-1', '1-a', '-a1', 'a1-',
// Underscores.
'a_1', '1_a', '_a1', 'a1_'
);
// Set some keys.
foreach ($keys as $key) {
$this->assertTrue($instance->set($key, $key), "Failed to set key `$key`");
}
// Get some keys.
foreach ($keys as $key) {
$this->assertEquals($key, $instance->get($key), "Failed to get key `$key`");
}
// Try get many.
$values = $instance->get_many($keys);
foreach ($values as $key => $value) {
$this->assertEquals($key, $value);
}
// Reset a key.
$this->assertTrue($instance->set($keys[0], 'New'), "Failed to reset key `$key`");
$this->assertEquals('New', $instance->get($keys[0]), "Failed to get reset key `$key`");
// Delete and check that we can't retrieve.
foreach ($keys as $key) {
$this->assertTrue($instance->delete($key), "Failed to delete key `$key`");
$this->assertFalse($instance->get($key), "Retrieved deleted key `$key`");
}
// Try set many, and check that count is correct.
$many = array();
foreach ($keys as $key) {
$many[] = array('key' => $key, 'value' => $key);
}
$returncount = $instance->set_many($many);
$this->assertEquals(count($many), $returncount, 'Set many count didn\'t match');
// Check keys retrieved with get_many.
$values = $instance->get_many($keys);
foreach ($keys as $key) {
$this->assertTrue(isset($values[$key]), "Failed to get_many key `$key`");
$this->assertEquals($key, $values[$key], "Failed to match get_many key `$key`");
}
// Delete many, make sure count matches.
$returncount = $instance->delete_many($keys);
$this->assertEquals(count($many), $returncount, 'Delete many count didn\'t match');
// Check that each key was deleted.
foreach ($keys as $key) {
$this->assertFalse($instance->get($key), "Retrieved many deleted key `$key`");
}
// Set the keys again.
$returncount = $instance->set_many($many);
$this->assertEquals(count($many), $returncount, 'Set many count didn\'t match');
// Purge.
$this->assertTrue($instance->purge(), 'Failure to purge');
// Delete and check that we can't retrieve.
foreach ($keys as $key) {
$this->assertFalse($instance->get($key), "Retrieved purged key `$key`");
}
}
/**
* Tests the clustering feature.
*/
public function test_clustered() {
$this->resetAfterTest(true);
if (!defined('TEST_CACHESTORE_MEMCACHE_TESTSERVERS')) {
$this->markTestSkipped();
}
$testservers = explode("\n", trim(TEST_CACHESTORE_MEMCACHE_TESTSERVERS));
if (count($testservers) < 2) {
$this->markTestSkipped();
}
// User the first server as our primary.
set_config('testservers', $testservers[0], 'cachestore_memcache');
set_config('testsetservers', TEST_CACHESTORE_MEMCACHE_TESTSERVERS, 'cachestore_memcache');
set_config('testclustered', true, 'cachestore_memcache');
// First and instance that we can use to test the second server.
$definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_memcache', 'phpunit_test');
$instance = cachestore_memcache::initialise_test_instance($definition);
if (!$instance) {
$this->markTestSkipped();
}
// Now we are going to setup a connection to each independent server.
set_config('testclustered', false, 'cachestore_memcache');
set_config('testsetservers', '', 'cachestore_memcache');
$checkinstances = array();
foreach ($testservers as $testserver) {
set_config('testservers', $testserver, 'cachestore_memcache');
$checkinstance = cachestore_memcache::initialise_test_instance($definition);
if (!$checkinstance) {
$this->markTestSkipped();
}
$checkinstances[] = $checkinstance;
}
$keys = array(
// Alphanumeric.
'abc', 'ABC', '123', 'aB1', '1aB',
// Hyphens.
'a-1', '1-a', '-a1', 'a1-',
// Underscores.
'a_1', '1_a', '_a1', 'a1_'
);
// Set each key.
foreach ($keys as $key) {
$this->assertTrue($instance->set($key, $key), "Failed to set key `$key`");
}
// Check each key.
foreach ($keys as $key) {
$this->assertEquals($key, $instance->get($key), "Failed to get key `$key`");
foreach ($checkinstances as $id => $checkinstance) {
$this->assertEquals($key, $checkinstance->get($key), "Failed to get key `$key` from server $id");
}
}
// Reset a key.
$this->assertTrue($instance->set($keys[0], 'New'), "Failed to reset key `$key`");
$this->assertEquals('New', $instance->get($keys[0]), "Failed to get reset key `$key`");
foreach ($checkinstances as $id => $checkinstance) {
$this->assertEquals('New', $checkinstance->get($keys[0]), "Failed to get reset key `$key` from server $id");
}
// Delete and check that we can't retrieve.
foreach ($keys as $key) {
$this->assertTrue($instance->delete($key), "Failed to delete key `$key`");
$this->assertFalse($instance->get($key), "Retrieved deleted key `$key`");
foreach ($checkinstances as $id => $checkinstance) {
$this->assertFalse($checkinstance->get($key), "Retrieved deleted key `$key` from server $id");
}
}
// Try set many, and check that count is correct.
$many = array();
foreach ($keys as $key) {
$many[] = array('key' => $key, 'value' => $key);
}
$returncount = $instance->set_many($many);
$this->assertEquals(count($many), $returncount, 'Set many count didn\'t match');
// Check keys retrieved with get_many.
$values = $instance->get_many($keys);
foreach ($keys as $key) {
$this->assertTrue(isset($values[$key]), "Failed to get_many key `$key`");
$this->assertEquals($key, $values[$key], "Failed to match get_many key `$key`");
}
foreach ($checkinstances as $id => $checkinstance) {
$values = $checkinstance->get_many($keys);
foreach ($keys as $key) {
$this->assertTrue(isset($values[$key]), "Failed to get_many key `$key` from server $id");
$this->assertEquals($key, $values[$key], "Failed to get_many key `$key` from server $id");
}
}
// Delete many, make sure count matches.
$returncount = $instance->delete_many($keys);
$this->assertEquals(count($many), $returncount, 'Delete many count didn\'t match');
// Check that each key was deleted.
foreach ($keys as $key) {
$this->assertFalse($instance->get($key), "Retrieved many deleted key `$key`");
foreach ($checkinstances as $id => $checkinstance) {
$this->assertFalse($checkinstance->get($key), "Retrieved many deleted key `$key` from server $id");
}
}
// Set the keys again.
$returncount = $instance->set_many($many);
$this->assertEquals(count($many), $returncount, 'Set many count didn\'t match');
// Purge.
$this->assertTrue($instance->purge(), 'Failure to purge');
// Delete and check that we can't retrieve.
foreach ($keys as $key) {
$this->assertFalse($instance->get($key), "Retrieved purged key `$key`");
foreach ($checkinstances as $id => $checkinstance) {
$this->assertFalse($checkinstance->get($key), "Retrieved purged key `$key` from server 2");
}
}
}
/**
* Test our checks for encoding.
*/
public function test_require_encoding() {
$this->assertTrue(cachestore_memcache::require_encoding('dev'));
$this->assertTrue(cachestore_memcache::require_encoding('1.0'));
$this->assertTrue(cachestore_memcache::require_encoding('1.0.0'));
$this->assertTrue(cachestore_memcache::require_encoding('2.0'));
$this->assertTrue(cachestore_memcache::require_encoding('2.0.8'));
$this->assertTrue(cachestore_memcache::require_encoding('2.2.8'));
$this->assertTrue(cachestore_memcache::require_encoding('3.0'));
$this->assertTrue(cachestore_memcache::require_encoding('3.0-dev'));
$this->assertTrue(cachestore_memcache::require_encoding('3.0.0'));
$this->assertTrue(cachestore_memcache::require_encoding('3.0.1'));
$this->assertTrue(cachestore_memcache::require_encoding('3.0.2-dev'));
$this->assertTrue(cachestore_memcache::require_encoding('3.0.2'));
$this->assertTrue(cachestore_memcache::require_encoding('3.0.3-dev'));
$this->assertFalse(cachestore_memcache::require_encoding('3.0.3'));
$this->assertFalse(cachestore_memcache::require_encoding('3.0.4'));
$this->assertFalse(cachestore_memcache::require_encoding('3.0.4-dev'));
$this->assertFalse(cachestore_memcache::require_encoding('3.0.8'));
$this->assertFalse(cachestore_memcache::require_encoding('3.1.0'));
$this->assertFalse(cachestore_memcache::require_encoding('3.1.2'));
}
}

View File

@ -1,31 +0,0 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Cache memcache store version information.
*
* Not to be confused with the memcached plugin.
*
* @package cachestore_memcache
* @copyright 2012 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
$plugin->version = 2018051400; // The current module version (Date: YYYYMMDDXX)
$plugin->requires = 2018050800; // Requires this Moodle version.
$plugin->component = 'cachestore_memcache'; // Full name of the plugin.

1
cache/upgrade.txt vendored
View File

@ -4,6 +4,7 @@ Information provided here is intended especially for developers.
=== 3.6 ===
* The `cache::now()` function now takes an optional boolean parameter to indicate that the cache should return a more
accurate time, generated by the PHP `microtime` function.
* The memcache store was removed as it is not compatible with PHP 7.0.
=== 3.3 ===
* Identifiers and invalidation events have been explictly been marked as incompatible and will

View File

@ -1648,6 +1648,7 @@ class core_plugin_manager {
'qformat' => array('blackboard', 'learnwise'),
'auth' => array('radius', 'fc', 'nntp', 'pam', 'pop3', 'imap'),
'block' => array('course_overview', 'messages'),
'cachestore' => array('memcache'),
'enrol' => array('authorize'),
'report' => array('search'),
'repository' => array('alfresco'),
@ -1732,7 +1733,7 @@ class core_plugin_manager {
),
'cachestore' => array(
'file', 'memcache', 'memcached', 'mongodb', 'session', 'static', 'apcu', 'redis'
'file', 'memcached', 'mongodb', 'session', 'static', 'apcu', 'redis'
),
'calendartype' => array(

View File

@ -2328,5 +2328,13 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2018083100.01);
}
if ($oldversion < 2018091200.00) {
if (!file_exists($CFG->dirroot.'/cache/stores/memcache/settings.php')) {
unset_all_config_for_plugin('cachestore_memcache');
}
upgrade_main_savepoint(true, 2018091200.00);
}
return true;
}

View File

@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2018090700.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2018091200.00; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.