Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public function update(array $fields)
WHERE id =:id");

$query->execute($fields);
$this->clearCache();
}

public function create(array $fields)
Expand All @@ -168,13 +169,15 @@ public function create(array $fields)
VALUES ({$stmtString[1]})");

$query->execute($fields);
$this->clearCache();
}

public function delete($id)
{
$table = $this->table;
$query = $this->dsn->prepare("DELETE FROM {$table} WHERE id = :id");
$query->execute([':id' => $id]);
$this->clearCache();
}

/**
Expand Down Expand Up @@ -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";
Expand All @@ -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);
}
}
}
}
Loading