Skip to content

[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 2 commits into from
Dec 3, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions components/expression_language/caching.rst
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
Copy link
Member

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 on doctrine/cache

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, done!

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
1 change: 1 addition & 0 deletions components/expression_language/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ Expression Language
introduction
syntax
extending
caching
6 changes: 6 additions & 0 deletions components/expression_language/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions components/map.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand Down