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 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
120 changes: 120 additions & 0 deletions docs/components/cache.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,124 @@
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.


.. 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 - replacing ENV with the environment e.g ``dev`` or ``prod`` - then run the command and/or browse to the site to rebuild.

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');
/** @var CacheItemInterface $item */
$item = $cache->getItem('test_tagged_Item');
$item->set('yesa!!!');
$item->tag(['firstTag', 'secondTag']);
$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']);
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);
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',
'cache_prefix' => 'app',
'cache_lifetime' => 86400
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',
'cache_prefix' => 'app_cache',
'cache_lifetime' => 86400,

Delivered adapters
fakela marked this conversation as resolved.
Show resolved Hide resolved
------------------
.. vale off

- ``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
------------------

The ``cache:clear`` command clears Mautic's cache. Use this command:

.. 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
Sponsor 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