added purgeValues to GCS

This commit is contained in:
Mark van Holsteijn 2021-06-09 22:27:34 +02:00
parent a203e6322b
commit 1232717334
2 changed files with 42 additions and 17 deletions

View file

@ -2,6 +2,7 @@
namespace PrivateBin\Data;
use DateTime;
use Exception;
use Google\Cloud\Core\Exception\NotFoundException;
use Google\Cloud\Storage\StorageClient;
@ -9,6 +10,8 @@ use PrivateBin\Json;
class GoogleCloudStorage extends AbstractData
{
const DATETIME_FORMAT = 'Y-m-d\TH:i:s.u\Z';
/**
* returns a Google Cloud Storage data backend.
*
@ -218,20 +221,32 @@ class GoogleCloudStorage extends AbstractData
}
/**
* Purge outdated entries.
*
* @access public
* @param string $namespace
* @param int $time
* @return void
* @inheritDoc
*/
public function purgeValues($namespace, $time)
{
if ($namespace === 'traffic_limiter') {
// TODO implement purging of keys in namespace that are <= $time
// if GCS has no easy way to iterate all keys, consider using the
// self::$_traffic_limiter_cache in a similar way as the other
// implementations.
$prefix = 'config/' . $namespace . '/';
try {
foreach ($this->_bucket->objects(array('prefix' => $prefix)) as $object) {
$info = $object->info();
$timeCreated = false;
if (key_exists('timeCreated', $info)) {
$timeCreated = DateTime::createFromFormat(GoogleCloudStorage::DATETIME_FORMAT, $info['timeCreated']);
}
if ($timeCreated && ($timeCreated->getTimestamp() < $time)) {
try {
$object->delete();
} catch (NotFoundException $e) {
// deleted by another instance.
}
} else {
if (!$timeCreated) {
error_log('failed to parse create timestamp ' . $info['timeCreated'] . ' of object ' . $object->name());
}
}
}
} catch (NotFoundException $e) {
// no objects in the bucket yet
}
}