Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

How to cache whole action in Phalcon?

I wonder how why Phalcon does not have the cache method of caching whole action output. (or I dont know yet).

For example, I want to cache the entire home page of my site as a static html page, so that the server will display a html page instead of requesting to database many times.

I know that Phalcon has a cache method called Caching Output Fragments

    <?php

    //Create an Output frontend. Cache the files for 2 days
    $frontCache = new Phalcon\Cache\Frontend\Output(array(
        "lifetime" => 172800
    ));

    // Create the component that will cache from the "Output" to a "File" backend
    // Set the cache file directory - it's important to keep the "/" at the end of
    // the value for the folder
    $cache = new Phalcon\Cache\Backend\File($frontCache, array(
        "cacheDir" => "../app/cache/"
    ));

    // Get/Set the cache file to ../app/cache/my-cache.html
    $content = $cache->start("my-cache.html");

    // If $content is null then the content will be generated for the cache
    if ($content === null) {

        //Print date and time
        echo date("r");

        //Generate a link to the sign-up action
        echo Phalcon\Tag::linkTo(
            array(
                "user/signup",
                "Sign Up",
                "class" => "signup-button"
            )
        );

        // Store the output into the cache file
        $cache->save();

    } else {

        // Echo the cached output
        echo $content;
    }

But it just implement the cache when there are some echo calls in the action. If in the action, it just returns the result for the view - how to implement the Caching Output Fragments above?

However, the needed cache is the final output of an action. It means that the render content of the view is exactly the date to cache. In the method above, it caches what the action echoes - not mention to the view.

Ex:

    <?php

    //Create an Output frontend. Cache the files for 2 days
    $frontCache = new Phalcon\Cache\Frontend\Output(array(
        "lifetime" => 172800
    ));

    // Create the component that will cache from the "Output" to a "File" backend
    // Set the cache file directory - it's important to keep the "/" at the end of
    // the value for the folder
    $cache = new Phalcon\Cache\Backend\File($frontCache, array(
        "cacheDir" => "../app/cache/"
    ));

    // Get/Set the cache file to ../app/cache/my-cache.html
    $content = $cache->start("my-cache.html");

    // If $content is null then the content will be generated for the cache
    if ($content === null) {

        //Print date and time
        echo date("r");

        //Generate a link to the sign-up action
        echo Phalcon\Tag::linkTo(
            array(
                "user/signup",
                "Sign Up",
                "class" => "signup-button"
            )
        );

        // Store the output into the cache file
        $cache->save();

        $results = Resultset::find();
        $this->view->results = $results;

    } else {

        // Echo the cached output
        echo $content;
    }

I hope that we can found solution for this situation.

@Phalcon: Thanks for your instruction.

In your code:

    if (!$usersId) {

        /**
         * Enable cache
         */
        $this->view->cache(array('key' => 'post-' . $id));

        /**
         * Check for a cache
         */
        if ($this->viewCache->exists('post-' . $id)) {
            return;
        }
    }

I don't know why you put this line:

    $this->view->cache(array('key' => 'post-' . $id));

before the condition that check the existion of cache key?

    if ($this->viewCache->exists('post-' . $id)) {
            return;
        }

As I thought, it would be:

    if (!$usersId) {
        /**
         * Check for a cache
         */
        if ($this->viewCache->exists('post-' . $id)) {
            return;
        }

        /**
         * Enable cache
         */
        $this->view->cache(array('key' => 'post-' . $id));
    }

because it will create cache only if cache key does not exist.

In your code, does it always recreate new cache, dispite it has already this cache before?



84.5k

This is not creating a cache $this->view->cache(array("key" => "post-" . $id)); only enables it

Although viewCache worked, I found that the response time was very long, approximately 1s. While without viewCache, the response time was approximately 1.34s

Why viewCache was too slowly?