sitemap icon indicating copy to clipboard operation
sitemap copied to clipboard

caching docs is confusing and missing important info

Open vesper8 opened this issue 6 years ago • 3 comments

Took me a while to figure out how the cache works and had to read a lot of issues and look into the code to finally get to the conclusion that on top of enabling the cache in the config file, you must add this code at the top of your controller action to return the cached version

        if (Sitemap::hasCachedView()) {
            return Sitemap::render();
        }

vesper8 avatar Nov 20 '17 22:11 vesper8

You shouldn't have to do that - the render() method will check for a cached view and return it by default. See these lines.

dwightwatson avatar Nov 20 '17 23:11 dwightwatson

@dwightwatson I know the render() does that.. I noticed

However I don't understand the thinking behind this since the call to Sitemap::render() is usually done after doing all the processing.

My controller action for one of my sitemaps (a big one with 5k url and 10k images, does a huge amount of processing in order to build up the sitemap. How is having a check for a cached version at the bottom of that code allowing to skip the processing.. which is the point of caching.

Maybe there's something I'm not getting?

Here's my code


    public function submission(Game $game)
    {
        if (Sitemap::hasCachedView()) {
            return Sitemap::render();
        }

        ini_set("max_execution_time", 240);
        ini_set("memory_limit", "1024M");

        $imageHelpers = new ImageHelpers();

        $input = [
            "game_id" => $game->id,
        ];
        $submissions = Submission::getSubmissions("sitemap", $input);

        foreach ($submissions as $submission) {
            $tag = Sitemap::addTag($submission->url(), $submission->updated_at, "daily", "0.8");

            $title = $submission->getTitleForSeo() . " in " . $submission->game->name;

            foreach ($submission->getMedia("submissions") as $image) {
                $tag->addImage(url($imageHelpers->getGlideUrl($image, "watermarked")), $title);
            }
        }

        return Sitemap::render();
    }

vesper8 avatar Nov 20 '17 23:11 vesper8

Yeah... that's actually a very fair point.

Will re-open this as to address the documentation on this area better. Open to PRs on it otherwise I'll try and find some time to improve this soon.

dwightwatson avatar Nov 20 '17 23:11 dwightwatson