Skip to content

Commit 1d2aa1c

Browse files
committed
OXDEV-7768 Add shop identifier to template cache
1 parent 1194701 commit 1d2aa1c

File tree

9 files changed

+233
-32
lines changed

9 files changed

+233
-32
lines changed

CHANGELOG-7.0.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Fixed
66
- Fix skipped backend startup checks [PR-927](https://github.com/OXID-eSales/oxideshop_ce/pull/927)
7+
- Fix sub shops share same template cache
78

89
## v7.0.2 - 2023-11-28
910

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/**
4+
* Copyright © OXID eSales AG. All rights reserved.
5+
* See LICENSE file for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace OxidEsales\EshopCommunity\Internal\Framework\Templating\Cache;
11+
12+
use OxidEsales\EshopCommunity\Internal\Transition\Utility\ContextInterface;
13+
use Symfony\Component\Filesystem\Path;
14+
use Symfony\Component\Filesystem\Filesystem;
15+
16+
class ShopTemplateCacheService implements ShopTemplateCacheServiceInterface
17+
{
18+
public function __construct(
19+
private ContextInterface $context,
20+
private Filesystem $filesystem
21+
) {
22+
}
23+
24+
public function getShopTemplateCacheDirectory(int $shopID): string
25+
{
26+
return Path::join(
27+
$this->context->getCacheDirectory(),
28+
'template_cache',
29+
'shops',
30+
(string) $shopID
31+
);
32+
}
33+
34+
public function invalidateShopTemplateCache(int $shopID): void
35+
{
36+
$templateCacheDirectory = $this->getShopTemplateCacheDirectory($shopID);
37+
38+
if ($this->filesystem->exists($templateCacheDirectory)) {
39+
$this->filesystem->remove($templateCacheDirectory);
40+
}
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/**
4+
* Copyright © OXID eSales AG. All rights reserved.
5+
* See LICENSE file for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace OxidEsales\EshopCommunity\Internal\Framework\Templating\Cache;
11+
12+
interface ShopTemplateCacheServiceInterface
13+
{
14+
public function getShopTemplateCacheDirectory(int $shopID): string;
15+
16+
public function invalidateShopTemplateCache(int $shopID): void;
17+
}

source/Internal/Framework/Templating/Cache/TemplateCacheService.php

+6-15
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,22 @@
99

1010
namespace OxidEsales\EshopCommunity\Internal\Framework\Templating\Cache;
1111

12-
use FilesystemIterator;
13-
use OxidEsales\EshopCommunity\Internal\Transition\Utility\BasicContextInterface;
14-
use RecursiveDirectoryIterator;
15-
use RecursiveIteratorIterator;
16-
use Symfony\Component\Filesystem\Filesystem;
12+
use OxidEsales\EshopCommunity\Internal\Transition\Utility\ContextInterface;
1713

1814
class TemplateCacheService implements TemplateCacheServiceInterface
1915
{
2016
public function __construct(
21-
private BasicContextInterface $basicContext,
22-
private Filesystem $filesystem
17+
private ContextInterface $context,
18+
private ShopTemplateCacheServiceInterface $shopTemplateCacheService
2319
) {
2420
}
2521

2622
public function invalidateTemplateCache(): void
2723
{
28-
$templateCacheDirectory = $this->basicContext->getTemplateCacheDirectory();
24+
$shops = $this->context->getAllShopIds();
2925

30-
if ($this->filesystem->exists($templateCacheDirectory)) {
31-
$recursiveIterator = new RecursiveIteratorIterator(
32-
new RecursiveDirectoryIterator($templateCacheDirectory, FilesystemIterator::SKIP_DOTS),
33-
RecursiveIteratorIterator::SELF_FIRST,
34-
RecursiveIteratorIterator::CATCH_GET_CHILD
35-
);
36-
$this->filesystem->remove($recursiveIterator);
26+
foreach ($shops as $shop) {
27+
$this->shopTemplateCacheService->invalidateShopTemplateCache($shop);
3728
}
3829
}
3930
}

source/Internal/Framework/Templating/services.yaml

+8-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,14 @@ services:
5454
OxidEsales\EshopCommunity\Internal\Framework\Templating\Cache\TemplateCacheServiceInterface:
5555
class: OxidEsales\EshopCommunity\Internal\Framework\Templating\Cache\TemplateCacheService
5656
arguments:
57-
- '@OxidEsales\EshopCommunity\Internal\Transition\Utility\BasicContextInterface'
57+
- '@OxidEsales\EshopCommunity\Internal\Transition\Utility\ContextInterface'
58+
- '@OxidEsales\EshopCommunity\Internal\Framework\Templating\Cache\ShopTemplateCacheServiceInterface'
59+
public: true
60+
61+
OxidEsales\EshopCommunity\Internal\Framework\Templating\Cache\ShopTemplateCacheServiceInterface:
62+
class: OxidEsales\EshopCommunity\Internal\Framework\Templating\Cache\ShopTemplateCacheService
63+
arguments:
64+
- '@OxidEsales\EshopCommunity\Internal\Transition\Utility\ContextInterface'
5865
- '@oxid_esales.symfony.file_system'
5966
public: true
6067

source/Internal/Transition/Utility/BasicContextInterface.php

+3
Original file line numberDiff line numberDiff line change
@@ -136,5 +136,8 @@ public function getCacheDirectory(): string;
136136

137137
public function getModuleCacheDirectory(): string;
138138

139+
/**
140+
* @deprecated Use OxidEsales\Eshop\Internal\Framework\Templating\Cache\ShopTemplateCacheServiceInterface instead
141+
*/
139142
public function getTemplateCacheDirectory(): string;
140143
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/**
4+
* Copyright © OXID eSales AG. All rights reserved.
5+
* See LICENSE file for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace OxidEsales\EshopCommunity\Tests\Integration\Internal\Framework\Templating\Cache;
11+
12+
use OxidEsales\EshopCommunity\Internal\Framework\Templating\Cache\ShopTemplateCacheServiceInterface;
13+
use OxidEsales\EshopCommunity\Tests\ContainerTrait;
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Filesystem\Filesystem;
16+
use Symfony\Component\Filesystem\Path;
17+
18+
final class ShopTemplateCacheServiceTest extends TestCase
19+
{
20+
use ContainerTrait;
21+
22+
private int $shopID = 123;
23+
private string $shopTemplateCachePath;
24+
private Filesystem $filesystem;
25+
private ShopTemplateCacheServiceInterface $shopTemplateCacheService;
26+
27+
protected function setUp(): void
28+
{
29+
$this->filesystem = new Filesystem();
30+
$this->shopTemplateCachePath = $this->get(ShopTemplateCacheServiceInterface::class)
31+
->getShopTemplateCacheDirectory($this->shopID);
32+
33+
$this->clearTemplateCache();
34+
$this->populateTemplateCache();
35+
36+
parent::setUp();
37+
}
38+
39+
public function testInvalidateTemplateCache(): void
40+
{
41+
$this->assertNotCount(0, \glob($this->selectAllCacheFiles()));
42+
43+
$this->get(ShopTemplateCacheServiceInterface::class)->invalidateShopTemplateCache($this->shopID);
44+
45+
self::assertCount(0, \glob($this->selectAllCacheFiles()));
46+
}
47+
48+
private function clearTemplateCache(): void
49+
{
50+
$this->filesystem->remove($this->selectAllCacheFiles());
51+
}
52+
53+
private function selectAllCacheFiles(): string
54+
{
55+
return "$this->shopTemplateCachePath/*";
56+
}
57+
58+
private function populateTemplateCache(): void
59+
{
60+
$this->filesystem->mkdir($this->shopTemplateCachePath);
61+
for ($i = 0; $i < 3; $i++) {
62+
$this->filesystem->touch(
63+
Path::join(
64+
$this->shopTemplateCachePath,
65+
uniqid('template-file-', true)
66+
)
67+
);
68+
}
69+
}
70+
}

tests/Integration/Internal/Framework/Templating/Cache/TemplateCacheServiceTest.php

+31-16
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99

1010
namespace OxidEsales\EshopCommunity\Tests\Integration\Internal\Framework\Templating\Cache;
1111

12+
use OxidEsales\EshopCommunity\Internal\Framework\Templating\Cache\ShopTemplateCacheServiceInterface;
1213
use OxidEsales\EshopCommunity\Internal\Framework\Templating\Cache\TemplateCacheServiceInterface;
13-
use OxidEsales\EshopCommunity\Internal\Transition\Utility\BasicContextInterface;
14+
use OxidEsales\EshopCommunity\Internal\Transition\Utility\ContextInterface;
1415
use OxidEsales\EshopCommunity\Tests\ContainerTrait;
1516
use PHPUnit\Framework\TestCase;
1617
use Symfony\Component\Filesystem\Filesystem;
@@ -20,13 +21,13 @@ final class TemplateCacheServiceTest extends TestCase
2021
{
2122
use ContainerTrait;
2223

23-
private string $templateCachePath;
24+
private array $shopsID;
2425
private Filesystem $filesystem;
2526

2627
protected function setUp(): void
2728
{
2829
$this->filesystem = new Filesystem();
29-
$this->templateCachePath = $this->get(BasicContextInterface::class)->getTemplateCacheDirectory();
30+
$this->shopsID = $this->get(ContextInterface::class)->getAllShopIds();
3031

3132
$this->clearTemplateCache();
3233
$this->populateTemplateCache();
@@ -36,34 +37,48 @@ protected function setUp(): void
3637

3738
public function testInvalidateTemplateCache(): void
3839
{
39-
$this->assertNotCount(0, \glob($this->selectAllCacheFiles()));
40+
$this->assertNotEquals(0, $this->countCacheFiles());
4041

4142
$this->get(TemplateCacheServiceInterface::class)->invalidateTemplateCache();
4243

43-
self::assertCount(0, \glob($this->selectAllCacheFiles()));
44+
self::assertEquals(0, $this->countCacheFiles());
4445
}
4546

4647
private function clearTemplateCache(): void
4748
{
48-
$this->filesystem->remove($this->selectAllCacheFiles());
49+
foreach ($this->shopsID as $shopID) {
50+
$this->filesystem->remove(
51+
$this->get(ShopTemplateCacheServiceInterface::class)
52+
->getShopTemplateCacheDirectory($shopID)
53+
);
54+
}
4955
}
5056

51-
private function selectAllCacheFiles(): string
57+
private function countCacheFiles(): int
5258
{
53-
return "$this->templateCachePath/*";
59+
$files = 0;
60+
foreach ($this->shopsID as $shopID) {
61+
$files += count(\glob($this->get(ShopTemplateCacheServiceInterface::class)
62+
->getShopTemplateCacheDirectory($shopID)));
63+
}
64+
return $files;
5465
}
5566

5667
private function populateTemplateCache(): void
5768
{
5869
$numberOfTestFiles = 3;
59-
$this->filesystem->mkdir($this->templateCachePath);
60-
for ($i = 0; $i < $numberOfTestFiles; $i++) {
61-
$this->filesystem->touch(
62-
Path::join(
63-
$this->templateCachePath,
64-
uniqid('template-file-', true)
65-
)
66-
);
70+
foreach ($this->shopsID as $shopID) {
71+
$templateCachePath = $this->get(ShopTemplateCacheServiceInterface::class)
72+
->getShopTemplateCacheDirectory($shopID);
73+
$this->filesystem->mkdir($templateCachePath);
74+
for ($i = 0; $i < $numberOfTestFiles; $i++) {
75+
$this->filesystem->touch(
76+
Path::join(
77+
$templateCachePath,
78+
uniqid('template-file-' . $shopID, true)
79+
)
80+
);
81+
}
6782
}
6883
}
6984
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/**
4+
* Copyright © OXID eSales AG. All rights reserved.
5+
* See LICENSE file for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace OxidEsales\EshopCommunity\Tests\Unit\Internal\Framework\Templating\Cache;
11+
12+
use OxidEsales\EshopCommunity\Internal\Framework\Templating\Cache\ShopTemplateCacheService;
13+
use OxidEsales\EshopCommunity\Internal\Framework\Templating\Cache\ShopTemplateCacheServiceInterface;
14+
use OxidEsales\EshopCommunity\Internal\Transition\Utility\ContextInterface;
15+
use PHPUnit\Framework\TestCase;
16+
use Symfony\Component\Filesystem\Filesystem;
17+
18+
class ShopTemplateCacheServiceTest extends TestCase
19+
{
20+
private string $cacheDirectory = '/path/to/cache';
21+
private ContextInterface $contextMock;
22+
private Filesystem $fileSystemMock;
23+
private ShopTemplateCacheServiceInterface $shopTemplateCacheService;
24+
25+
protected function setUp(): void
26+
{
27+
parent::setUp();
28+
29+
// Mock the ContextInterface
30+
$this->contextMock = $this->getMockBuilder(ContextInterface::class)->getMock();
31+
$this->fileSystemMock = $this->getMockBuilder(Filesystem::class)->getMock();
32+
33+
$this->contextMock->expects($this->any())
34+
->method('getCacheDirectory')
35+
->willReturn($this->cacheDirectory);
36+
37+
$this->shopTemplateCacheService = new ShopTemplateCacheService($this->contextMock, $this->fileSystemMock);
38+
}
39+
public function testGetShopTemplateCacheDirectory()
40+
{
41+
$shopID = 123;
42+
43+
$shopCachePath = $this->shopTemplateCacheService->getShopTemplateCacheDirectory($shopID);
44+
45+
$this->assertEquals($this->cacheDirectory . '/template_cache/shops/' . $shopID, $shopCachePath);
46+
}
47+
public function testInvalidateShopTemplateCache()
48+
{
49+
$shopID = 123;
50+
51+
$shopCachePath = $this->shopTemplateCacheService->getShopTemplateCacheDirectory($shopID);
52+
53+
$this->assertEquals($this->cacheDirectory . '/template_cache/shops/' . $shopID, $shopCachePath);
54+
}
55+
}

0 commit comments

Comments
 (0)