Skip to content

Commit

Permalink
IBX-1136: Prevent legacy and symfony clear:cache script from clearing…
Browse files Browse the repository at this point in the history
… SPI cache (#185)

Co-authored-by: Vidar Langseid <[email protected]>
Co-authored-by: Gunnstein Lye <[email protected]>
Co-authored-by: Paweł Niedzielski <[email protected]>
  • Loading branch information
4 people authored Oct 4, 2021
1 parent 58892fc commit 264404b
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 7 deletions.
20 changes: 20 additions & 0 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,23 @@ rewrite "^/var/storage/packages/(.*)" "/var/storage/packages/$1" break;
Last step, if you are on *nix operation system, is to make sure to run
the appropriate command for setting correct folder permissions, you
can find the information you need in [installation guide for eZ Platform](https://doc.ezplatform.com/en/latest/getting_started/install_ez_platform/).

### Clearing of cache

Legacy bridge will by default also clear SPI cache (stored in redis, memcached or disk) when content-view cache is
cleared in legacy. This behaviour can be disabled using the setting:

```
// app/config/ezplatform.yml
ez_publish_legacy:
clear_all_spi_cache_from_legacy: false
```

Legacy bridge will also alter the default behaviour of Symfony's `bin/console cache:clear` command so that SPI cache
is cleared when it is executed. This behaviour can be disabled using the setting:

```
// app/config/ezplatform.yml
ez_publish_legacy:
clear_all_spi_cache_on_symfony_clear_cache: false
```
42 changes: 35 additions & 7 deletions bundle/Cache/PersistenceCachePurger.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ class PersistenceCachePurger implements CacheClearerInterface
*/
protected $allCleared = false;

/**
* @var bool
*/
private $clearAllSPICacheOnSymfonyClearCache;

/**
* @var bool
*/
private $clearAllSPICacheFromLegacy;

/**
* Setups current handler with everything needed.
*
Expand All @@ -65,21 +75,35 @@ class PersistenceCachePurger implements CacheClearerInterface
public function __construct(
TagAwareAdapterInterface $cache,
LocationHandlerInterface $locationHandler,
CacheIdentifierGeneratorInterface $cacheIdentifierGenerator
CacheIdentifierGeneratorInterface $cacheIdentifierGenerator,
bool $clearAllSPICacheOnSymfonyClearCache = true,
bool $clearAllSPICacheFromLegacy = true
) {
$this->cache = $cache;
$this->locationHandler = $locationHandler;
$this->cacheIdentifierGenerator = $cacheIdentifierGenerator;
$this->clearAllSPICacheOnSymfonyClearCache = $clearAllSPICacheOnSymfonyClearCache;
$this->clearAllSPICacheFromLegacy = $clearAllSPICacheFromLegacy;
}

/**
* Clear all persistence cache.
* Clear all persistence cache if that is allowed by config.
*
* In legacy kernel used when user presses clear all cache button in admin interface.
*/
public function all()
{
if ($this->clearAllSPICacheFromLegacy) {
$this->flushSPICache();
}
}

/**
* Clear all persistence cache.
*
* Sets a internal flag 'allCleared' to avoid clearing cache several times
*/
public function all()
private function flushSPICache()
{
if ($this->isSwitchedOff()) {
return;
Expand Down Expand Up @@ -219,9 +243,11 @@ public function contentType($id = null)
}

if ($id === null) {
$this->cache->invalidateTags([
$this->cacheIdentifierGenerator->generateTag(self::TYPE_MAP_IDENTIFIER),
]);
if ($this->clearAllSPICacheFromLegacy) {
$this->cache->invalidateTags([
$this->cacheIdentifierGenerator->generateTag(self::TYPE_MAP_IDENTIFIER),
]);
}
} elseif (is_scalar($id)) {
$this->cache->invalidateTags([
$this->cacheIdentifierGenerator->generateTag(self::TYPE_IDENTIFIER, [$id]),
Expand Down Expand Up @@ -358,6 +384,8 @@ public function user($id = null)
*/
public function clear($cacheDir)
{
$this->all();
if ($this->clearAllSPICacheOnSymfonyClearCache) {
$this->flushSPICache();
}
}
}
2 changes: 2 additions & 0 deletions bundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public function getConfigTreeBuilder()
$rootNode
->children()
->booleanNode('enabled')->defaultTrue()->end()
->booleanNode('clear_all_spi_cache_on_symfony_clear_cache')->defaultTrue()->end()
->booleanNode('clear_all_spi_cache_from_legacy')->defaultTrue()->end()
->scalarNode('root_dir')
->validate()
->ifTrue(
Expand Down
14 changes: 14 additions & 0 deletions bundle/DependencyInjection/EzPublishLegacyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('ezpublish_legacy.root_dir', $config['root_dir']);
}

if (isset($config['clear_all_spi_cache_on_symfony_clear_cache'])) {
$container->setParameter(
'ezpublish_legacy.clear_all_spi_cache_on_symfony_clear_cache',
$config['clear_all_spi_cache_on_symfony_clear_cache']
);
}

if (isset($config['clear_all_spi_cache_from_legacy'])) {
$container->setParameter(
'ezpublish_legacy.clear_all_spi_cache_from_legacy',
$config['clear_all_spi_cache_from_legacy']
);
}

// Templating
$loader->load('templating.yml');

Expand Down
2 changes: 2 additions & 0 deletions bundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ services:
- "@ezpublish.cache_pool"
- "@ezpublish.spi.persistence.cache.locationHandler"
- '@Ibexa\Core\Persistence\Cache\Tag\CacheIdentifierGeneratorInterface'
- '%ezpublish_legacy.clear_all_spi_cache_on_symfony_clear_cache%'
- '%ezpublish_legacy.clear_all_spi_cache_from_legacy%'
tags:
- { name: kernel.cache_clearer }
lazy: true
Expand Down

0 comments on commit 264404b

Please sign in to comment.