From f2a7bbe7a53798ee55c70823253781fc56a5dd9a Mon Sep 17 00:00:00 2001 From: Jagepard Date: Wed, 18 Jun 2025 10:54:06 +0300 Subject: [PATCH] add clearCache --- src/Repository.php | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Repository.php b/src/Repository.php index b26f6a1..cf9d00d 100755 --- a/src/Repository.php +++ b/src/Repository.php @@ -156,6 +156,7 @@ public function update(array $fields) WHERE id =:id"); $query->execute($fields); + $this->clearCache(); } public function create(array $fields) @@ -168,6 +169,7 @@ public function create(array $fields) VALUES ({$stmtString[1]})"); $query->execute($fields); + $this->clearCache(); } public function delete($id) @@ -175,6 +177,7 @@ public function delete($id) $table = $this->table; $query = $this->dsn->prepare("DELETE FROM {$table} WHERE id = :id"); $query->execute([':id' => $id]); + $this->clearCache(); } /** @@ -308,7 +311,7 @@ public function toggle() } } - public function qCache(array $params, $cacheTime = null) + public function cache(array $params, $cacheTime = null) { $directory = dirname(__DIR__, 4) . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'database'; $file = "$directory/$params[0].json"; @@ -328,4 +331,27 @@ public function qCache(array $params, $cacheTime = null) return $data; } + + public function clearCache(string $type = 'database') + { + $baseDir = dirname(__DIR__, 4) . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR; + + if (!in_array($type, ['database', 'view', 'all'])) { + return; + } + + if ($type === 'all') { + $this->clearCache('database'); + $this->clearCache('view'); + return; + } + + $directory = $baseDir . $type; + + if (is_dir($directory)) { + foreach (glob("$directory/*.json") as $file) { + if (is_file($file)) unlink($file); + } + } + } }