-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[ExpressionLanguage] Documented Caching Strategies #3260
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
.. 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); | ||
|
||
.. 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 | ||
--------------------------------------- | ||
|
||
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 | ||
|
||
.. _DoctrineBridge: https://github.com/symfony/DoctrineBridge | ||
.. _`doctrine cache library`: http://docs.doctrine-project.org/projects/doctrine-common/en/latest/reference/caching.html |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ Expression Language | |
introduction | ||
syntax | ||
extending | ||
caching |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should add a note mentionning that the
symfony/doctrine-bridge
package provides an implementation of the ParserCacheInterface based ondoctrine/cache
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, done!