next up previous contents
Next: 匯入優化 Up: MySQL 優化 Previous: 查看紀錄檔   Contents

優化

  1. 修改 lib/accesslib.php 讓訪客權限的查詢,改用 cache。
    [root@dywang html]# diff -urN moodle/lib/accesslib.php moodle23/lib/accesslib.php
    --- moodle/lib/accesslib.php	2012-08-10 08:05:01.000000000 +0800
    +++ moodle23/lib/accesslib.php	2014-02-25 13:28:26.000000000 +0800
    @@ -256,7 +256,8 @@
      * @return array
      */
     function get_role_access($roleid) {
    -    global $DB, $ACCESSLIB_PRIVATE;
    +    global $DB, $ACCESSLIB_PRIVATE,$CFG;  //dywang
     
         /* Get it in 1 DB query...
          * - relevant role caps at the root and down
    @@ -264,6 +265,12 @@
          */
     
         //TODO: MUC - this could be cached in shared memory to speed up first page loading, web crawlers, etc.
    +    // Temporary hack for better performance http://tracker.moodle.org/browse/MDL-25288
    +    if (6 == $roleid) { // Guest role dywang 
    +        return unserialize(file_get_contents($CFG->dataroot . '/cache/guest_role_access_cache.serialized'));
    +    } else if ('update_guest_role_access' == $roleid) { // For update script
    +        $roleid = 6;
    +    }
     
         $accessdata = get_empty_accessdata();
     
    @@ -3822,7 +3829,7 @@
         if (empty($fields)) {
             $fields = 'u.id, u.confirmed, u.username, u.firstname, u.lastname, '.
                       'u.maildisplay, u.mailformat, u.maildigest, u.email, u.emailstop, u.city, '.
    -                  'u.country, u.picture, u.idnumber, u.department, u.institution, '.
    +                  'u.department, u.country, u.picture, u.idnumber, u.department, u.institution, '. //dywang
                       'u.lang, u.timezone, u.lastaccess, u.mnethostid, r.name AS rolename, r.sortorder';
         }
    
  2. 撰寫建立訪客權限查詢 cache 檔的 php 程式 guest_role_access_cache_update.php 存放在 admin/cli 目錄下。
    [root@moodle moodle23]# vim admin/cli/guest_role_access_cache_update.php
    <?php
    
    define('CLI_SCRIPT', true);
    
    require_once('../../config.php');
    require_once('../../lib/accesslib.php');
    
    $accessInfo = serialize(get_role_access('update_guest_role_access'));
    $success = file_put_contents($CFG->dataroot . '/cache/guest_role_access_cache.serialized', $accessInfo);
    
    echo ($success ? "Guest role access file updated! $success bites written.\n" : "ERROR during guest access file update!\n");
    ?>
    
  3. 設定例行性工作排程,定時更新 cache 資料,但出現錯誤,原因為例行工作執行時工作目錄不會在 /var/www/html/moodle23/admin/cli/,故 php 中指定相對目錄的檔找不到。
    [root@moodle moodle23]# crontab -e 
    29 * * * * php /$moodledir/moodle23/admin/cli/guest_role_access_cache_update.php
    
    PHP Warning:  require_once(../../config.php): failed to open stream: No such fil
    e or directory in /var/www/html/moodle23/admin/cli/guest_role_access_cache_updat
    e.php on line 5
    
  4. 設定例行性工作排程,定時更新 cache 資料,先變換目錄再執行,結果正常。
    [root@moodle moodle23]# crontab -e 
    29 * * * * cd /var/www/html/moodle23/admin/cli && /usr/bin/php guest_role_access
    
    Guest role access file updated! 1188 bites written.
    


2017-08-04