implementing key/value store of Persistance in Database storage

This commit is contained in:
El RIDO
2021-06-09 07:47:40 +02:00
parent 7901ec74a7
commit a203e6322b
8 changed files with 86 additions and 65 deletions

View File

@@ -28,6 +28,15 @@ abstract class AbstractData
*/
protected static $_instance = null;
/**
* cache for the traffic limiter
*
* @access private
* @static
* @var array
*/
protected static $_traffic_limiter_cache = array();
/**
* Enforce singleton, disable constructor
*
@@ -138,7 +147,16 @@ abstract class AbstractData
* @param int $time
* @return void
*/
abstract public function purgeValues($namespace, $time);
public function purgeValues($namespace, $time)
{
if ($namespace === 'traffic_limiter') {
foreach (self::$_traffic_limiter_cache as $key => $last_access) {
if ($last_access <= $time) {
unset(self::$_traffic_limiter_cache[$key]);
}
}
}
}
/**
* Save a value.

View File

@@ -423,23 +423,6 @@ class Database extends AbstractData
);
}
/**
* Purge outdated entries.
*
* @access public
* @param string $namespace
* @param int $time
* @return void
*/
public function purgeValues($namespace, $time)
{
switch ($namespace) {
case 'traffic_limiter':
;
break;
}
}
/**
* Save a value.
*
@@ -451,20 +434,19 @@ class Database extends AbstractData
*/
public function setValue($value, $namespace, $key = '')
{
switch ($namespace) {
case 'purge_limiter':
;
break;
case 'salt':
;
break;
case 'traffic_limiter':
;
break;
default:
if ($namespace === 'traffic_limiter') {
self::$_traffic_limiter_cache[$key] = $value;
try {
$value = Json::encode(self::$_traffic_limiter_cache);
} catch (Exception $e) {
return false;
break;
}
}
return self::_exec(
'UPDATE ' . self::_sanitizeIdentifier('config') .
' SET value = ? WHERE id = ?',
array($value, strtoupper($namespace))
);
}
/**
@@ -477,7 +459,36 @@ class Database extends AbstractData
*/
public function getValue($namespace, $key = '')
{
$configKey = strtoupper($namespace);
$value = $this->_getConfig($configKey);
if ($value === '') {
// initialize the row, so that setValue can rely on UPDATE queries
self::_exec(
'INSERT INTO ' . self::_sanitizeIdentifier('config') .
' VALUES(?,?)',
array($configKey, '')
);
// migrate filesystem based salt into database
$file = 'data' . DIRECTORY_SEPARATOR . 'salt.php';
if ($namespace === 'salt' && is_readable($file)) {
$value = Filesystem::getInstance(array('dir' => 'data'))->getValue('salt');
$this->setValue($value, 'salt');
@unlink($file);
return $value;
}
}
if ($value && $namespace === 'traffic_limiter') {
try {
self::$_traffic_limiter_cache = Json::decode($value);
} catch (Exception $e) {
self::$_traffic_limiter_cache = array();
}
if (array_key_exists($key, self::$_traffic_limiter_cache)) {
return self::$_traffic_limiter_cache[$key];
}
}
return (string) $value;
}
/**
@@ -629,7 +640,7 @@ class Database extends AbstractData
'SELECT value FROM ' . self::_sanitizeIdentifier('config') .
' WHERE id = ?', array($key), true
);
return $row['value'];
return $row ? $row['value']: '';
}
/**

View File

@@ -38,15 +38,6 @@ class Filesystem extends AbstractData
*/
private static $_path = 'data';
/**
* cache for the traffic limiter
*
* @access private
* @static
* @var array
*/
private static $_traffic_limiter_cache = array();
/**
* get instance of singleton
*
@@ -249,27 +240,6 @@ class Filesystem extends AbstractData
);
}
/**
* Purge outdated entries.
*
* @access public
* @param string $namespace
* @param int $time
* @return void
*/
public function purgeValues($namespace, $time)
{
switch ($namespace) {
case 'traffic_limiter':
foreach (self::$_traffic_limiter_cache as $key => $last_access) {
if ($last_access <= $time) {
unset(self::$_traffic_limiter_cache[$key]);
}
}
break;
}
}
/**
* Save a value.
*

View File

@@ -227,10 +227,11 @@ class GoogleCloudStorage extends AbstractData
*/
public function purgeValues($namespace, $time)
{
switch ($namespace) {
case 'traffic_limiter':
;
break;
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.
}
}