From a9e0e66dd5967788a3b5862022e0080a9b2bb300 Mon Sep 17 00:00:00 2001 From: Wouter J Date: Mon, 2 Dec 2013 14:50:47 +0100 Subject: [PATCH 1/2] Added Caching article --- components/expression_language/caching.rst | 61 +++++++++++++++++++ components/expression_language/index.rst | 1 + .../expression_language/introduction.rst | 6 ++ components/map.rst.inc | 1 + 4 files changed, 69 insertions(+) create mode 100644 components/expression_language/caching.rst diff --git a/components/expression_language/caching.rst b/components/expression_language/caching.rst new file mode 100644 index 00000000000..44b0760612d --- /dev/null +++ b/components/expression_language/caching.rst @@ -0,0 +1,61 @@ +.. index:: + single: Caching; ExpressionLanguage + +Caching Expressions Using ParserCaches +====================================== + +The ExpressionLanguage component already provides a +:method:`Symfony\\Component\\ExpresionLanguage\\ExpressionLanguage::compile` +method to be able to cache the expressions in plain PHP. But internally, the +component also caches the parsed expressions, so duplicated expressions can be +compiled/evaluated quicker. + +The Workflow +------------ + +Both ``evaluate`` and ``compile`` needs to do some things before it can +provide the return values. For ``evaluate``, this overhead is even bigger. + +Both methods need to tokenize and parse the expression. This is done by the +:method:`Symfony\\Component\\ExpressionLanguage\\ExpressionLanguage::parse` +method. It'll return a :class:`Symfony\\Component\\ExpressionLanguage\\ParsedExpression`. +Now, the ``compile`` method just returns the string conversion of this object. +The ``evaluate`` method needs to loop through the "nodes" (pieces of an +expression saved in the ``ParsedExpression``) and evaluate them on the fly. + +To save time, the ``ExpressionLanguage`` caches the ``ParsedExpression``, so +it can skip the tokenize and parse steps with duplicate expressions. +The caching is done by a +:class:`Symfony\\Component\\ExpressionLanguage\\ParserCache\\ParserCacheInterface` +instance (by default, it uses an +:class:`Symfony\\Component\\ExpressionLanguage\\ParserCache\\ArrayParserCache`). +You can customize this by creating a custom ``ParserCache`` and injecting this +in the object using the constructor:: + + use Symfony\Component\ExpressionLanguage\ExpressionLanguage; + use Acme\ExpressionLanguage\ParserCache\MyDatabaseParserCache; + + $cache = new MyDatabaseParserCache(...); + $language = new ExpressionLanguage($cache); + +Using Parsed and Serialized Expressions +--------------------------------------- + +Both ``evaluate`` and ``compile`` can handle ``ParsedExpression`` and +``SerializedParsedExpression``:: + + use Symfony\Component\ExpressionLanguage\ParsedExpression; + // ... + + $expression = new ParsedExpression($language->parse('1 + 4')); + + echo $language->evaluate($expression); // prints 5 + +.. code-block:: php + + use Symfony\Component\ExpressionLanguage\SerializedParsedExpression; + // ... + + $expression = new SerializedParsedExpression(serialize($language->parse('1 + 4'))); + + echo $language->evaluate($expression); // prints 5 diff --git a/components/expression_language/index.rst b/components/expression_language/index.rst index 5ae40be979f..aa63907d921 100644 --- a/components/expression_language/index.rst +++ b/components/expression_language/index.rst @@ -7,3 +7,4 @@ Expression Language introduction syntax extending + caching diff --git a/components/expression_language/introduction.rst b/components/expression_language/introduction.rst index 78fd8ae8a74..a53c8aa960d 100644 --- a/components/expression_language/introduction.rst +++ b/components/expression_language/introduction.rst @@ -109,4 +109,10 @@ PHP type (including objects):: This will print "Honeycrisp". For more information, see the :doc:`/components/expression_language/syntax` entry, especially :ref:`component-expression-objects` and :ref:`component-expression-arrays`. +Caching +------- + +The component provides some different caching strategies, read more about them +in :doc:`/components/expression_language/caching`. + .. _Packagist: https://packagist.org/packages/symfony/expression-language diff --git a/components/map.rst.inc b/components/map.rst.inc index 2703a6057fc..a5f2d0f8db7 100644 --- a/components/map.rst.inc +++ b/components/map.rst.inc @@ -62,6 +62,7 @@ * :doc:`/components/expression_language/introduction` * :doc:`/components/expression_language/syntax` * :doc:`/components/expression_language/extending` + * :doc:`/components/expression_language/caching` * **Filesystem** From 0a59f6d4956ccbfe2b58326969c166cd1c14f6f8 Mon Sep 17 00:00:00 2001 From: Wouter J Date: Mon, 2 Dec 2013 16:54:39 +0100 Subject: [PATCH 2/2] Added doctrine note --- components/expression_language/caching.rst | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/components/expression_language/caching.rst b/components/expression_language/caching.rst index 44b0760612d..12953e66cb2 100644 --- a/components/expression_language/caching.rst +++ b/components/expression_language/caching.rst @@ -38,6 +38,12 @@ in the object using the constructor:: $cache = new MyDatabaseParserCache(...); $language = new ExpressionLanguage($cache); +.. note:: + + The `DoctrineBridge`_ has a ParserCache implementation using the + `doctrine cache library`_, which gives you caching for all sorts of cache + strategies, like Apc, Filesystem and Apc. + Using Parsed and Serialized Expressions --------------------------------------- @@ -59,3 +65,6 @@ Both ``evaluate`` and ``compile`` can handle ``ParsedExpression`` and $expression = new SerializedParsedExpression(serialize($language->parse('1 + 4'))); echo $language->evaluate($expression); // prints 5 + +.. _DoctrineBridge: https://github.com/symfony/DoctrineBridge +.. _`doctrine cache library`: http://docs.doctrine-project.org/projects/doctrine-common/en/latest/reference/caching.html