Merge pull request #5695 from appwrite/fix-cache-delete-bug
cache deletion bug fix
This commit is contained in:
commit
7ec4b2171c
2 changed files with 51 additions and 26 deletions
|
@ -642,11 +642,6 @@ App::post('/v1/storage/buckets/:bucketId/files')
|
||||||
->setContext('bucket', $bucket)
|
->setContext('bucket', $bucket)
|
||||||
;
|
;
|
||||||
|
|
||||||
$deletes
|
|
||||||
->setType(DELETE_TYPE_CACHE_BY_RESOURCE)
|
|
||||||
->setResource('file/' . $file->getId())
|
|
||||||
;
|
|
||||||
|
|
||||||
$metadata = null; // was causing leaks as it was passed by reference
|
$metadata = null; // was causing leaks as it was passed by reference
|
||||||
|
|
||||||
$response
|
$response
|
||||||
|
@ -1263,7 +1258,8 @@ App::put('/v1/storage/buckets/:bucketId/files/:fileId')
|
||||||
->inject('user')
|
->inject('user')
|
||||||
->inject('mode')
|
->inject('mode')
|
||||||
->inject('events')
|
->inject('events')
|
||||||
->action(function (string $bucketId, string $fileId, ?array $permissions, Response $response, Database $dbForProject, Document $user, string $mode, Event $events) {
|
->inject('deletes')
|
||||||
|
->action(function (string $bucketId, string $fileId, ?array $permissions, Response $response, Database $dbForProject, Document $user, string $mode, Event $events, Delete $deletes) {
|
||||||
|
|
||||||
$bucket = Authorization::skip(fn () => $dbForProject->getDocument('buckets', $bucketId));
|
$bucket = Authorization::skip(fn () => $dbForProject->getDocument('buckets', $bucketId));
|
||||||
|
|
||||||
|
@ -1329,6 +1325,11 @@ App::put('/v1/storage/buckets/:bucketId/files/:fileId')
|
||||||
$file = Authorization::skip(fn() => $dbForProject->updateDocument('bucket_' . $bucket->getInternalId(), $fileId, $file));
|
$file = Authorization::skip(fn() => $dbForProject->updateDocument('bucket_' . $bucket->getInternalId(), $fileId, $file));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$deletes
|
||||||
|
->setType(DELETE_TYPE_CACHE_BY_RESOURCE)
|
||||||
|
->setResource('file/' . $file->getId())
|
||||||
|
;
|
||||||
|
|
||||||
$events
|
$events
|
||||||
->setParam('bucketId', $bucket->getId())
|
->setParam('bucketId', $bucket->getId())
|
||||||
->setParam('fileId', $file->getId())
|
->setParam('fileId', $file->getId())
|
||||||
|
|
|
@ -111,10 +111,10 @@ class DeletesV1 extends Worker
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case DELETE_TYPE_CACHE_BY_RESOURCE:
|
case DELETE_TYPE_CACHE_BY_RESOURCE:
|
||||||
$this->deleteCacheByResource($this->args['resource']);
|
$this->deleteCacheByResource($project, $this->args['resource']);
|
||||||
break;
|
break;
|
||||||
case DELETE_TYPE_CACHE_BY_TIMESTAMP:
|
case DELETE_TYPE_CACHE_BY_TIMESTAMP:
|
||||||
$this->deleteCacheByDate();
|
$this->deleteCacheByDate($this->args['datetime']);
|
||||||
break;
|
break;
|
||||||
case DELETE_TYPE_SCHEDULES:
|
case DELETE_TYPE_SCHEDULES:
|
||||||
$this->deleteSchedules($this->args['datetime']);
|
$this->deleteSchedules($this->args['datetime']);
|
||||||
|
@ -162,25 +162,44 @@ class DeletesV1 extends Worker
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param Document $project
|
||||||
* @param string $resource
|
* @param string $resource
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
protected function deleteCacheByResource(string $resource): void
|
protected function deleteCacheByResource(Document $project, string $resource): void
|
||||||
{
|
{
|
||||||
$this->deleteCacheFiles([
|
$dbForProject = $this->getProjectDB($project);
|
||||||
Query::equal('resource', [$resource]),
|
$projectId = $project->getId();
|
||||||
]);
|
$document = $dbForProject->findOne('cache', [Query::equal('resource', [$resource])]);
|
||||||
|
|
||||||
|
if ($document) {
|
||||||
|
$cache = new Cache(
|
||||||
|
new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $projectId)
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->deleteById(
|
||||||
|
$document,
|
||||||
|
$dbForProject,
|
||||||
|
function ($document) use ($cache, $projectId) {
|
||||||
|
$path = APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $projectId . DIRECTORY_SEPARATOR . $document->getId();
|
||||||
|
|
||||||
|
if ($cache->purge($document->getId())) {
|
||||||
|
Console::success('Deleting cache file: ' . $path);
|
||||||
|
} else {
|
||||||
|
Console::error('Failed to delete cache file: ' . $path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function deleteCacheByDate(): void
|
/**
|
||||||
|
* @param string $datetime
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
protected function deleteCacheByDate(string $datetime): void
|
||||||
{
|
{
|
||||||
$this->deleteCacheFiles([
|
$this->deleteForProjectIds(function (Document $project) use ($datetime) {
|
||||||
Query::lessThan('accessedAt', $this->args['datetime']),
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function deleteCacheFiles($query): void
|
|
||||||
{
|
|
||||||
$this->deleteForProjectIds(function (Document $project) use ($query) {
|
|
||||||
|
|
||||||
$projectId = $project->getId();
|
$projectId = $project->getId();
|
||||||
$dbForProject = $this->getProjectDB($project);
|
$dbForProject = $this->getProjectDB($project);
|
||||||
|
@ -188,6 +207,10 @@ class DeletesV1 extends Worker
|
||||||
new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $projectId)
|
new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $projectId)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$query = [
|
||||||
|
Query::lessThan('accessedAt', $datetime),
|
||||||
|
];
|
||||||
|
|
||||||
$this->deleteByGroup(
|
$this->deleteByGroup(
|
||||||
'cache',
|
'cache',
|
||||||
$query,
|
$query,
|
||||||
|
@ -205,9 +228,10 @@ class DeletesV1 extends Worker
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Document $document database document
|
* @param Document $document database document
|
||||||
* @param Document $projectId
|
* @param Document $project
|
||||||
*/
|
*/
|
||||||
protected function deleteDatabase(Document $document, Document $project): void
|
protected function deleteDatabase(Document $document, Document $project): void
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue