-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathrun-dependency.php
More file actions
92 lines (71 loc) · 3.33 KB
/
Copy pathrun-dependency.php
File metadata and controls
92 lines (71 loc) · 3.33 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
<?php
declare(strict_types=1);
/**
* Cache Dependency Demo
*
* This script demonstrates cache dependency logging to help understand:
* - Cache hit/miss operations
* - Dependency registration (depends-on)
* - Cascade invalidation (invalidate-etag)
*
* Resources used (from tests/Fake/fake-app):
* - LevelOne -> LevelTwo -> LevelThree (3-level dependency chain)
* - ParentA, ParentB -> ChildC (multiple parents depend on same child)
*/
use BEAR\QueryRepository\FakeEtagPoolModule;
use BEAR\QueryRepository\ModuleFactory;
use BEAR\QueryRepository\QueryRepositoryInterface;
use BEAR\Resource\ResourceInterface;
use BEAR\Resource\Uri;
use Koriym\SemanticLogger\SemanticLoggerInterface;
use Koriym\SemanticLogger\Stree\RenderConfig;
use Koriym\SemanticLogger\Stree\TreeRenderer;
use Ray\Di\Injector;
require dirname(__DIR__) . '/vendor/autoload.php';
// Scenario descriptions (for humans)
echo <<<'SCENARIOS'
=== Cache Dependency Demo ===
This demo executes the following scenarios:
1. Initial access to level-one (3-level chain)
- LevelOne embeds LevelTwo embeds LevelThree
- All three will be cache-miss, dependencies registered
2. Re-access level-one
- Should be cache-hit
3. Purge level-three (grandchild)
- Should cascade invalidate level-two and level-one
4. Re-access level-one after purge
- All three should be cache-miss (regenerated)
5. Access ParentA and ParentB
- Both embed ChildC (shared dependency)
6. Purge child-c
- Should invalidate both ParentA and ParentB
7. Re-access both parents after purge
- Both should be cache-miss (regenerated)
=== Executing... ===
SCENARIOS;
$namespace = 'FakeVendor\HelloWorld';
$injector = new Injector(
new FakeEtagPoolModule(ModuleFactory::getInstance($namespace)),
__DIR__ . '/tmp',
);
$resource = $injector->getInstance(ResourceInterface::class);
$repository = $injector->getInstance(QueryRepositoryInterface::class);
$logger = $injector->getInstance(SemanticLoggerInterface::class);
// Execute scenarios. Embedded child GETs nest under their parent GET, so the
// log's open/close tree IS the embed/dependency tree (no reconstruction).
$resource->get('page://self/dep/level-one'); // 1. Initial access (cache-miss chain)
$resource->get('page://self/dep/level-one'); // 2. Re-access (cache-hit)
$repository->purge(new Uri('page://self/dep/level-three')); // 3. Purge grandchild (cascade)
$resource->get('page://self/dep/level-one'); // 4. Re-access after purge (rebuilt)
$resource->get('page://self/dep/parent-a'); // 5a. Access ParentA
$resource->get('page://self/dep/parent-b'); // 5b. Access ParentB
$repository->purge(new Uri('page://self/dep/child-c')); // 6. Purge shared child
$resource->get('page://self/dep/parent-a'); // 7a. Re-access ParentA
$resource->get('page://self/dep/parent-b'); // 7b. Re-access ParentB
$log = $logger->flush();
// Human/AI-readable tree (open = embed scope, close = hit/miss, events = saves/invalidations)
echo "=== Cache Log Tree ===" . PHP_EOL;
echo (new TreeRenderer())->render($log->toArray(), new RenderConfig(true, 0.0, 1000, true)) . PHP_EOL;
// Machine-readable, schema-validated JSON (also: `vendor/bin/stree <file>`)
echo PHP_EOL . "=== Cache Log JSON ===" . PHP_EOL;
echo json_encode($log, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL;