Skip to content

Commit

Permalink
Bust cache on store
Browse files Browse the repository at this point in the history
  • Loading branch information
daftspunk committed Feb 6, 2022
1 parent 95729e3 commit 265b20d
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/Halcyon/Datasource/DbDatasource.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,11 @@ public function insert(string $dirName, string $fileName, string $extension, str
{
$path = $this->makeFilePath($dirName, $fileName, $extension);

// Check for an existing record
if ($this->getQuery()->where('path', $path)->count() > 0) {
throw (new FileExistsException())->setInvalidPath($path);
}

// Check for a deleted record, update it if it exists instead
// Update a trashed record
if ($this->getQuery(false)->where('path', $path)->first()) {
return $this->update($dirName, $fileName, $extension, $content);
}
Expand Down Expand Up @@ -208,9 +207,10 @@ public function insert(string $dirName, string $fileName, string $extension, str
*/
$this->fireEvent('halcyon.datasource.db.beforeInsert', [&$record]);

// Get a raw query without filters applied to it
$this->getBaseQuery()->insert($record);

$this->flushCache();

return $record['file_size'];
}
catch (Exception $ex) {
Expand All @@ -235,7 +235,6 @@ public function update(string $dirName, string $fileName, string $extension, str

$oldPath = $this->makeFilePath($dirName, $fileName, $extension);

// Update the existing record
try {
$fileSize = mb_strlen($content, '8bit');

Expand Down Expand Up @@ -263,6 +262,8 @@ public function update(string $dirName, string $fileName, string $extension, str

$this->getQuery(false)->where('path', $oldPath)->update($data);

$this->flushCache();

return $fileSize;
}
catch (Exception $ex) {
Expand All @@ -276,18 +277,18 @@ public function update(string $dirName, string $fileName, string $extension, str
public function delete(string $dirName, string $fileName, string $extension): bool
{
try {
// Get the existing record
$path = $this->makeFilePath($dirName, $fileName, $extension);
$recordQuery = $this->getQuery()->where('path', $path);

// Attempt to delete the existing record
if ($this->forceDeleting) {
$result = $recordQuery->delete();
}
else {
$result = $recordQuery->update(['deleted_at' => Carbon::now()->toDateTimeString()]);
}

$this->flushCache();

return (bool) $result;
}
catch (Exception $ex) {
Expand Down Expand Up @@ -370,4 +371,13 @@ protected function makeFilePath(string $dirName, string $fileName, string $exten
{
return $dirName . '/' . $fileName . '.' . $extension;
}

/**
* flushCache
*/
protected function flushCache()
{
unset(self::$pathCache[$this->source]);
unset(self::$mtimeCache[$this->source]);
}
}

0 comments on commit 265b20d

Please sign in to comment.