diff options
author | Michael Hamann <michael@content-space.de> | 2012-12-04 16:13:46 +0100 |
---|---|---|
committer | Michael Hamann <michael@content-space.de> | 2012-12-04 16:13:46 +0100 |
commit | 3d2ce006bd7cffab5cda27f01787d2fd66ab630e (patch) | |
tree | d8ab1ef5e7b94642f48a71e4e9e6cc5bfc40969d /inc/indexer.php | |
parent | 0572700e3546546abe8375a0fff7245673f2b02a (diff) | |
download | dokuwiki-3d2ce006bd7cffab5cda27f01787d2fd66ab630e.tar.gz dokuwiki-3d2ce006bd7cffab5cda27f01787d2fd66ab630e.zip |
Indexer: Add cache for getPID()
This avoids re-reading of the page index file for every getPID()-call by
using a simple FIFO cache, limited to 10 items. In idx_addPage() and the
functions that it calls getPID() is called 3 times for the same PID.
Diffstat (limited to 'inc/indexer.php')
-rw-r--r-- | inc/indexer.php | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/inc/indexer.php b/inc/indexer.php index cbb817d78..05b082872 100644 --- a/inc/indexer.php +++ b/inc/indexer.php @@ -102,6 +102,10 @@ function wordlen($w){ * @author Tom N Harris <tnharris@whoopdedo.org> */ class Doku_Indexer { + /** + * @var array $pidCache Cache for getPID() + */ + protected $pidCache = array(); /** * Adds the contents of a page to the fulltext index @@ -460,6 +464,9 @@ class Doku_Indexer { * @return bool|int The page id on success, false on error */ public function getPID($page) { + // return PID without locking when it is in the cache + if (isset($this->pidCache[$page])) return $this->pidCache[$page]; + if (!$this->lock()) return false; @@ -482,7 +489,14 @@ class Doku_Indexer { * @return bool|int The page id on success, false on error */ protected function getPIDNoLock($page) { - return $this->addIndexKey('page', '', $page); + // avoid expensive addIndexKey operation for the most recently requested pages by using a cache + if (isset($this->pidCache[$page])) return $this->pidCache[$page]; + $pid = $this->addIndexKey('page', '', $page); + // limit cache to 10 entries by discarding the oldest element as in DokuWiki usually only the most recently + // added item will be requested again + if (count($this->pidCache) > 10) array_shift($this->pidCache); + $this->pidCache[$page] = $pid; + return $pid; } /** |