simplest implementation of kv support on gcs
This commit is contained in:
parent
7bdcc2ae15
commit
55efc858b5
2 changed files with 54 additions and 30 deletions
|
@ -218,43 +218,48 @@ class GoogleCloudStorage extends AbstractData
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save a value.
|
* This is the simplest thing that could possibly work.
|
||||||
*
|
* will be to tested for runtime performance.
|
||||||
* @access public
|
* @inheritDoc
|
||||||
* @param string $value
|
|
||||||
* @param string $namespace
|
|
||||||
* @param string $key
|
|
||||||
* @return bool
|
|
||||||
*/
|
*/
|
||||||
public function setValue($value, $namespace, $key = '')
|
public function setValue($value, $namespace, $key = '')
|
||||||
{
|
{
|
||||||
switch ($namespace) {
|
$key = 'config/' . $namespace . '/' . $key;
|
||||||
case 'purge_limiter':
|
$data = Json::encode($value);
|
||||||
;
|
|
||||||
break;
|
try {
|
||||||
case 'salt':
|
$this->_bucket->upload($data, array(
|
||||||
;
|
'name' => $key,
|
||||||
break;
|
'chunkSize' => 262144,
|
||||||
case 'traffic_limiter':
|
'predefinedAcl' => 'private',
|
||||||
;
|
'metadata' => array(
|
||||||
break;
|
'content-type' => 'application/json',
|
||||||
default:
|
'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 false;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load a value.
|
* This is the simplest thing that could possibly work.
|
||||||
*
|
* will be to tested for runtime performance.
|
||||||
* @access public
|
* @inheritDoc
|
||||||
* @param string $namespace
|
|
||||||
* @param string $key
|
|
||||||
* @return string
|
|
||||||
*/
|
*/
|
||||||
public function getValue($namespace, $key = '')
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -31,9 +31,6 @@ class GoogleCloudStorageTest extends PHPUnit_Framework_TestCase
|
||||||
|
|
||||||
public function setUp()
|
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']);
|
ini_set('error_log', stream_get_meta_data(tmpfile())['uri']);
|
||||||
$this->_model = GoogleCloudStorage::getInstance(array(
|
$this->_model = GoogleCloudStorage::getInstance(array(
|
||||||
'bucket' => self::$_bucket->name(),
|
'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->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');
|
$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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue