Skip to content

Commit 9046206

Browse files
committed
Improve caching strategy
1 parent 6b3b8ae commit 9046206

File tree

10 files changed

+200
-160
lines changed

10 files changed

+200
-160
lines changed

api.php

Lines changed: 98 additions & 78 deletions
Large diffs are not rendered by default.

src/Tqdev/PhpCrudApi/Column/Reflection/ReflectedDatabase.php

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,28 @@ public function __construct(String $name, array $tables)
1313
$this->name = $name;
1414
$this->tables = [];
1515
foreach ($tables as $table) {
16-
$this->tables[$table->getName()] = $table;
16+
$this->tables[$table] = true;
1717
}
1818
}
1919

2020
public static function fromReflection(GenericReflection $reflection): ReflectedDatabase
2121
{
2222
$name = $reflection->getDatabaseName();
2323
$tables = [];
24-
foreach ($reflection->getTables() as $tableName) {
25-
if (in_array($tableName['TABLE_NAME'], $reflection->getIgnoredTables())) {
24+
foreach ($reflection->getTables() as $table) {
25+
$tableName = $table['TABLE_NAME'];
26+
if (in_array($tableName, $reflection->getIgnoredTables())) {
2627
continue;
2728
}
28-
$table = ReflectedTable::fromReflection($reflection, $tableName);
29-
$tables[$table->getName()] = $table;
29+
$tables[$tableName] = true;
3030
}
31-
return new ReflectedDatabase($name, array_values($tables));
31+
return new ReflectedDatabase($name, array_keys($tables));
3232
}
3333

3434
public static function fromJson( /* object */$json): ReflectedDatabase
3535
{
3636
$name = $json->name;
37-
$tables = [];
38-
if (isset($json->tables) && is_array($json->tables)) {
39-
foreach ($json->tables as $table) {
40-
$tables[] = ReflectedTable::fromJson($table);
41-
}
42-
}
37+
$tables = $json->tables;
4338
return new ReflectedDatabase($name, $tables);
4439
}
4540

@@ -53,12 +48,7 @@ public function exists(String $tableName): bool
5348
return isset($this->tables[$tableName]);
5449
}
5550

56-
public function get(String $tableName): ReflectedTable
57-
{
58-
return $this->tables[$tableName];
59-
}
60-
61-
public function getTableNames(): array
51+
public function getTables(): array
6252
{
6353
return array_keys($this->tables);
6454
}
@@ -67,7 +57,7 @@ public function serialize()
6757
{
6858
return [
6959
'name' => $this->name,
70-
'tables' => array_values($this->tables),
60+
'tables' => array_keys($this->tables),
7161
];
7262
}
7363

src/Tqdev/PhpCrudApi/Column/Reflection/ReflectedTable.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ public function __construct(String $name, array $columns)
3737
}
3838
}
3939

40-
public static function fromReflection(GenericReflection $reflection, array $tableResult): ReflectedTable
40+
public static function fromReflection(GenericReflection $reflection, String $name): ReflectedTable
4141
{
42-
$name = $tableResult['TABLE_NAME'];
4342
// set columns
4443
$columns = [];
4544
foreach ($reflection->getTableColumns($name) as $tableColumn) {

src/Tqdev/PhpCrudApi/Column/ReflectionService.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public function __construct(GenericDB $db, Cache $cache, int $ttl)
1919
$this->cache = $cache;
2020
$this->ttl = $ttl;
2121
$this->tables = $this->loadTables(true);
22+
$this->tableCache = [];
2223
}
2324

2425
private function loadTables(bool $useCache): ReflectedDatabase
@@ -34,23 +35,49 @@ private function loadTables(bool $useCache): ReflectedDatabase
3435
return $tables;
3536
}
3637

37-
public function refresh()
38+
private function loadTable(String $name, bool $useCache): ReflectedTable
39+
{
40+
$data = $useCache ? $this->cache->get("ReflectedTable($name)") : '';
41+
if ($data != '') {
42+
$table = ReflectedTable::fromJson(json_decode(gzuncompress($data)));
43+
} else {
44+
$table = ReflectedTable::fromReflection($this->db->reflection(), $name);
45+
$data = gzcompress(json_encode($table, JSON_UNESCAPED_UNICODE));
46+
$this->cache->set("ReflectedTable($name)", $data, $this->ttl);
47+
}
48+
return $table;
49+
}
50+
51+
public function refreshTables()
3852
{
3953
$this->tables = $this->loadTables(false);
4054
}
4155

56+
public function refreshTable(String $tableName)
57+
{
58+
$this->tableCache[$tableName] = $this->loadTable($tableName, false);
59+
}
60+
4261
public function hasTable(String $table): bool
4362
{
4463
return $this->tables->exists($table);
4564
}
4665

4766
public function getTable(String $table): ReflectedTable
4867
{
49-
return $this->tables->get($table);
68+
if (!isset($this->tableCache[$table])) {
69+
$this->tableCache[$table] = $this->loadTable($table, true);
70+
}
71+
return $this->tableCache[$table];
72+
}
73+
74+
public function getTableNames(): array
75+
{
76+
return $this->tables->getTables();
5077
}
5178

52-
public function getDatabase(): ReflectedDatabase
79+
public function getDatabaseName(): String
5380
{
54-
return $this->tables;
81+
return $this->tables->getName();
5582
}
5683
}

src/Tqdev/PhpCrudApi/Controller/ColumnController.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?php
22
namespace Tqdev\PhpCrudApi\Controller;
33

4-
use Tqdev\PhpCrudApi\Record\ErrorCode;
54
use Tqdev\PhpCrudApi\Column\DefinitionService;
65
use Tqdev\PhpCrudApi\Column\ReflectionService;
6+
use Tqdev\PhpCrudApi\Middleware\Router\Router;
7+
use Tqdev\PhpCrudApi\Record\ErrorCode;
78
use Tqdev\PhpCrudApi\Request;
89
use Tqdev\PhpCrudApi\Response;
9-
use Tqdev\PhpCrudApi\Middleware\Router\Router;
1010

1111
class ColumnController
1212
{
@@ -32,7 +32,12 @@ public function __construct(Router $router, Responder $responder, ReflectionServ
3232

3333
public function getDatabase(Request $request): Response
3434
{
35-
$database = $this->reflection->getDatabase();
35+
$name = $this->reflection->getDatabaseName();
36+
$tables = [];
37+
foreach ($this->reflection->getTableNames() as $table) {
38+
$tables[] = $this->reflection->getTable($table);
39+
}
40+
$database = ['name' => $name, 'tables' => $tables];
3641
return $this->responder->success($database);
3742
}
3843

@@ -69,7 +74,7 @@ public function updateTable(Request $request): Response
6974
}
7075
$success = $this->definition->updateTable($tableName, $request->getBody());
7176
if ($success) {
72-
$this->reflection->refresh();
77+
$this->reflection->refreshTables();
7378
}
7479
return $this->responder->success($success);
7580
}
@@ -87,7 +92,7 @@ public function updateColumn(Request $request): Response
8792
}
8893
$success = $this->definition->updateColumn($tableName, $columnName, $request->getBody());
8994
if ($success) {
90-
$this->reflection->refresh();
95+
$this->reflection->refreshTable($tableName);
9196
}
9297
return $this->responder->success($success);
9398
}
@@ -100,7 +105,7 @@ public function addTable(Request $request): Response
100105
}
101106
$success = $this->definition->addTable($request->getBody());
102107
if ($success) {
103-
$this->reflection->refresh();
108+
$this->reflection->refreshTables();
104109
}
105110
return $this->responder->success($success);
106111
}
@@ -118,7 +123,7 @@ public function addColumn(Request $request): Response
118123
}
119124
$success = $this->definition->addColumn($tableName, $request->getBody());
120125
if ($success) {
121-
$this->reflection->refresh();
126+
$this->reflection->refreshTable($tableName);
122127
}
123128
return $this->responder->success($success);
124129
}
@@ -131,7 +136,7 @@ public function removeTable(Request $request): Response
131136
}
132137
$success = $this->definition->removeTable($tableName);
133138
if ($success) {
134-
$this->reflection->refresh();
139+
$this->reflection->refreshTables();
135140
}
136141
return $this->responder->success($success);
137142
}
@@ -149,7 +154,7 @@ public function removeColumn(Request $request): Response
149154
}
150155
$success = $this->definition->removeColumn($tableName, $columnName);
151156
if ($success) {
152-
$this->reflection->refresh();
157+
$this->reflection->refreshTable($tableName);
153158
}
154159
return $this->responder->success($success);
155160
}

src/Tqdev/PhpCrudApi/Middleware/SanitationMiddleware.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ public function handle(Request $request): Response
3737
$path = $request->getPathSegment(1);
3838
$tableName = $request->getPathSegment(2);
3939
$record = $request->getBody();
40-
$database = $this->reflection->getDatabase();
41-
if ($path == 'records' && $database->exists($tableName) && $record !== null) {
42-
$table = $database->get($tableName);
40+
if ($path == 'records' && $this->reflection->hasTable($tableName) && $record !== null) {
41+
$table = $this->reflection->getTable($tableName);
4342
$method = $request->getMethod();
4443
$handler = $this->getProperty('handler', '');
4544
if ($handler !== '') {

src/Tqdev/PhpCrudApi/Middleware/ValidationMiddleware.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
use Tqdev\PhpCrudApi\Column\Reflection\ReflectedTable;
66
use Tqdev\PhpCrudApi\Controller\Responder;
77
use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
8+
use Tqdev\PhpCrudApi\Middleware\Router\Router;
89
use Tqdev\PhpCrudApi\Record\ErrorCode;
910
use Tqdev\PhpCrudApi\Request;
1011
use Tqdev\PhpCrudApi\Response;
11-
use Tqdev\PhpCrudApi\Middleware\Router\Router;
1212

1313
class ValidationMiddleware extends Middleware
1414
{
@@ -45,9 +45,8 @@ public function handle(Request $request): Response
4545
$path = $request->getPathSegment(1);
4646
$tableName = $request->getPathSegment(2);
4747
$record = $request->getBody();
48-
$database = $this->reflection->getDatabase();
49-
if ($path == 'records' && $database->exists($tableName) && $record !== null) {
50-
$table = $database->get($tableName);
48+
if ($path == 'records' && $this->reflection->hasTable($tableName) && $record !== null) {
49+
$table = $this->reflection->getTable($tableName);
5150
$method = $request->getMethod();
5251
$handler = $this->getProperty('handler', '');
5352
if ($handler !== '') {

src/Tqdev/PhpCrudApi/OpenApi/OpenApiService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
class OpenApiService
77
{
8-
private $tables;
8+
private $reflection;
99

1010
public function __construct(ReflectionService $reflection)
1111
{
12-
$this->tables = $reflection->getDatabase();
12+
$this->reflection = $reflection;
1313
}
1414

1515
}

src/Tqdev/PhpCrudApi/Record/RecordService.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class RecordService
99
{
1010
private $db;
11-
private $tables;
11+
private $reflection;
1212
private $columns;
1313
private $joiner;
1414
private $filters;
@@ -18,9 +18,9 @@ class RecordService
1818
public function __construct(GenericDB $db, ReflectionService $reflection)
1919
{
2020
$this->db = $db;
21-
$this->tables = $reflection->getDatabase();
21+
$this->reflection = $reflection;
2222
$this->columns = new ColumnIncluder();
23-
$this->joiner = new RelationJoiner($this->columns);
23+
$this->joiner = new RelationJoiner($reflection, $this->columns);
2424
$this->filters = new FilterInfo();
2525
$this->ordering = new OrderingInfo();
2626
$this->pagination = new PaginationInfo();
@@ -30,14 +30,14 @@ private function sanitizeRecord(String $tableName, /* object */ $record, String
3030
{
3131
$keyset = array_keys((array) $record);
3232
foreach ($keyset as $key) {
33-
if (!$this->tables->get($tableName)->exists($key)) {
33+
if (!$this->reflection->getTable($tableName)->exists($key)) {
3434
unset($record->$key);
3535
}
3636
}
3737
if ($id != '') {
38-
$pk = $this->tables->get($tableName)->getPk();
39-
foreach ($this->tables->get($tableName)->columnNames() as $key) {
40-
$field = $this->tables->get($tableName)->get($key);
38+
$pk = $this->reflection->getTable($tableName)->getPk();
39+
foreach ($this->reflection->getTable($tableName)->columnNames() as $key) {
40+
$field = $this->reflection->getTable($tableName)->get($key);
4141
if ($field->getName() == $pk->getName()) {
4242
unset($record->$key);
4343
}
@@ -47,57 +47,57 @@ private function sanitizeRecord(String $tableName, /* object */ $record, String
4747

4848
public function exists(String $table): bool
4949
{
50-
return $this->tables->exists($table);
50+
return $this->reflection->hasTable($table);
5151
}
5252

5353
public function create(String $tableName, /* object */ $record, array $params)
5454
{
5555
$this->sanitizeRecord($tableName, $record, '');
56-
$table = $this->tables->get($tableName);
56+
$table = $this->reflection->getTable($tableName);
5757
$columnValues = $this->columns->getValues($table, true, $record, $params);
5858
return $this->db->createSingle($table, $columnValues);
5959
}
6060

6161
public function read(String $tableName, String $id, array $params) /*: ?object*/
6262
{
63-
$table = $this->tables->get($tableName);
64-
$this->joiner->addMandatoryColumns($table, $this->tables, $params);
63+
$table = $this->reflection->getTable($tableName);
64+
$this->joiner->addMandatoryColumns($table, $params);
6565
$columnNames = $this->columns->getNames($table, true, $params);
6666
$record = $this->db->selectSingle($table, $columnNames, $id);
6767
if ($record == null) {
6868
return null;
6969
}
7070
$records = array($record);
71-
$this->joiner->addJoins($table, $records, $this->tables, $params, $this->db);
71+
$this->joiner->addJoins($table, $records, $params, $this->db);
7272
return $records[0];
7373
}
7474

7575
public function update(String $tableName, String $id, /* object */ $record, array $params)
7676
{
7777
$this->sanitizeRecord($tableName, $record, $id);
78-
$table = $this->tables->get($tableName);
78+
$table = $this->reflection->getTable($tableName);
7979
$columnValues = $this->columns->getValues($table, true, $record, $params);
8080
return $this->db->updateSingle($table, $columnValues, $id);
8181
}
8282

8383
public function delete(String $tableName, String $id, array $params)
8484
{
85-
$table = $this->tables->get($tableName);
85+
$table = $this->reflection->getTable($tableName);
8686
return $this->db->deleteSingle($table, $id);
8787
}
8888

8989
public function increment(String $tableName, String $id, /* object */ $record, array $params)
9090
{
9191
$this->sanitizeRecord($tableName, $record, $id);
92-
$table = $this->tables->get($tableName);
92+
$table = $this->reflection->getTable($tableName);
9393
$columnValues = $this->columns->getValues($table, true, $record, $params);
9494
return $this->db->incrementSingle($table, $columnValues, $id);
9595
}
9696

9797
public function _list(String $tableName, array $params): ListDocument
9898
{
99-
$table = $this->tables->get($tableName);
100-
$this->joiner->addMandatoryColumns($table, $this->tables, $params);
99+
$table = $this->reflection->getTable($tableName);
100+
$this->joiner->addMandatoryColumns($table, $params);
101101
$columnNames = $this->columns->getNames($table, true, $params);
102102
$condition = $this->filters->getCombinedConditions($table, $params);
103103
$columnOrdering = $this->ordering->getColumnOrdering($table, $params);
@@ -111,7 +111,7 @@ public function _list(String $tableName, array $params): ListDocument
111111
$count = $this->db->selectCount($table, $condition);
112112
}
113113
$records = $this->db->selectAll($table, $columnNames, $condition, $columnOrdering, $offset, $limit);
114-
$this->joiner->addJoins($table, $records, $this->tables, $params, $this->db);
114+
$this->joiner->addJoins($table, $records, $params, $this->db);
115115
return new ListDocument($records, $count);
116116
}
117117
}

0 commit comments

Comments
 (0)