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
@@ -11,23 +11,34 @@
|
||||
namespace IPS\Data\Store;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use IPS\Data\Cache;
|
||||
use IPS\Data\Cache\None;
|
||||
use IPS\Data\Store;
|
||||
use IPS\Db;
|
||||
use IPS\Db\Exception;
|
||||
use IPS\Session\Front;
|
||||
use UnderflowException;
|
||||
use function defined;
|
||||
use function in_array;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Database Storage Class
|
||||
*/
|
||||
class _Database extends \IPS\Data\Store
|
||||
class Database extends Store
|
||||
{
|
||||
/**
|
||||
* Server supports this method?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function supported()
|
||||
public static function supported(): bool
|
||||
{
|
||||
return TRUE;
|
||||
}
|
||||
@@ -35,17 +46,17 @@ class _Database extends \IPS\Data\Store
|
||||
/**
|
||||
* @brief Always needed Store keys
|
||||
*/
|
||||
public $initLoad = array( 'cacheKeys', 'settings', 'storageConfigurations', 'themes', 'languages', 'groups', 'applications', 'modules', 'widgets', 'furl', 'javascript_map', 'metaTags', 'bannedIpAddresses', 'license_data', 'furl_configuration', 'rssFeeds', 'frontNavigation', 'globalStreamIds', 'profileSteps' );
|
||||
public array $initLoad = array( 'cacheKeys', 'settings', 'storageConfigurations', 'themes', 'languages', 'groups', 'applications', 'modules', 'widgets', 'furl', 'javascript_map', 'metaTags', 'bannedIpAddresses', 'license_data', 'furl_configuration', 'rssFeeds', 'frontNavigation', 'globalStreamIds', 'profileSteps' );
|
||||
|
||||
/**
|
||||
* @brief Have we done the intitial load?
|
||||
*/
|
||||
protected $doneInitLoad = FALSE;
|
||||
protected bool $doneInitLoad = FALSE;
|
||||
|
||||
/**
|
||||
* @brief Cache
|
||||
*/
|
||||
protected static $cache = array();
|
||||
protected static array $cache = array();
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
@@ -56,7 +67,7 @@ class _Database extends \IPS\Data\Store
|
||||
public function __construct()
|
||||
{
|
||||
/* When caching is enabled, we only need cacheKeys, the rest will come from the cache */
|
||||
if ( !\IPS\Data\Cache::i() instanceof \IPS\Data\Cache\None )
|
||||
if ( !Cache::i() instanceof None )
|
||||
{
|
||||
$this->initLoad = array( 'cacheKeys' );
|
||||
}
|
||||
@@ -70,13 +81,16 @@ class _Database extends \IPS\Data\Store
|
||||
$this->initLoad[] = 'acpNotifications';
|
||||
$this->initLoad[] = 'emoticons';
|
||||
|
||||
if ( \IPS\Session\Front::loggedIn() )
|
||||
{
|
||||
$this->initLoad[] = 'administrators';
|
||||
$this->initLoad[] = 'moderators';
|
||||
$this->initLoad[] = 'group_promotions';
|
||||
$this->initLoad[] = 'promoters';
|
||||
}
|
||||
$this->initLoad[] = 'administrators';
|
||||
$this->initLoad[] = 'moderators';
|
||||
$this->initLoad[] = 'group_promotions';
|
||||
$this->initLoad[] = 'promoters';
|
||||
|
||||
/* things from other apps */
|
||||
$this->initLoad[] = 'nexusPackagesWithReviews';
|
||||
$this->initLoad[] = 'cms_menu';
|
||||
$this->initLoad[] = 'cms_databases';
|
||||
$this->initLoad[] = 'pages_page_urls';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,9 +101,9 @@ class _Database extends \IPS\Data\Store
|
||||
* @param array $keys Keys
|
||||
* @return void
|
||||
*/
|
||||
public function loadIntoMemory( array $keys )
|
||||
public function loadIntoMemory( array $keys ) : void
|
||||
{
|
||||
foreach ( \IPS\Db::i()->select( '*', 'core_store', \IPS\Db::i()->in( 'store_key', $keys ) ) as $row )
|
||||
foreach ( Db::i()->select( '*', 'core_store', Db::i()->in( 'store_key', $keys ) ) as $row )
|
||||
{
|
||||
static::$cache[ $row['store_key'] ] = $row['store_value'];
|
||||
}
|
||||
@@ -98,12 +112,12 @@ class _Database extends \IPS\Data\Store
|
||||
/**
|
||||
* Abstract Method: Get
|
||||
*
|
||||
* @param string $key Key
|
||||
* @param string $key Key
|
||||
* @return string Value from the datastore
|
||||
*/
|
||||
public function get( $key )
|
||||
public function get( string $key ) : string
|
||||
{
|
||||
if ( !$this->doneInitLoad and \in_array( $key, $this->initLoad ) )
|
||||
if ( !$this->doneInitLoad and in_array( $key, $this->initLoad ) )
|
||||
{
|
||||
$this->loadIntoMemory( $this->initLoad );
|
||||
$this->doneInitLoad = TRUE;
|
||||
@@ -113,11 +127,11 @@ class _Database extends \IPS\Data\Store
|
||||
{
|
||||
try
|
||||
{
|
||||
static::$cache[ $key ] = \IPS\Db::i()->select( 'store_value', 'core_store', array( 'store_key=?', $key ) )->first();
|
||||
static::$cache[ $key ] = Db::i()->select( 'store_value', 'core_store', array( 'store_key=?', $key ) )->first();
|
||||
}
|
||||
catch ( \IPS\Db\Exception $e )
|
||||
catch ( Exception $e )
|
||||
{
|
||||
throw new \UnderflowException;
|
||||
throw new UnderflowException;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,13 +141,13 @@ class _Database extends \IPS\Data\Store
|
||||
/**
|
||||
* Abstract Method: Set
|
||||
*
|
||||
* @param string $key Key
|
||||
* @param string $value Value
|
||||
* @param string $key Key
|
||||
* @param string $value Value
|
||||
* @return bool
|
||||
*/
|
||||
public function set( $key, $value )
|
||||
public function set( string $key, string $value ): bool
|
||||
{
|
||||
\IPS\Db::i()->replace( 'core_store', array(
|
||||
Db::i()->replace( 'core_store', array(
|
||||
'store_key' => $key,
|
||||
'store_value' => $value
|
||||
) );
|
||||
@@ -146,10 +160,10 @@ class _Database extends \IPS\Data\Store
|
||||
/**
|
||||
* Abstract Method: Exists?
|
||||
*
|
||||
* @param string $key Key
|
||||
* @param string $key Key
|
||||
* @return bool
|
||||
*/
|
||||
public function exists( $key )
|
||||
public function exists( string $key ): bool
|
||||
{
|
||||
if ( isset( static::$cache[ $key ] ) )
|
||||
{
|
||||
@@ -162,7 +176,7 @@ class _Database extends \IPS\Data\Store
|
||||
$this->get( $key );
|
||||
return TRUE;
|
||||
}
|
||||
catch ( \UnderflowException $e )
|
||||
catch ( UnderflowException $e )
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
@@ -175,31 +189,31 @@ class _Database extends \IPS\Data\Store
|
||||
* @param string $key Key
|
||||
* @return bool
|
||||
*/
|
||||
public function delete( $key )
|
||||
public function delete( string $key ): bool
|
||||
{
|
||||
if( isset( static::$cache[ $key ] ) )
|
||||
{
|
||||
unset( static::$cache[ $key ] );
|
||||
}
|
||||
|
||||
\IPS\Db::i()->delete( 'core_store', array( 'store_key=?', $key ) );
|
||||
Db::i()->delete( 'core_store', array( 'store_key=?', $key ) );
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract Method: Clear All Caches
|
||||
*
|
||||
* @param NULL|string $exclude Key to exclude (keep)
|
||||
* @param string|null $exclude Key to exclude (keep)
|
||||
* @return void
|
||||
*/
|
||||
public function clearAll( $exclude=NULL )
|
||||
public function clearAll( string $exclude=NULL ) : void
|
||||
{
|
||||
$where = array();
|
||||
if( $exclude !== NULL )
|
||||
{
|
||||
$where[] = array( 'store_key != ?', $exclude );
|
||||
}
|
||||
\IPS\Db::i()->delete( 'core_store', $where );
|
||||
Db::i()->delete( 'core_store', $where );
|
||||
|
||||
foreach( static::$cache as $key => $value )
|
||||
{
|
||||
|
||||
@@ -11,16 +11,20 @@
|
||||
namespace IPS\Data\Store;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use RuntimeException;
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cache Exception Class
|
||||
*/
|
||||
class _Exception extends \RuntimeException
|
||||
class Exception extends RuntimeException
|
||||
{
|
||||
|
||||
}
|
||||
@@ -11,23 +11,38 @@
|
||||
namespace IPS\Data\Store;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use DirectoryIterator;
|
||||
use DomainException;
|
||||
use IPS\Data\Store;
|
||||
use IPS\Helpers\Form\Text;
|
||||
use IPS\Request;
|
||||
use ParseError;
|
||||
use UnderflowException;
|
||||
use function defined;
|
||||
use function file_put_contents;
|
||||
use function function_exists;
|
||||
use const IPS\IPS_FILE_PERMISSION;
|
||||
use const IPS\IPS_FOLDER_PERMISSION;
|
||||
use const IPS\SUITE_UNIQUE_KEY;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* File System Storage Class
|
||||
*/
|
||||
class _FileSystem extends \IPS\Data\Store
|
||||
class FileSystem extends Store
|
||||
{
|
||||
/**
|
||||
* Server supports this method?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function supported()
|
||||
public static function supported(): bool
|
||||
{
|
||||
return TRUE;
|
||||
}
|
||||
@@ -38,23 +53,23 @@ class _FileSystem extends \IPS\Data\Store
|
||||
* @param array $configuration Existing settings
|
||||
* @return array \IPS\Helpers\Form\FormAbstract elements
|
||||
*/
|
||||
public static function configuration( $configuration )
|
||||
public static function configuration( array $configuration ): array
|
||||
{
|
||||
return array(
|
||||
'path' => new \IPS\Helpers\Form\Text( 'datastore_filesystem_path', ( isset( $configuration['path'] ) ) ? rtrim( str_replace( '{root}', \IPS\ROOT_PATH, $configuration['path'] ), '/' ) : \IPS\ROOT_PATH . '/datastore', FALSE, array(), function( $val )
|
||||
'path' => new Text( 'datastore_filesystem_path', ( isset( $configuration['path'] ) ) ? rtrim( str_replace( '{root}', \IPS\ROOT_PATH, $configuration['path'] ), '/' ) : \IPS\ROOT_PATH . '/datastore', FALSE, array(), function( $val )
|
||||
{
|
||||
if ( \IPS\Request::i()->datastore_method === 'FileSystem' )
|
||||
if ( Request::i()->datastore_method === 'FileSystem' )
|
||||
{
|
||||
if ( !is_dir( $val ) and is_writable( $val ) )
|
||||
{
|
||||
mkdir( $val );
|
||||
chmod( $val, \IPS\IPS_FOLDER_PERMISSION );
|
||||
\file_put_contents( $val . '/index.html', '' );
|
||||
chmod( $val, IPS_FOLDER_PERMISSION );
|
||||
file_put_contents( $val . '/index.html', '' );
|
||||
}
|
||||
|
||||
if ( !is_dir( $val ) or !is_writable( $val ) )
|
||||
{
|
||||
throw new \DomainException( 'datastore_filesystem_path_err' );
|
||||
throw new DomainException( 'datastore_filesystem_path_err' );
|
||||
}
|
||||
}
|
||||
} )
|
||||
@@ -64,7 +79,7 @@ class _FileSystem extends \IPS\Data\Store
|
||||
/**
|
||||
* @brief Storage Path
|
||||
*/
|
||||
public $_path;
|
||||
public string $_path;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
@@ -72,7 +87,7 @@ class _FileSystem extends \IPS\Data\Store
|
||||
* @param array $configuration Configuration
|
||||
* @return void
|
||||
*/
|
||||
public function __construct( $configuration )
|
||||
public function __construct( array $configuration )
|
||||
{
|
||||
$this->_path = rtrim( str_replace( '{root}', \IPS\ROOT_PATH, $configuration['path'] ), '/' );
|
||||
|
||||
@@ -86,7 +101,7 @@ class _FileSystem extends \IPS\Data\Store
|
||||
/**
|
||||
* @brief Cache
|
||||
*/
|
||||
protected static $cache = array();
|
||||
protected static array $cache = array();
|
||||
|
||||
/**
|
||||
* Abstract Method: Get
|
||||
@@ -94,24 +109,24 @@ class _FileSystem extends \IPS\Data\Store
|
||||
* @param string $key Key
|
||||
* @return string Value from the _datastore
|
||||
*/
|
||||
public function get( $key )
|
||||
public function get( string $key ): string
|
||||
{
|
||||
if ( !isset( static::$cache[ $key ] ) )
|
||||
{
|
||||
if ( @filesize( $this->_path . '/' . $key . '.' . \IPS\SUITE_UNIQUE_KEY . '.php' ) )
|
||||
if ( @filesize( $this->_path . '/' . $key . '.' . SUITE_UNIQUE_KEY . '.php' ) )
|
||||
{
|
||||
try
|
||||
{
|
||||
static::$cache[ $key ] = require( $this->_path . '/' . $key . '.' . \IPS\SUITE_UNIQUE_KEY . '.php' );
|
||||
static::$cache[ $key ] = require( $this->_path . '/' . $key . '.' . SUITE_UNIQUE_KEY . '.php' );
|
||||
}
|
||||
catch ( \ParseError $e )
|
||||
catch ( ParseError $e )
|
||||
{
|
||||
throw new \UnderflowException;
|
||||
throw new UnderflowException;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \UnderflowException;
|
||||
throw new UnderflowException;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,11 +136,11 @@ class _FileSystem extends \IPS\Data\Store
|
||||
/**
|
||||
* Abstract Method: Set
|
||||
*
|
||||
* @param string $key Key
|
||||
* @param string $value Value
|
||||
* @param string $key Key
|
||||
* @param string $value Value
|
||||
* @return bool
|
||||
*/
|
||||
public function set( $key, $value )
|
||||
public function set( string $key, string $value ): bool
|
||||
{
|
||||
$contents = <<<CONTENTS
|
||||
<?php
|
||||
@@ -136,24 +151,24 @@ VALUE;
|
||||
|
||||
CONTENTS;
|
||||
|
||||
$result = (bool) @\file_put_contents( $this->_path . '/' . $key . '.' . \IPS\SUITE_UNIQUE_KEY . '.php', $contents, LOCK_EX );
|
||||
$result = (bool) @file_put_contents( $this->_path . '/' . $key . '.' . SUITE_UNIQUE_KEY . '.php', $contents, LOCK_EX );
|
||||
|
||||
/* Sometimes LOCK_EX is unavailable and throws file_put_contents(): Exclusive locks are not supported for this stream.
|
||||
While we would prefer an exclusive lock, it would be better to write the file if possible. */
|
||||
if( !$result )
|
||||
{
|
||||
@unlink( $this->_path . '/' . $key . '.' . \IPS\SUITE_UNIQUE_KEY . '.php' );
|
||||
$result = (bool) @\file_put_contents( $this->_path . '/' . $key . '.' . \IPS\SUITE_UNIQUE_KEY . '.php', $contents );
|
||||
@unlink( $this->_path . '/' . $key . '.' . SUITE_UNIQUE_KEY . '.php' );
|
||||
$result = (bool) @file_put_contents( $this->_path . '/' . $key . '.' . SUITE_UNIQUE_KEY . '.php', $contents );
|
||||
}
|
||||
|
||||
@chmod( $this->_path . '/' . $key . '.' . \IPS\SUITE_UNIQUE_KEY . '.php', \IPS\IPS_FILE_PERMISSION );
|
||||
@chmod( $this->_path . '/' . $key . '.' . SUITE_UNIQUE_KEY . '.php', IPS_FILE_PERMISSION );
|
||||
|
||||
static::$cache[ $key ] = $value;
|
||||
|
||||
/* Clear zend opcache if enabled */
|
||||
if ( \function_exists( 'opcache_invalidate' ) )
|
||||
if ( function_exists( 'opcache_invalidate' ) )
|
||||
{
|
||||
@opcache_invalidate( $this->_path . '/' . $key . '.' . \IPS\SUITE_UNIQUE_KEY . '.php' );
|
||||
@opcache_invalidate( $this->_path . '/' . $key . '.' . SUITE_UNIQUE_KEY . '.php' );
|
||||
}
|
||||
|
||||
return $result;
|
||||
@@ -165,7 +180,7 @@ CONTENTS;
|
||||
* @param string $key Key
|
||||
* @return bool
|
||||
*/
|
||||
public function exists( $key )
|
||||
public function exists( string $key ): bool
|
||||
{
|
||||
if( isset( static::$cache[ $key ] ) )
|
||||
{
|
||||
@@ -178,7 +193,7 @@ CONTENTS;
|
||||
$this->get( $key );
|
||||
return TRUE;
|
||||
}
|
||||
catch ( \UnderflowException $e )
|
||||
catch ( UnderflowException $e )
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
@@ -191,12 +206,12 @@ CONTENTS;
|
||||
* @param string $key Key
|
||||
* @return bool
|
||||
*/
|
||||
public function delete( $key )
|
||||
public function delete( string $key ): bool
|
||||
{
|
||||
$return = false;
|
||||
if ( file_exists( $this->_path . '/' . $key . '.' . \IPS\SUITE_UNIQUE_KEY . '.php' ) )
|
||||
if ( file_exists( $this->_path . '/' . $key . '.' . SUITE_UNIQUE_KEY . '.php' ) )
|
||||
{
|
||||
$return = @unlink( $this->_path . '/' . $key . '.' . \IPS\SUITE_UNIQUE_KEY . '.php' );
|
||||
$return = @unlink( $this->_path . '/' . $key . '.' . SUITE_UNIQUE_KEY . '.php' );
|
||||
}
|
||||
|
||||
if( array_key_exists( $key, static::$cache ) )
|
||||
@@ -205,9 +220,9 @@ CONTENTS;
|
||||
}
|
||||
|
||||
/* Clear zend opcache if enabled */
|
||||
if ( \function_exists( 'opcache_invalidate' ) )
|
||||
if ( function_exists( 'opcache_invalidate' ) )
|
||||
{
|
||||
@opcache_invalidate( $this->_path . '/' . $key . '.' . \IPS\SUITE_UNIQUE_KEY . '.php' );
|
||||
@opcache_invalidate( $this->_path . '/' . $key . '.' . SUITE_UNIQUE_KEY . '.php' );
|
||||
}
|
||||
|
||||
return $return;
|
||||
@@ -219,9 +234,9 @@ CONTENTS;
|
||||
* @param NULL|string $exclude Key to exclude (keep)
|
||||
* @return void
|
||||
*/
|
||||
public function clearAll( $exclude=NULL )
|
||||
public function clearAll( string $exclude=NULL ) : void
|
||||
{
|
||||
foreach ( new \DirectoryIterator( $this->_path ) as $file )
|
||||
foreach ( new DirectoryIterator( $this->_path ) as $file )
|
||||
{
|
||||
if ( !$file->isDot() and ( mb_substr( $file, -9 ) === '.ipsstore' or ( mb_substr( $file, -4 ) === '.php' and $file != $exclude . '.php' ) ) )
|
||||
{
|
||||
@@ -240,7 +255,7 @@ CONTENTS;
|
||||
/* Clear zend opcache if enabled - we call reset here since we're wiping multiple files
|
||||
and since this gets called as part of a general 'rebuild everything', a reset is
|
||||
probably a good idea anyway */
|
||||
if ( \function_exists( 'opcache_reset' ) )
|
||||
if ( function_exists( 'opcache_reset' ) )
|
||||
{
|
||||
@opcache_reset();
|
||||
}
|
||||
|
||||
+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