Skip to content

Commit 5bd29e3

Browse files
committed
Add cache file to enable faster generation of output.
Caching of the results of the tags includes a md5 of the file. This allows different git branches to continue to make use of the cache file 4 when switching. Caching of fields is not included. You will currently get unexpected behaviour if using a cachefile with different fields included. Performance seen from changes; - When updating Moodle 2.4 using a cache file, updates are approximately 3 seconds with no changes.
1 parent f32ebcb commit 5bd29e3

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

PHPCtags.class.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class PHPCtags
2323
private $mLines;
2424
private $mOptions;
2525
private $tagdata;
26+
private $cachefile;
2627
private $filecount;
2728

2829
public function __construct($options)
@@ -60,6 +61,10 @@ public function addFile($file)
6061
$this->mFiles[realpath($file)] = 1;
6162
}
6263

64+
public function setCacheFile($file) {
65+
$this->cachefile = $file;
66+
}
67+
6368
public function addFiles($files)
6469
{
6570
foreach ($files as $file) {
@@ -377,6 +382,13 @@ private function full_render() {
377382
$str = self::stringSortByLine($str, $this->mOptions['sort'] == 'foldcase');
378383
}
379384

385+
// Save all tag information to a file for faster updates if a cache file was specified.
386+
if (isset($this->cachefile)) {
387+
file_put_contents($this->cachefile, serialize($this->tagdata));
388+
if ($this->mOptions['v']) {
389+
echo "Saved cache file.".PHP_EOL;
390+
}
391+
}
380392
return $str;
381393
}
382394

@@ -395,6 +407,14 @@ public function export()
395407

396408
private function process($file)
397409
{
410+
// Load the tag md5 data to skip unchanged files.
411+
if (!isset($this->tagdata) && isset($this->cachefile) && file_exists($this->cachefile)) {
412+
if ($this->mOptions['v']) {
413+
echo "Loaded cache file.".PHP_EOL;
414+
}
415+
$this->tagdata = unserialize(file_get_contents($this->cachefile));
416+
}
417+
398418
if (is_dir($file) && isset($this->mOptions['R'])) {
399419
$iterator = new RecursiveIteratorIterator(
400420
new RecursiveDirectoryIterator(

bootstrap.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
-f <name>
4242
Write tags to specified file. Value of "-" writes tags to stdout
4343
["tags"].
44+
-C <name>
45+
Use a cache file to store tags for faster updates.
4446
-n Equivalent to --excmd=number.
4547
-N Equivalent to --excmd=pattern.
4648
-o Alternative for -f.
@@ -203,6 +205,7 @@
203205
try {
204206
$ctags = new PHPCtags($options);
205207
$ctags->addFiles($argv);
208+
$ctags->setCacheFile(isset($options['C']) ? $options['C'] : null);
206209
$result = $ctags->export();
207210
} catch (Exception $e) {
208211
die("phpctags: {$e->getMessage()}".PHP_EOL);

0 commit comments

Comments
 (0)