Merge pull request #809 from binxio/persistence-into-data

simplest implementation of kv support on gcs
This commit is contained in:
El RIDO 2021-06-07 12:36:01 +02:00 committed by GitHub
commit f46221e7c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 30 deletions

View file

@ -218,43 +218,48 @@ class GoogleCloudStorage extends AbstractData
}
/**
* Save a value.
*
* @access public
* @param string $value
* @param string $namespace
* @param string $key
* @return bool
* This is the simplest thing that could possibly work.
* will be to tested for runtime performance.
* @inheritDoc
*/
public function setValue($value, $namespace, $key = '')
{
switch ($namespace) {
case 'purge_limiter':
;
break;
case 'salt':
;
break;
case 'traffic_limiter':
;
break;
default:
return false;
break;
$key = 'config/' . $namespace . '/' . $key;
$data = Json::encode($value);
try {
$this->_bucket->upload($data, array(
'name' => $key,
'chunkSize' => 262144,
'predefinedAcl' => 'private',
'metadata' => array(
'content-type' => 'application/json',
'metadata' => array('namespace' => $namespace),
),
));
} catch (Exception $e) {
error_log('failed to set key ' . $key . ' to ' . $this->_bucket->name() . ', ' .
trim(preg_replace('/\s\s+/', ' ', $e->getMessage())));
return false;
}
return true;
}
/**
* Load a value.
*
* @access public
* @param string $namespace
* @param string $key
* @return string
* This is the simplest thing that could possibly work.
* will be to tested for runtime performance.
* @inheritDoc
*/
public function getValue($namespace, $key = '')
{
$key = 'config/' . $namespace . '/' . $key;
try {
$o = $this->_bucket->object($key);
$data = $o->downloadAsString();
return Json::decode($data);
} catch (NotFoundException $e) {
return false;
}
}
/**

View file

@ -31,9 +31,6 @@ class GoogleCloudStorageTest extends PHPUnit_Framework_TestCase
public function setUp()
{
// do not report E_NOTICE as fsouza/fake-gcs-server does not return a `generation` value in the response
// which the Google Cloud Storage PHP library expects.
error_reporting(E_ERROR | E_WARNING | E_PARSE);
ini_set('error_log', stream_get_meta_data(tmpfile())['uri']);
$this->_model = GoogleCloudStorage::getInstance(array(
'bucket' => self::$_bucket->name(),
@ -138,6 +135,28 @@ class GoogleCloudStorageTest extends PHPUnit_Framework_TestCase
$this->assertFalse($this->_model->createComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId(), $comment), 'unable to store broken comment');
$this->assertFalse($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment does still not exist');
}
/**
* @throws Exception
*/
public function testKeyValueStore()
{
$salt = bin2hex(random_bytes(256));
$this->_model->setValue($salt, 'salt', 'master');
$storedSalt = $this->_model->getValue('salt', 'master');
$this->assertEquals($salt, $storedSalt);
$client = hash_hmac('sha512', '127.0.0.1', $salt);
$expire = time();
$this->_model->setValue($expire, 'traffic_limiter', $client);
$storedExpired = $this->_model->getValue('traffic_limiter', $client);
$this->assertEquals($expire, $storedExpired);
$purgeAt = $expire + (15 * 60);
$this->_model->setValue($purgeAt, 'purge_limiter', 'at');
$storedPurgedAt = $this->_model->getValue('purge_limiter', 'at');
$this->assertEquals($purgeAt, $storedPurgedAt);
}
}
/**