-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy path10-tenant-aware-cache.php
More file actions
97 lines (82 loc) · 2.53 KB
/
Copy path10-tenant-aware-cache.php
File metadata and controls
97 lines (82 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/**
* Example 10: Tenant-Aware Cache
*
* When enabled, the bundle decorates Symfony's cache to automatically
* prefix keys with the current tenant ID. This ensures cache isolation
* between tenants without any changes to your application code.
*
* Tenant A's "product_list" key becomes "42__product_list"
* Tenant B's "product_list" key becomes "99__product_list"
*/
/*
# config/packages/hakam_multi_tenancy.yaml
hakam_multi_tenancy:
cache:
enabled: true
prefix_separator: '__' # tenant_id + separator + key
*/
namespace App\Service;
use Hakam\MultiTenancyBundle\Context\TenantContextInterface;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Contracts\Cache\CacheInterface;
class ProductCatalogService
{
public function __construct(
private readonly CacheInterface $cache,
private readonly TenantContextInterface $tenantContext,
) {}
/**
* This cache call is automatically tenant-scoped.
* When tenant 42 is active, the key becomes "42__product_catalog".
* When tenant 99 is active, the key becomes "99__product_catalog".
*
* No changes needed in your code — the TenantAwareCacheDecorator
* handles the prefixing transparently.
*/
public function getCatalog(): array
{
return $this->cache->get('product_catalog', function () {
// This expensive computation runs once per tenant
return $this->buildCatalog();
});
}
/**
* Deleting a cache entry also uses the tenant prefix automatically.
*/
public function invalidateCatalog(): void
{
$this->cache->delete('product_catalog');
}
private function buildCatalog(): array
{
// Expensive query — result is cached per-tenant
return ['products' => '...'];
}
}
/**
* If you need to work with the PSR-6 CacheItemPoolInterface directly,
* the same tenant-aware prefixing applies.
*/
class ReportService
{
public function __construct(
private readonly CacheItemPoolInterface $cachePool,
) {}
public function getReport(string $reportId): array
{
$item = $this->cachePool->getItem("report_{$reportId}");
if ($item->isHit()) {
return $item->get();
}
$report = $this->generateReport($reportId);
$item->set($report);
$item->expiresAfter(3600);
$this->cachePool->save($item);
return $report;
}
private function generateReport(string $reportId): array
{
return ['id' => $reportId, 'data' => '...'];
}
}