|
| 1 | +.. index:: |
| 2 | + single: Caching; ExpressionLanguage |
| 3 | + |
| 4 | +Caching Expressions Using ParserCaches |
| 5 | +====================================== |
| 6 | + |
| 7 | +The ExpressionLanguage component already provides a |
| 8 | +:method:`Symfony\\Component\\ExpresionLanguage\\ExpressionLanguage::compile` |
| 9 | +method to be able to cache the expressions in plain PHP. But internally, the |
| 10 | +component also caches the parsed expressions, so duplicated expressions can be |
| 11 | +compiled/evaluated quicker. |
| 12 | + |
| 13 | +The Workflow |
| 14 | +------------ |
| 15 | + |
| 16 | +Both ``evaluate`` and ``compile`` needs to do some things before it can |
| 17 | +provide the return values. For ``evaluate``, this overhead is even bigger. |
| 18 | + |
| 19 | +Both methods need to tokenize and parse the expression. This is done by the |
| 20 | +:method:`Symfony\\Component\\ExpressionLanguage\\ExpressionLanguage::parse` |
| 21 | +method. It'll return a :class:`Symfony\\Component\\ExpressionLanguage\\ParsedExpression`. |
| 22 | +Now, the ``compile`` method just returns the string conversion of this object. |
| 23 | +The ``evaluate`` method needs to loop through the "nodes" (pieces of an |
| 24 | +expression saved in the ``ParsedExpression``) and evaluate them on the fly. |
| 25 | + |
| 26 | +To save time, the ``ExpressionLanguage`` caches the ``ParsedExpression``, so |
| 27 | +it can skip the tokenize and parse steps with duplicate expressions. |
| 28 | +The caching is done by a |
| 29 | +:class:`Symfony\\Component\\ExpressionLanguage\\ParserCache\\ParserCacheInterface` |
| 30 | +instance (by default, it uses an |
| 31 | +:class:`Symfony\\Component\\ExpressionLanguage\\ParserCache\\ArrayParserCache`). |
| 32 | +You can customize this by creating a custom ``ParserCache`` and injecting this |
| 33 | +in the object using the constructor:: |
| 34 | + |
| 35 | + use Symfony\Component\ExpressionLanguage\ExpressionLanguage; |
| 36 | + use Acme\ExpressionLanguage\ParserCache\MyDatabaseParserCache; |
| 37 | + |
| 38 | + $cache = new MyDatabaseParserCache(...); |
| 39 | + $language = new ExpressionLanguage($cache); |
| 40 | + |
| 41 | +.. note:: |
| 42 | + |
| 43 | + The `DoctrineBridge`_ has a ParserCache implementation using the |
| 44 | + `doctrine cache library`_, which gives you caching for all sorts of cache |
| 45 | + strategies, like Apc, Filesystem and Apc. |
| 46 | + |
| 47 | +Using Parsed and Serialized Expressions |
| 48 | +--------------------------------------- |
| 49 | + |
| 50 | +Both ``evaluate`` and ``compile`` can handle ``ParsedExpression`` and |
| 51 | +``SerializedParsedExpression``:: |
| 52 | + |
| 53 | + use Symfony\Component\ExpressionLanguage\ParsedExpression; |
| 54 | + // ... |
| 55 | + |
| 56 | + $expression = new ParsedExpression($language->parse('1 + 4')); |
| 57 | + |
| 58 | + echo $language->evaluate($expression); // prints 5 |
| 59 | + |
| 60 | +.. code-block:: php |
| 61 | +
|
| 62 | + use Symfony\Component\ExpressionLanguage\SerializedParsedExpression; |
| 63 | + // ... |
| 64 | +
|
| 65 | + $expression = new SerializedParsedExpression(serialize($language->parse('1 + 4'))); |
| 66 | +
|
| 67 | + echo $language->evaluate($expression); // prints 5 |
| 68 | +
|
| 69 | +.. _DoctrineBridge: https://github.com/symfony/DoctrineBridge |
| 70 | +.. _`doctrine cache library`: http://docs.doctrine-project.org/projects/doctrine-common/en/latest/reference/caching.html |
0 commit comments