Skip to content

Commit

Permalink
Merge pull request #81 from sitegeist/feature/cacheFusionObjectTreeIf…
Browse files Browse the repository at this point in the history
…Enabled

FEATURE: Fusion Object Tree Caching
  • Loading branch information
mficzel authored Jul 20, 2018
2 parents b3c394a + 681e396 commit bf3c0ac
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 0 deletions.
51 changes: 51 additions & 0 deletions Classes/Sitegeist/Monocle/Aspects/FusionCachingAspect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
namespace Sitegeist\Monocle\Aspects;

/**
* This file is part of the Sitegeist.Monocle package
*
* (c) 2016
* Martin Ficzel <[email protected]>
* Wilhelm Behncke <[email protected]>
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

use Neos\Flow\Annotations as Flow;
use Neos\Flow\Aop\JoinPointInterface;
use Neos\Cache\Frontend\VariableFrontend;

/**
* @Flow\Scope("singleton")
* @Flow\Aspect
*/
class FusionCachingAspect
{
/**
* @Flow\Inject
* @var VariableFrontend
*/
protected $fusionCache;

/**
* @Flow\Around("setting(Sitegeist.Monocle.fusion.enableObjectTreeCache) && method(Sitegeist\Monocle\Fusion\FusionService->getMergedFusionObjectTreeForSitePackage())")
* @param JoinPointInterface $joinPoint The current join point
* @return mixed
*/
public function cacheGetMergedFusionObjectTree(JoinPointInterface $joinPoint)
{
$siteResourcesPackageKey = $joinPoint->getMethodArgument('siteResourcesPackageKey');
$cacheIdentifier = str_replace('.', '_', $siteResourcesPackageKey);

if ($this->fusionCache->has($cacheIdentifier)) {
$fusionObjectTree = $this->fusionCache->get($cacheIdentifier);
} else {
$fusionObjectTree = $joinPoint->getAdviceChain()->proceed($joinPoint);
$this->fusionCache->set($cacheIdentifier, $fusionObjectTree);
}

return $fusionObjectTree;
}
}
79 changes: 79 additions & 0 deletions Classes/Sitegeist/Monocle/Package.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
namespace Sitegeist\Monocle;

/**
* This file is part of the Sitegeist.Monocle package
*
* (c) 2016
* Martin Ficzel <[email protected]>
* Wilhelm Behncke <[email protected]>
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

use Neos\Flow\Cache\CacheManager;
use Neos\Flow\Core\Booting\Sequence;
use Neos\Flow\Core\Bootstrap;
use Neos\Flow\Monitor\FileMonitor;
use Neos\Flow\Package\FlowPackageInterface;
use Neos\Flow\Package\Package as BasePackage;
use Neos\Flow\Package\PackageManager;
use Neos\Flow\Package\PackageManagerInterface;

/**
* The Fluid Package
*
*/
class Package extends BasePackage
{

/**
* Invokes custom PHP code directly after the package manager has been initialized.
*
* @param Bootstrap $bootstrap The current bootstrap
* @return void
*/
public function boot(Bootstrap $bootstrap)
{
$dispatcher = $bootstrap->getSignalSlotDispatcher();

$context = $bootstrap->getContext();
if (!$context->isProduction()) {
$dispatcher->connect(Sequence::class, 'afterInvokeStep', function ($step) use ($bootstrap, $dispatcher) {
if ($step->getIdentifier() === 'neos.flow:systemfilemonitor') {
$templateFileMonitor = FileMonitor::createFileMonitorAtBoot('Sitegeist_Monocle_Fusion_Files', $bootstrap);
$packageManager = $bootstrap->getEarlyInstance(PackageManagerInterface::class);
/**
* @var PackageManager $packageKey
* @var FlowPackageInterface $package
*/
foreach ($packageManager->getFlowPackages() as $packageKey => $package) {
$templatesPath = $package->getResourcesPath() . 'Private/Fusion';
if (is_dir($templatesPath)) {
$templateFileMonitor->monitorDirectory($templatesPath);
}
}

$templateFileMonitor->detectChanges();
$templateFileMonitor->shutdownObject();
}
});
}

$flushTemplates = function ($identifier, $changedFiles) use ($bootstrap) {
if ($identifier !== 'Sitegeist_Monocle_Fusion_Files') {
return;
}

if ($changedFiles === []) {
return;
}

$templateCache = $bootstrap->getObjectManager()->get(CacheManager::class)->getCache('Sitegeist_Monocle_Fusion');
$templateCache->flush();
};
$dispatcher->connect(FileMonitor::class, 'filesHaveChanged', $flushTemplates);
}
}
3 changes: 3 additions & 0 deletions Configuration/Caches.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Sitegeist_Monocle_Fusion:
frontend: Neos\Cache\Frontend\VariableFrontend
backend: Neos\Cache\Backend\FileBackend
10 changes: 10 additions & 0 deletions Configuration/Objects.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,13 @@ Sitegeist\Monocle\Fusion\FusionView:
properties:
fallbackView:
object: Neos\FluidAdaptor\View\TemplateView

Sitegeist\Monocle\Aspects\FusionCachingAspect:
properties:
fusionCache:
object:
factoryObjectName: Neos\Flow\Cache\CacheManager
factoryMethodName: getCache
arguments:
1:
value: Sitegeist_Monocle_Fusion
3 changes: 3 additions & 0 deletions Configuration/Settings.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

Sitegeist:
Monocle:
fusion:
enableObjectTreeCache: true

packages: { }

ui:
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,14 @@ Sitegeist:
height: 1000
```

### Fusion Object Tree Caching

Monocle will cache the fusion code for every site package. To invalidate this cache
the the Fusion directories of all packages are monitored and changes trigger the flushing
of the fusion-cache.

The setting `Sitegeist.Monocle.fusion.enableObjectTreeCache` enables the caching in Monocle by default.

### Routes

The monocle Routes are included automatically via Settings.
Expand Down

0 comments on commit bf3c0ac

Please sign in to comment.