Merge branch 'w47_MDL-36682_m24_mcache' of git://github.com/skodak/moodle

This commit is contained in:
Dan Poltawski 2012-11-19 14:50:48 +08:00
commit 98d11a8c83
9 changed files with 15 additions and 473 deletions

View file

@ -1473,6 +1473,19 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2012111200.01);
}
if ($oldversion < 2012111601.01) {
// Clea up after old shared memory caching support.
unset_config('cachetype');
unset_config('rcache');
unset_config('rcachettl');
unset_config('intcachemax');
unset_config('memcachedhosts');
unset_config('memcachedpconn');
// Main savepoint reached.
upgrade_main_savepoint(true, 2012111601.01);
}
return true;
}

View file

@ -1,189 +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/>.
/**
* This class abstracts eaccelerator/turckmmcache
* API to provide
*
* - get()
* - set()
* - delete()
* - getforfill()
* - releaseforfill()
*
* Note: do NOT store booleans here. For compatibility with
* memcached, a false value is indistinguisable from a
* "not found in cache" response.
*
* @package core
* @subpackage lib
* @copyright Martin Langhoff <martin@catalyst.net.nz>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
*
* @copyright Martin Langhoff <martin@catalyst.net.nz>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package moodlecore
*/
class eaccelerator {
/**
* @todo Document this function
*
* @global object
*/
function eaccelerator() {
global $CFG;
if ( function_exists('eaccelerator_get')) {
$this->mode = 'eaccelerator';
} elseif (function_exists('mmcache_get')) {
$this->mode = 'mmcache';
} else {
debugging("\$CFG->eaccelerator is set to true but the required functions are not available. You need to have either eaccelerator or turckmmcache extensions installed, compiled with the shmem keys option enabled.");
}
$this->prefix = $CFG->dbname .'|' . $CFG->prefix . '|';
}
/**
* The status of the eaccelerator, if it has been established
* this will return true
*
* @return bool
*/
function status() {
if (isset($this->mode)) {
return true;
}
return false;
}
/**
* @todo Document this function
*
* @param string $key
* @param string $value
* @param int $ttl
* @return mixed
*/
function set($key, $value, $ttl=0) {
$set = $this->mode . '_put';
$unlock = $this->mode . '_unlock';
// we may have acquired a lock via getforfill
// release if it exists
@$unlock($this->prefix . $key . '_forfill');
return $set($this->prefix . $key, serialize($value), $ttl);
}
/**
* @todo Document this function
*
* @param string $key
* @return string|bool String if success else false
*/
function get($key) {
$fn = $this->mode . '_get';
$rec = $fn($this->prefix . $key);
if (is_null($rec)) {
return false;
}
return unserialize($rec);
}
/**
* @todo Document this function
*
* @param string $key
* @return mixed
*/
function delete($key) {
$fn = $this->mode . '_rm';
return $fn($this->prefix . $key);
}
/**
* In the simple case, this function will
* get the cached value if available. If the entry
* is not cached, it will try to get an exclusive
* lock that announces that this process will
* populate the cache.
*
* If we fail to get the lock -- this means another
* process is doing it.
* so we wait (block) for a few microseconds while we wait for
* the cache to be filled or the lock to timeout.
*
* If you get a false from this call, you _must_
* populate the cache ASAP or indicate that
* you won't by calling releaseforfill().
*
* This technique forces serialisation and so helps deal
* with thundering herd scenarios where a lot of clients
* ask the for the same idempotent (and costly) operation.
* The implementation is based on suggestions in this message
* http://marc.theaimsgroup.com/?l=git&m=116562052506776&w=2
*
* @param $key string
* @return mixed on cache hit, false otherwise
*/
function getforfill ($key) {
$get = $this->mode . '_get';
$lock = $this->mode . '_lock';
$rec = $get($this->prefix . $key);
if (!is_null($rec)) {
return unserialize($rec);
}
if ($lock($this->prefix . $key . '_forfill')) {
// we obtained the _forfill lock
// our caller will compute and set the value
return false;
}
// someone else has the lock
// "block" till we can get the value
// actually, loop .05s waiting for it
for ($n=0;$n<5;$n++) {
usleep(10000);
$rec = $get($this->prefix . $key);
if (!is_null($rec)) {
return unserialize($rec);
}
}
return false;
}
/**
* Release the exclusive lock obtained by
* getforfill(). See getforfill()
* for more details.
*
* @param $key string
* @return bool
*/
function releaseforfill ($key) {
$unlock = $this->mode . '_unlock';
return $unlock($this->prefix . $key . '_forfill');
}
}
?>

View file

@ -1,160 +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/>.
/**
* @package core
* @subpackage lib
* @copyright Martin Langhoff <martin@catalyst.net.nz>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* This class abstracts PHP's PECL memcached
* API to provide
*
* - get()
* - set()
* - delete()
* - getforfill()
* - releaseforfill()
*
* Author: Martin Langhoff <martin@catalyst.net.nz>
*
* Note: do NOT store booleans here. With memcached, a false value
* is indistinguisable from a "not found in cache" response.
*
* @package moodlecore
* @copyright Martin Langhoff <martin@catalyst.net.nz>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
**/
class memcached {
function memcached() {
global $CFG;
if (!function_exists('memcache_connect')) {
debugging("Memcached is set to true but the memcached extension is not installed");
return false;
}
$this->_cache = new Memcache;
$hosts = explode(',', $CFG->memcachedhosts);
if (count($hosts) === 1 && !empty($CFG->memcachedpconn)) {
// the faster pconnect is only available
// for single-server setups
// NOTE: PHP-PECL client is buggy and pconnect()
// will segfault if the server is unavailable
$this->_cache->pconnect($hosts[0]);
} else {
// multi-host setup will share key space
foreach ($hosts as $host) {
$host = trim($host);
$this->_cache->addServer($host);
}
}
$this->prefix = $CFG->dbname .'|' . $CFG->prefix . '|';
}
function status() {
if (is_object($this->_cache)) {
return true;
}
return false;
}
function set($key, $value, $ttl=0) {
// we may have acquired a lock via getforfill
// release if it exists
@$this->_cache->delete($this->prefix . $key . '_forfill');
return $this->_cache->set($this->prefix . $key, $value, false);
}
function get($key) {
$rec = $this->_cache->get($this->prefix . $key);
return $rec;
}
function delete($key) {
return $this->_cache->delete($this->prefix . $key);
}
/**
* In the simple case, this function will
* get the cached value if available. If the entry
* is not cached, it will try to get an exclusive
* lock that announces that this process will
* populate the cache.
*
* If we fail to get the lock -- this means another
* process is doing it.
* so we wait (block) for a few microseconds while we wait for
* the cache to be filled or the lock to timeout.
*
* If you get a false from this call, you _must_
* populate the cache ASAP or indicate that
* you won't by calling releaseforfill().
*
* This technique forces serialisation and so helps deal
* with thundering herd scenarios where a lot of clients
* ask the for the same idempotent (and costly) operation.
* The implementation is based on suggestions in this message
* http://marc.theaimsgroup.com/?l=git&m=116562052506776&w=2
*
* @param $key string
* @return mixed on cache hit, NULL otherwise
*/
function getforfill ($key) {
$rec = $this->_cache->get($this->prefix . $key);
if ($rec) {
return $rec;
}
if ($this->_cache->add($this->prefix . $key . '_forfill', 'true', false, 1)) {
// we obtained the _forfill lock
// our caller will compute and set the value
return false;
}
// someone else has the lock
// "block" till we can get the value
// actually, loop .05s waiting for it
for ($n=0;$n<5;$n++) {
usleep(10000);
$rec = $this->_cache->get($this->prefix . $key);
if ($rec) {
return $rec;
}
}
return false;
}
/**
* Release the exclusive lock obtained by
* getforfill(). See getforfill()
* for more details.
*
* @param $key string
* @return bool
*/
function releaseforfill ($key) {
return $this->_cache->delete($this->prefix . $key . '_forfill');
}
}

View file

@ -1589,7 +1589,7 @@ function set_cache_flag($type, $name, $value, $expiry=NULL) {
if ($f = $DB->get_record('cache_flags', array('name'=>$name, 'flagtype'=>$type), '*', IGNORE_MULTIPLE)) { // this is a potential problem in DEBUG_DEVELOPER
if ($f->value == $value and $f->expiry == $expiry and $f->timemodified == $timemodified) {
return true; //no need to update; helps rcache too
return true; //no need to update
}
$f->value = $value;
$f->expiry = $expiry;
@ -10482,15 +10482,6 @@ function get_performance_info() {
$info['txt'] .= "Session: {$info['sessionsize']} ";
}
/* if (isset($rcache->hits) && isset($rcache->misses)) {
$info['rcachehits'] = $rcache->hits;
$info['rcachemisses'] = $rcache->misses;
$info['html'] .= '<span class="rcache">Record cache hit/miss ratio : '.
"{$rcache->hits}/{$rcache->misses}</span> ";
$info['txt'] .= 'rcache: '.
"{$rcache->hits}/{$rcache->misses} ";
}*/
if ($stats = cache_helper::get_stats()) {
$html = '<span class="cachesused">';
$html .= '<span class="cache-stats-heading">Caches interaction by definition then store</span>';

View file

@ -344,13 +344,6 @@ global $COURSE;
*/
global $OUTPUT;
/**
* Shared memory cache.
* @global object $MCACHE
* @name $MCACHE
*/
global $MCACHE;
/**
* Cache used within grouplib to cache data within current request only.
*
@ -592,42 +585,6 @@ if (!empty($CFG->version) and $CFG->version < 2007101509) {
die;
}
// Shared-Memory cache init -- will set $MCACHE
// $MCACHE is a global object that offers at least add(), set() and delete()
// with similar semantics to the memcached PHP API http://php.net/memcache
// Ensure we define rcache - so we can later check for it
// with a really fast and unambiguous $CFG->rcache === false
if (!empty($CFG->cachetype)) {
if (empty($CFG->rcache)) {
$CFG->rcache = false;
} else {
$CFG->rcache = true;
}
// do not try to initialize if cache disabled
if (!$CFG->rcache) {
$CFG->cachetype = '';
}
if ($CFG->cachetype === 'memcached' && !empty($CFG->memcachedhosts)) {
if (!init_memcached()) {
debugging("Error initialising memcached");
$CFG->cachetype = '';
$CFG->rcache = false;
}
} else if ($CFG->cachetype === 'eaccelerator') {
if (!init_eaccelerator()) {
debugging("Error initialising eaccelerator cache");
$CFG->cachetype = '';
$CFG->rcache = false;
}
}
} else { // just make sure it is defined
$CFG->cachetype = '';
$CFG->rcache = false;
}
// Calculate and set $CFG->ostype to be used everywhere. Possible values are:
// - WINDOWS: for any Windows flavour.
// - UNIX: for the rest

View file

@ -1292,41 +1292,6 @@ function make_cache_directory($directory, $exceptiononerror = true) {
return make_writable_directory("$CFG->cachedir/$directory", $exceptiononerror);
}
/**
* Initialises an Memcached instance
* @global memcached $MCACHE
* @return boolean Returns true if an mcached instance could be successfully initialised
*/
function init_memcached() {
global $CFG, $MCACHE;
include_once($CFG->libdir . '/memcached.class.php');
$MCACHE = new memcached;
if ($MCACHE->status()) {
return true;
}
unset($MCACHE);
return false;
}
/**
* Initialises an eAccelerator instance
* @global eaccelerator $MCACHE
* @return boolean Returns true if an eAccelerator instance could be successfully initialised
*/
function init_eaccelerator() {
global $CFG, $MCACHE;
include_once($CFG->libdir . '/eaccelerator.class.php');
$MCACHE = new eaccelerator;
if ($MCACHE->status()) {
return true;
}
unset($MCACHE);
return false;
}
/**
* Checks if current user is a web crawler.
*