Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Template cache memory exhausted. #2903

Closed
blasvicco opened this issue May 18, 2018 · 0 comments
Closed

BUG: Template cache memory exhausted. #2903

blasvicco opened this issue May 18, 2018 · 0 comments

Comments

@blasvicco
Copy link
Contributor

Description

Memory usage can reach over 250MB caching template.
The function in craftcms/cms/src/services/TemplateCaches.php called includeElementQueryInTemplateCaches can be optimized reducing the memory usage to less than 15 Mb.

Suggestion

    /**
     * Includes an element criteria in any active caches.
     *
     * @param Event $event The 'afterPrepare' element query event
     */
    public function includeElementQueryInTemplateCaches(Event $event)
    {
        // Make sure template caching is enabled
        if ($this->_isTemplateCachingEnabled() === false) {
            return;
        }

        if (!empty($this->_cachedQueries)) {
            /** @var ElementQuery $elementQuery */
            $elementQuery = $event->sender;
            $query = $elementQuery->query;
            $subQuery = $elementQuery->subQuery;
            $customFields = $elementQuery->customFields;
            $elementQuery->query = null;
            $elementQuery->subQuery = null;
            $elementQuery->customFields = null;
            // We need to base64-encode the string so db\Connection::quoteSql() doesn't tweak any of the table/columns names
            $serialized = base64_encode(serialize($elementQuery));
            $elementQuery->query = $query;
            $elementQuery->subQuery = $subQuery;
            $elementQuery->customFields = $customFields;
            $hash = md5($serialized);

            gc_disable(); // disable automatic garbage collector
            foreach ($this->_cachedQueries as &$queries) {
                $queries[$hash] = [
                    $elementQuery->elementType,
                    $serialized
                ];
                gc_collect_cycles();
            }
            $queries = null; // release memory
            unset($queries); // break the binding between variable name and variable content
            gc_enable(); // enabling automatic garbage collector
        }
    }

Steps to reproduce

  1. Mesure the memory usage of a non cached request with the enableTemplateCaching = true.

Additional info

  • Craft version: 3
  • PHP version: 7.0.12
@blasvicco blasvicco changed the title Template cache need memory optimization. BUG: Template cache memory exhausted. May 18, 2018
brandonkelly added a commit that referenced this issue May 22, 2018
BUGFIX: Template cache memory exhausted. #2903
brandonkelly added a commit that referenced this issue May 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants