Skip to content
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

add cache docs #172

Merged
merged 18 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
108 changes: 108 additions & 0 deletions docs/components/cache.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,112 @@
Cache
#####

Symfony makes heavy use of a filesystem cache. When developing for Mautic, clearing the cache is a regular occurrence. By default, Mautic instances have the cache located in ``var/cache/ENV`` where ``ENV`` is the environment currently accessed (``dev`` or ``prod``). To rebuild the cache, delete the relevant ``ENV`` folder within the cache directory, or run the Symfony command ``php bin/console cache:clear --env=ENV``. If a specific environment isn't passed to the command via ``--env=ENV``, Mautic uses the ``dev`` environment by default.
fakela marked this conversation as resolved.
Show resolved Hide resolved


.. vale off

In the ``dev`` environment, Mautic doesn't cache translations, views, and assets. However, changes to these files require clearing the cache for them to take effect in the ``prod`` environment. Changes to Mautic config files, Symfony config files, etc., require clearing of the cache regardless of the environment.

.. vale on

The typical rule of thumb is, if Mautic isn't acting as you expect after making changes, try clearing your cache. If you get ``class could not be found`` or ``cannot redeclare class`` errors when using the ``cache:clear`` command, manually delete the ``var/cache/ENV`` folder then run the command and/or browse to the site to rebuild.
fakela marked this conversation as resolved.
Show resolved Hide resolved

Cache bundle
************

Enables PSR-6 and PSR-16 caching. Check :xref:`Symfony Cache Component`
RCheesley marked this conversation as resolved.
Show resolved Hide resolved

Namespace versus tag
====================

This bundle introduces tags to the cache. All its adapters are fully tag aware which makes the use of namespace obsolete for daily use.

Previously, if you wanted to keep control on cache section and didn't want to hold the index of all keys to clear, you would have to use namespace.

The main disadvantage of this approach is that Mautic creates a new adapter for each namespace.

From Symfony 3.4, the cache uses tag-aware adapters. If you want to clear all records related to your Bundle or Component, you just need to tag them.

.. code-block:: php
fakela marked this conversation as resolved.
Show resolved Hide resolved
/** @var CacheProvider $cache */
$cache = $this->get('mautic.cache.provider');

Check failure on line 33 in docs/components/cache.rst

View workflow job for this annotation

GitHub Actions / prose

[vale] reported by reviewdog 🐶 [Vale.Terms] Use 'Mautic' instead of 'mautic'. Raw Output: {"message": "[Vale.Terms] Use 'Mautic' instead of 'mautic'.", "location": {"path": "docs/components/cache.rst", "range": {"start": {"line": 33, "column": 26}}}, "severity": "ERROR"}
/** @var CacheItemInterface $item */
$item = $cache->getItem('test_tagged_Item');
$item->set('yesa!!!');
$item->tag(['firstTag', 'secondTag']);

Check failure on line 37 in docs/components/cache.rst

View workflow job for this annotation

GitHub Actions / prose

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'secondTag'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'secondTag'?", "location": {"path": "docs/components/cache.rst", "range": {"start": {"line": 37, "column": 30}}}, "severity": "ERROR"}
$item->expiresAfter(20000);
fakela marked this conversation as resolved.
Show resolved Hide resolved
All you need to do now is to clear all tagged items:

.. code-block:: php
fakela marked this conversation as resolved.
Show resolved Hide resolved
$cache->invalidateTags(['firstTag']);
RCheesley marked this conversation as resolved.
Show resolved Hide resolved
Pools clearing
==============

Removing cache items
--------------------

Cache Pools include methods to delete a cache item, some of them, or all of them. The most common is ``Psr\\Cache\\CacheItemPoolInterface::deleteItem``, which deletes the cache item identified by the given key.

.. code-block:: php
fakela marked this conversation as resolved.
Show resolved Hide resolved
$isDeleted = $cache->deleteItem('user_'.$userId);

Check failure on line 52 in docs/components/cache.rst

View workflow job for this annotation

GitHub Actions / prose

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'isDeleted'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'isDeleted'?", "location": {"path": "docs/components/cache.rst", "range": {"start": {"line": 52, "column": 6}}}, "severity": "ERROR"}
RCheesley marked this conversation as resolved.
Show resolved Hide resolved
Use the ``Psr\\Cache\\CacheItemPoolInterface::deleteItems`` method to delete several cache items simultaneously - it returns true only if all the items have been deleted, even when any or some of them don't exist.

Configuration
-------------

Plugins come preconfigured to utilize filesystem caching.

These are the default settings:

.. code-block:: php
fakela marked this conversation as resolved.
Show resolved Hide resolved
'cache_adapter' => 'mautic.cache.adapter.filesystem',

Check failure on line 63 in docs/components/cache.rst

View workflow job for this annotation

GitHub Actions / prose

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'cache_adapter'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'cache_adapter'?", "location": {"path": "docs/components/cache.rst", "range": {"start": {"line": 63, "column": 6}}}, "severity": "ERROR"}

Check failure on line 63 in docs/components/cache.rst

View workflow job for this annotation

GitHub Actions / prose

[vale] reported by reviewdog 🐶 [Vale.Terms] Use 'Mautic' instead of 'mautic'. Raw Output: {"message": "[Vale.Terms] Use 'Mautic' instead of 'mautic'.", "location": {"path": "docs/components/cache.rst", "range": {"start": {"line": 63, "column": 25}}}, "severity": "ERROR"}
'cache_prefix' => 'app',

Check failure on line 64 in docs/components/cache.rst

View workflow job for this annotation

GitHub Actions / prose

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'cache_prefix'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'cache_prefix'?", "location": {"path": "docs/components/cache.rst", "range": {"start": {"line": 64, "column": 6}}}, "severity": "ERROR"}
'cache_lifetime' => 86400

Check failure on line 65 in docs/components/cache.rst

View workflow job for this annotation

GitHub Actions / prose

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'cache_lifetime'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'cache_lifetime'?", "location": {"path": "docs/components/cache.rst", "range": {"start": {"line": 65, "column": 6}}}, "severity": "ERROR"}
They can be overridden in ``local.php`` like this:

.. code-block:: php
fakela marked this conversation as resolved.
Show resolved Hide resolved
'cache_adapter' => 'mautic.cache.adapter.redis',

Check failure on line 69 in docs/components/cache.rst

View workflow job for this annotation

GitHub Actions / prose

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'cache_adapter'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'cache_adapter'?", "location": {"path": "docs/components/cache.rst", "range": {"start": {"line": 69, "column": 6}}}, "severity": "ERROR"}

Check failure on line 69 in docs/components/cache.rst

View workflow job for this annotation

GitHub Actions / prose

[vale] reported by reviewdog 🐶 [Vale.Terms] Use 'Mautic' instead of 'mautic'. Raw Output: {"message": "[Vale.Terms] Use 'Mautic' instead of 'mautic'.", "location": {"path": "docs/components/cache.rst", "range": {"start": {"line": 69, "column": 26}}}, "severity": "ERROR"}
'cache_prefix' => 'app_cache',
'cache_lifetime' => 86400,
Delivered adapters
fakela marked this conversation as resolved.
Show resolved Hide resolved
------------------

- ``mautic.cache.adapter.filesystem``
- ``mautic.cache.adapter.memcached``

.. code-block:: php
'memcached' => [
'servers' => ['memcached://localhost'],
'options' => [
'compression' => true,
'libketama_compatible' => true,
'serializer' => 'igbinary',
],
],
fakela marked this conversation as resolved.
Show resolved Hide resolved
- ``mautic.cache.adapter.redis``

Redis configuration in ``local.php``:

.. code-block:: php
fakela marked this conversation as resolved.
Show resolved Hide resolved
'redis' => [
'dsn' => 'redis://localhost',
'options' => [
'lazy' => false,
'persistent' => 0,
'persistent_id' => null,
'timeout' => 30,
'read_timeout' => 0,
'retry_interval' => 0,
],
],
fakela marked this conversation as resolved.
Show resolved Hide resolved
In order to use another adapter, just set it up as a service.

Clearing the cache
------------------

When the ``cache:clear`` command is run, Mautic's cache is cleared. The cache can be cleared by running:
fakela marked this conversation as resolved.
Show resolved Hide resolved

.. code-block:: bash
fakela marked this conversation as resolved.
Show resolved Hide resolved
bin/console mautic:cache:clear

7 changes: 7 additions & 0 deletions docs/links/symfony_cache_component.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from . import link

link_name = "Symfony Cache Component"
link_text = "Symfony Cache Component"
link_url = "https://symfony.com/doc/current/components/cache.html"

link.xref_links.update({link_name: (link_text, link_url)})
Copy link
Contributor

Choose a reason for hiding this comment

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

no new line

Copy link
Member

Choose a reason for hiding this comment

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

These files are created automatically using the make link command, so I think we are OK to leave as they are :)

Loading