Beispiel #1
0
 /**
  * The real highlighting function
  * @throw InvalidArgumentException if $scanner is not either a string or a
  *    LuminousScanner instance, or if $source is not a string.
  */
 function highlight($scanner, $source, $use_cache = true)
 {
     if (!is_string($source)) {
         throw new InvalidArgumentException('Non-string ' . 'supplied for $source');
     }
     if (!$scanner instanceof LuminousScanner) {
         if (!is_string($scanner)) {
             throw new InvalidArgumentException('Non-string
     or LuminousScanner instance supllied for $scanner');
         }
         $code = $scanner;
         $scanner = $this->scanners->GetScanner($code);
         if ($scanner === null) {
             throw new Exception("No known scanner for '{$code}' and no default set");
         }
     }
     $cache_obj = null;
     $cache_hit = true;
     $out = null;
     if ($use_cache) {
         $cache_id = $this->cache_id($scanner, $source);
         if ($this->settings->sql_function !== null) {
             $cache_obj = new LuminousSQLCache($cache_id);
             $cache_obj->set_sql_function($this->settings->sql_function);
         } else {
             $cache_obj = new LuminousFileSystemCache($cache_id);
         }
         $cache_obj->set_purge_time($this->settings->cache_age);
         $out = $cache_obj->read();
     }
     if ($out === null) {
         $cache_hit = false;
         $out_raw = $scanner->highlight($source);
         $formatter = $this->get_formatter();
         $out = $formatter->format($out_raw);
     }
     if ($use_cache && !$cache_hit) {
         $cache_obj->write($out);
     }
     return $out;
 }
Beispiel #2
0
$write_fs = array();
for ($i = 0; $i < 1000; $i++) {
    $data = '';
    for ($j = 0; $j < 1024 / 8; $j++) {
        // unrolled for speed
        $data .= chr(rand(32, 126));
        $data .= chr(rand(32, 126));
        $data .= chr(rand(32, 126));
        $data .= chr(rand(32, 126));
        $data .= chr(rand(32, 126));
        $data .= chr(rand(32, 126));
        $data .= chr(rand(32, 126));
        $data .= chr(rand(32, 126));
    }
    $id = md5($data);
    $sql_cache = new LuminousSQLCache($id);
    $sql_cache->set_sql_function('query');
    $t = microtime(true);
    $sql_cache->write($data);
    $t1 = microtime(true);
    $data1 = $sql_cache->read();
    $t2 = microtime(true);
    $read_sql[] = $t2 - $t1;
    $write_sql[] = $t1 - $t;
    assert($data === $data1);
    $fs_cache = new LuminousFileSystemCache($id);
    $t = microtime(true);
    $fs_cache->write($data);
    $t1 = microtime(true);
    $data1 = $fs_cache->read();
    $t2 = microtime(true);