Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita9e0e66

Browse files
committed
Added Caching article
1 parent816c06b commita9e0e66

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
Using Parsed and Serialized Expressions
42+
---------------------------------------
43+
44+
Both ``evaluate`` and ``compile`` can handle ``ParsedExpression`` and
45+
``SerializedParsedExpression``::
46+
47+
use Symfony\Component\ExpressionLanguage\ParsedExpression;
48+
// ...
49+
50+
$expression = new ParsedExpression($language->parse('1 + 4'));
51+
52+
echo $language->evaluate($expression); // prints 5
53+
54+
..code-block::php
55+
56+
use Symfony\Component\ExpressionLanguage\SerializedParsedExpression;
57+
// ...
58+
59+
$expression = new SerializedParsedExpression(serialize($language->parse('1 + 4')));
60+
61+
echo $language->evaluate($expression); // prints 5

‎components/expression_language/index.rst

Lines changed: 1 addition & 0 deletions
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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)

[8]ページ先頭

©2009-2025 Movatter.jp