Version 5.0.0 beta 1
This commit is contained in:
1 parent
25ddeb65d6
commit
15c7beabc5
6736 files changed
+627902
-497943
No files matched your search
+53
-45
@@ -11,31 +11,38 @@
|
||||
namespace IPS\Data\Store;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use IPS\Data\Store;
|
||||
use IPS\Redis as RedisClass;
|
||||
use RedisException;
|
||||
use UnderflowException;
|
||||
use function defined;
|
||||
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Redis Storage Class
|
||||
*/
|
||||
class _Redis extends \IPS\Data\Store
|
||||
class Redis extends Store
|
||||
{
|
||||
/**
|
||||
* Server supports this method?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function supported()
|
||||
public static function supported(): bool
|
||||
{
|
||||
return class_exists('Redis');
|
||||
return class_exists('\\Redis');
|
||||
}
|
||||
|
||||
/**
|
||||
* Redis key
|
||||
*/
|
||||
protected $_redisKey;
|
||||
protected string|false $_redisKey;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
@@ -43,21 +50,23 @@ class _Redis extends \IPS\Data\Store
|
||||
* @param array $configuration Configuration to use
|
||||
* @return void
|
||||
*/
|
||||
public function __construct( $configuration )
|
||||
public function __construct( array $configuration )
|
||||
{
|
||||
try
|
||||
{
|
||||
$writeConnection = \IPS\Redis::i()->connection('write');
|
||||
$readConnection = \IPS\Redis::i()->connection('read');
|
||||
$writeConnection = RedisClass::i()->connection('write');
|
||||
$readConnection = RedisClass::i()->connection('read');
|
||||
|
||||
if( !$writeConnection OR !$readConnection )
|
||||
{
|
||||
throw new \RedisException;
|
||||
throw new RedisException;
|
||||
}
|
||||
|
||||
$this->_redisKey = false;
|
||||
}
|
||||
catch( \RedisException $e )
|
||||
catch( RedisException $e )
|
||||
{
|
||||
throw new \IPS\Data\Store\Exception;
|
||||
throw new Exception;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,16 +75,16 @@ class _Redis extends \IPS\Data\Store
|
||||
*
|
||||
* @return string|FALSE Value from the _datastore; FALSE if key doesn't exist
|
||||
*/
|
||||
protected function _getRedisKey()
|
||||
protected function _getRedisKey(): string|FALSE
|
||||
{
|
||||
if ( !$this->_redisKey )
|
||||
{
|
||||
/* Last access ensures that the data is not stale if we fail back to MySQL and then go back to Redis later */
|
||||
if ( !( $this->_redisKey = \IPS\Redis::i()->get( 'redisKey_store' ) ) OR ! \IPS\Redis::i()->get( 'redisStore_lastAccess' ) )
|
||||
if ( !( $this->_redisKey = RedisClass::i()->get( 'redisKey_store' ) ) OR ! RedisClass::i()->get( 'redisStore_lastAccess' ) )
|
||||
{
|
||||
$this->_redisKey = md5( mt_rand() );
|
||||
\IPS\Redis::i()->setex( 'redisKey_store', 604800, $this->_redisKey );
|
||||
\IPS\Redis::i()->setex( 'redisStore_lastAccess', ( 3 * 3600 ), time() );
|
||||
RedisClass::i()->setex( 'redisKey_store', 604800, $this->_redisKey );
|
||||
RedisClass::i()->setex( 'redisStore_lastAccess', ( 3 * 3600 ), time() );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,12 +94,12 @@ class _Redis extends \IPS\Data\Store
|
||||
/**
|
||||
* @brief Cache
|
||||
*/
|
||||
protected static $cache = array();
|
||||
protected static array $cache = array();
|
||||
|
||||
/**
|
||||
* @brief Already updated lastAccess?
|
||||
*/
|
||||
protected static $updatedLastAccess = FALSE;
|
||||
protected static bool $updatedLastAccess = FALSE;
|
||||
|
||||
/**
|
||||
* Abstract Method: Get
|
||||
@@ -98,7 +107,7 @@ class _Redis extends \IPS\Data\Store
|
||||
* @param string $key Key
|
||||
* @return string|FALSE Value from the _datastore; FALSE if key doesn't exist
|
||||
*/
|
||||
public function get( $key )
|
||||
public function get( string $key ): string|FALSE
|
||||
{
|
||||
if( array_key_exists( $key, static::$cache ) )
|
||||
{
|
||||
@@ -110,31 +119,31 @@ class _Redis extends \IPS\Data\Store
|
||||
/* Set the last access time */
|
||||
if ( static::$updatedLastAccess === FALSE )
|
||||
{
|
||||
\IPS\Redis::i()->setex( 'redisStore_lastAccess', ( 3 * 3600 ), time() );
|
||||
RedisClass::i()->setex( 'redisStore_lastAccess', ( 3 * 3600 ), time() );
|
||||
static::$updatedLastAccess = TRUE;
|
||||
}
|
||||
|
||||
$return = \IPS\Redis::i()->get( $this->_getRedisKey() . '_' . $key );
|
||||
$return = RedisClass::i()->get( $this->_getRedisKey() . '_' . $key );
|
||||
|
||||
if ( $return !== FALSE AND $decoded = \IPS\Redis::i()->decode( $return ) )
|
||||
if ( $return !== FALSE AND $decoded = RedisClass::i()->decode( $return ) )
|
||||
{
|
||||
static::$cache[ $key ] = $decoded;
|
||||
return static::$cache[ $key ];
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \UnderflowException;
|
||||
throw new UnderflowException;
|
||||
}
|
||||
}
|
||||
catch( \RedisException $e )
|
||||
catch( RedisException $e )
|
||||
{
|
||||
/* Do not reset the connection if the decode failed */
|
||||
if ( $e->getMessage() !== 'DECODE_ERROR' )
|
||||
{
|
||||
\IPS\Redis::i()->resetConnection( $e );
|
||||
RedisClass::i()->resetConnection( $e );
|
||||
}
|
||||
|
||||
throw new \UnderflowException;
|
||||
throw new UnderflowException;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,15 +154,15 @@ class _Redis extends \IPS\Data\Store
|
||||
* @param string $value Value
|
||||
* @return bool
|
||||
*/
|
||||
public function set( $key, $value )
|
||||
public function set( string $key, string $value ): bool
|
||||
{
|
||||
try
|
||||
{
|
||||
return (bool) \IPS\Redis::i()->setex( $this->_getRedisKey() . '_' . $key, 604800, \IPS\Redis::i()->encode( $value ) );
|
||||
return RedisClass::i()->setex( $this->_getRedisKey() . '_' . $key, 604800, RedisClass::i()->encode( $value ) );
|
||||
}
|
||||
catch( \RedisException $e )
|
||||
catch( RedisException $e )
|
||||
{
|
||||
\IPS\Redis::i()->resetConnection( $e );
|
||||
RedisClass::i()->resetConnection( $e );
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
@@ -165,19 +174,19 @@ class _Redis extends \IPS\Data\Store
|
||||
* @param string $key Key
|
||||
* @return bool
|
||||
*/
|
||||
public function exists( $key )
|
||||
public function exists( string $key ): bool
|
||||
{
|
||||
if( array_key_exists( $key, static::$cache ) )
|
||||
{
|
||||
return ( static::$cache[ $key ] === FALSE ) ? FALSE : TRUE;
|
||||
return !( ( static::$cache[$key] === FALSE ) );
|
||||
}
|
||||
|
||||
/* We do a get instead of an exists() check because it will cause the cache value to be fetched and cached inline, saving another call to the server */
|
||||
try
|
||||
{
|
||||
return ( $this->get( $key ) === FALSE ) ? FALSE : TRUE;
|
||||
return !( ( $this->get( $key ) === FALSE ) );
|
||||
}
|
||||
catch ( \UnderflowException $e )
|
||||
catch ( UnderflowException $e )
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
@@ -189,16 +198,16 @@ class _Redis extends \IPS\Data\Store
|
||||
* @param string $key Key
|
||||
* @return bool
|
||||
*/
|
||||
public function delete( $key )
|
||||
public function delete( string $key ): bool
|
||||
{
|
||||
try
|
||||
{
|
||||
unset( static::$cache[ $key ] );
|
||||
return (bool) \IPS\Redis::i()->del( $this->_getRedisKey() . '_' . $key );
|
||||
return (bool) RedisClass::i()->del( $this->_getRedisKey() . '_' . $key );
|
||||
}
|
||||
catch( \RedisException $e )
|
||||
catch( RedisException $e )
|
||||
{
|
||||
\IPS\Redis::i()->resetConnection( $e );
|
||||
RedisClass::i()->resetConnection( $e );
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
@@ -208,17 +217,16 @@ class _Redis extends \IPS\Data\Store
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function clearAll()
|
||||
public function clearAll() : void
|
||||
{
|
||||
try
|
||||
{
|
||||
$this->_redisKey = md5( mt_rand() );
|
||||
\IPS\Redis::i()->setex( 'redisKey_store', 604800, $this->_redisKey );
|
||||
RedisClass::i()->setex( 'redisKey_store', 604800, $this->_redisKey );
|
||||
}
|
||||
catch( \RedisException $e )
|
||||
catch( RedisException $e )
|
||||
{
|
||||
\IPS\Redis::i()->resetConnection( $e );
|
||||
return FALSE;
|
||||
RedisClass::i()->resetConnection( $e );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -228,8 +236,8 @@ class _Redis extends \IPS\Data\Store
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function test()
|
||||
public function test(): bool
|
||||
{
|
||||
return \IPS\Redis::i()->test();
|
||||
return RedisClass::i()->test();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user