Changeset 172


Ignore:
Timestamp:
Jun 15, 2006 7:59:45 PM (18 years ago)
Author:
scdev
Message:

Q - added caching to ACL, and flush command to acl.cli.php

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/bin/acl.cli.php

    r171 r172  
    116116    break;
    117117
     118case 'flush' :
     119    echo $acl->initDB(true) ? "Ok\n" : "Error!\n";
     120    break;
     121
    118122case 'grant' :
    119123    $aro = isset($_SERVER['argv'][2]) ? $_SERVER['argv'][2] : null;
     
    195199<?php echo $_SERVER['argv'][0]; ?> rmaco <aco_object>
    196200<?php echo $_SERVER['argv'][0]; ?> rmaxo <axo_object>
     201<?php echo $_SERVER['argv'][0]; ?> flush
    197202<?php echo $_SERVER['argv'][0]; ?> grant <aro_object> [aco_object] [axo_object]
    198203<?php echo $_SERVER['argv'][0]; ?> revoke <aro_object> [aco_object] [axo_object]
  • trunk/lib/ACL.inc.php

    r171 r172  
    1212*/
    1313
     14require_once dirname(__FILE__) . '/Cache.inc.php';
     15
    1416class ACL {
    1517
    1618    // Configuration parameters for this object.
    1719    var $_params = array(
     20       
     21        // If false nothing will be cached or retreived. Useful for testing realtime data requests.
     22        'enable_cache' => true,
    1823
    1924        // Automatically create table and verify columns. Better set to false after site launch.
     
    2732    {
    2833        $app =& App::getInstance();
     34
     35        // Configure the cache object.
     36        $this->cache = new Cache('acl');
     37        $this->cache->setParam(array('enabled' => true));
    2938
    3039        // Get create tables config from global context.
     
    170179        }
    171180        $_db_tested = true;
     181        return true;
    172182    }
    173183
     
    219229        $qid = $db->query("SELECT 1 FROM $tbl WHERE name = '" . $db->escapeString($name) . "'");
    220230        if (mysql_num_rows($qid) > 0) {
    221             $app->logMsg(sprintf('Cannot add %s node, name exists: %s', $type, $name), LOG_WARNING, __FILE__, __LINE__);
     231            $app->logMsg(sprintf('Cannot add %s node, already exists: %s', $type, $name), LOG_NOTICE, __FILE__, __LINE__);
    222232            return false;
    223233        }
     
    299309        $qid = $db->query("SELECT lft, rgt FROM $tbl WHERE name = '" . $db->escapeString($name) . "'");
    300310        if (!list($lft, $rgt) = mysql_fetch_row($qid)) {
    301             $app->logMsg(sprintf('Cannot delete nonexistant %s name: %s', $type, $name), LOG_WARNING, __FILE__, __LINE__);
     311            $app->logMsg(sprintf('Cannot delete nonexistant %s name: %s', $type, $name), LOG_NOTICE, __FILE__, __LINE__);
    302312            return false;
    303313        }
     
    412422        $aco = is_null($aco) ? 'root' : $aco;
    413423        $axo = is_null($axo) ? 'root' : $axo;
    414 
    415         $qid = $db->query("
    416             SELECT acl_tbl.access
    417             FROM acl_tbl
    418             LEFT JOIN aro_tbl ON (acl_tbl.aro_id = aro_tbl.aro_id)
    419             LEFT JOIN aco_tbl ON (acl_tbl.aco_id = aco_tbl.aco_id)
    420             LEFT JOIN axo_tbl ON (acl_tbl.axo_id = axo_tbl.axo_id)
    421             WHERE aro_tbl.lft <= (SELECT lft FROM aro_tbl WHERE name = '" . $db->escapeString($aro) . "')
    422             AND aco_tbl.lft <= (SELECT lft FROM aco_tbl WHERE name = '" . $db->escapeString($aco) . "')
    423             AND axo_tbl.lft <= (SELECT lft FROM axo_tbl WHERE name = '" . $db->escapeString($axo) . "')
    424             ORDER BY aro_tbl.aro_id DESC, aco_tbl.aco_id DESC, axo_tbl.axo_id DESC
    425             LIMIT 1
    426         ");
    427         if (!list($access) = mysql_fetch_row($qid)) {
    428             $app->logMsg(sprintf('Access denyed: %s -> %s -> %s. No records found.', $aro, $aco, $axo), LOG_DEBUG, __FILE__, __LINE__);
    429             return false;
     424       
     425        $cache_hash = $aro . '|' . $aco . '|' . $axo;
     426        if ($this->cache->exists($cache_hash) && true === $this->getParam('enable_cache')) {
     427            // Access value is cached.
     428            $access = $this->cache->get($cache_hash);
     429        } else {
     430            // Retreive access value from db.
     431            $qid = $db->query("
     432                SELECT acl_tbl.access
     433                FROM acl_tbl
     434                LEFT JOIN aro_tbl ON (acl_tbl.aro_id = aro_tbl.aro_id)
     435                LEFT JOIN aco_tbl ON (acl_tbl.aco_id = aco_tbl.aco_id)
     436                LEFT JOIN axo_tbl ON (acl_tbl.axo_id = axo_tbl.axo_id)
     437                WHERE aro_tbl.lft <= (SELECT lft FROM aro_tbl WHERE name = '" . $db->escapeString($aro) . "')
     438                AND aco_tbl.lft <= (SELECT lft FROM aco_tbl WHERE name = '" . $db->escapeString($aco) . "')
     439                AND axo_tbl.lft <= (SELECT lft FROM axo_tbl WHERE name = '" . $db->escapeString($axo) . "')
     440                ORDER BY aro_tbl.aro_id DESC, aco_tbl.aco_id DESC, axo_tbl.axo_id DESC
     441                LIMIT 1
     442            ");
     443            if (!list($access) = mysql_fetch_row($qid)) {
     444                $app->logMsg(sprintf('Access denyed: %s -> %s -> %s. No records found.', $aro, $aco, $axo), LOG_DEBUG, __FILE__, __LINE__);
     445                return false;
     446            }
     447            $this->cache->set($cache_hash, $access);
    430448        }
    431449       
  • trunk/lib/App.inc.php

    r170 r172  
    156156        // Initialize default parameters.
    157157        $this->_params = array_merge($this->_params, $this->_param_defaults);
     158       
     159        // Begin timing script.
     160        require_once dirname(__FILE__) . '/ScriptTimer.inc.php';
     161        $this->timer = new ScriptTimer();
     162        $this->timer->start('_app');
    158163    }
    159164
     
    334339        restore_include_path();
    335340        $this->running = false;
     341        $num_queries = 0;
    336342        if (true === $this->getParam('enable_db')) {
     343            $num_queries = $this->db->numQueries();
    337344            $this->db->close();
    338345        }
     346        $this->timer->stop('_app');
     347        $this->logMsg(sprintf('Script ended gracefully. Execution time: %s. Number of db queries: %s.', $this->timer->getTime('_app'), $num_queries), LOG_DEBUG, __FILE__, __LINE__);
    339348    }
    340349
  • trunk/lib/Cache.inc.php

    r162 r172  
    4747    function Cache($namespace='')
    4848    {
     49        $app =& App::getInstance();
     50       
    4951        $this->_ns = $namespace;
     52
     53        if (true !== $app->getParam('enable_session')) {
     54            $app->logMsg('Cache disabled, enable_session is false.', LOG_DEBUG, __FILE__, __LINE__);
     55            $this->setParam(array('enabled' => false));
     56        }
    5057       
    5158        if (!isset($_SESSION['_cache'][$this->_ns])) {
     
    191198            return unserialize($_SESSION['_cache'][$this->_ns][$keyhash]);
    192199        } else {
     200            $app->logMsg(sprintf('Missing %s from cache.', $key), LOG_DEBUG, __FILE__, __LINE__);
    193201            return false;
    194202        }
  • trunk/lib/DB.inc.php

    r156 r172  
    1717    // Database handle.
    1818    var $dbh;
     19   
     20    // Count how many queries run during the whole instance.
     21    var $_query_count = 0;
    1922
    2023    // Hash of DB parameters.
     
    229232    function query($query, $debug=false)
    230233    {   
    231         static $_query_count = 0;
    232234        $app =& App::getInstance();
    233235
     
    236238        }
    237239
    238         $_query_count++;
     240        $this->_query_count++;
     241
    239242        $debugqry = preg_replace("/\n[\t ]+/", "\n", $query);
    240243        if ($this->getParam('db_always_debug') || $debug) {
    241             echo "<!-- ----------------- Query $_query_count ---------------------\n$debugqry\n-->\n";
     244            echo "<!-- ----------------- Query $this->_query_count ---------------------\n$debugqry\n-->\n";
    242245        }
    243246
     
    342345        }
    343346    }
     347   
     348    /*
     349    * Return the total number of queries run this for.
     350    *
     351    * @access   public
     352    * @return   int Number of queries
     353    * @author   Quinn Comendant <quinn@strangecode.com>
     354    * @version  1.0
     355    * @since    15 Jun 2006 11:46:05
     356    */
     357    function numQueries()
     358    {
     359        return $this->_query_count;
     360    }
    344361
    345362    /**
  • trunk/lib/ScriptTimer.inc.php

    r136 r172  
    1818    function stop($name='default')
    1919    {
     20        if (!isset($this->_timing_stop_times[$name]) || !isset($this->_timing_cumulative_times[$name])) {
     21            $this->_timing_stop_times[$name] = null;
     22            $this->_timing_cumulative_times[$name] = null;
     23        }
    2024        $this->_timing_stop_times[$name] = explode(' ', microtime());
    2125        $this->_timing_cumulative_times[$name] += $this->getTime($name);
     
    2529    {
    2630        if (!isset($this->_timing_start_times[$name])) {
    27             return 0;
     31            return 0;
    2832        }
    2933        if (!isset($this->_timing_stop_times[$name])) {
     
    4852        }
    4953       
    50         $this->_timing_cumulative_times["TOTAL"] = array_sum($this->_timing_cumulative_times);
     54        $this->_timing_cumulative_times['TOTAL'] = array_sum($this->_timing_cumulative_times);
    5155
    5256        echo '<pre>';
Note: See TracChangeset for help on using the changeset viewer.