helllicht / kirby-autoid

AutoID is a plugin for Kirby 2 wich generates unique ids (numeric or hash) for every page.
MIT License
46 stars 3 forks source link

suggestion speed up autoid #7

Open bnomei opened 7 years ago

bnomei commented 7 years ago

hash you could forward the $page->diruri() into getUniqueAutoId() and use it as an additional param to create the hash. it would be a pretty good unique and the very expensive check using site()->pages()->index() in getUniqueAutoIdcould be avoided.

id why not write/read the latest value in an autoid-storage-file at /cache/? and only check for index if file is missing? that is certainly faster than site()->pages()->index() every time.

bnomei commented 7 years ago

cached hash to file maps instead of using site()->pages()->index() one could also cache the results to make it faster. that is when having al lot of pages and index() is slower than a file-read.

public static function getPageByAutoID($autoid) {
    $f = kirby()->roots()->cache().DS.'autoid-'.$autoid.'.txt';
    if(f::exists($f)) {
        $uri = f::read($f);
        if($page = page($uri)) {
            return $page;
        }
    }
    $pageCollection = site()->pages()->index()->filterBy(c::get('autoid.name', 'autoid'), $autoid);
    if($pageCollection->count() > 0) {
        $page = $pageCollection->first();
        f::write($f, $page->uri());
        return $page;
    }
    return null;
}
Targoran commented 7 years ago

You are making a valid point, site()->pages()->index() is indeed quite slow on bigger sites. Will look into it soon!