Skip to content

Commit 180fc13

Browse files
committed
Merge pull request symfony#3260 from WouterJ/component_expression_caching
[ExpressionLanguage] Documented Caching Strategies
2 parents d1bbdc4 + 0a59f6d commit 180fc13

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed
+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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

components/expression_language/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ Expression Language
77
introduction
88
syntax
99
extending
10+
caching

components/expression_language/introduction.rst

+6
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,10 @@ PHP type (including objects)::
109109
This will print "Honeycrisp". For more information, see the :doc:`/components/expression_language/syntax`
110110
entry, especially :ref:`component-expression-objects` and :ref:`component-expression-arrays`.
111111

112+
Caching
113+
-------
114+
115+
The component provides some different caching strategies, read more about them
116+
in :doc:`/components/expression_language/caching`.
117+
112118
.. _Packagist: https://packagist.org/packages/symfony/expression-language

components/map.rst.inc

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
* :doc:`/components/expression_language/introduction`
6363
* :doc:`/components/expression_language/syntax`
6464
* :doc:`/components/expression_language/extending`
65+
* :doc:`/components/expression_language/caching`
6566

6667
* **Filesystem**
6768

0 commit comments

Comments
 (0)