Version 5.0.0 beta 1

This commit is contained in:
Neo committed 2025-12-19 16:27:35 -08:00
1 parent 25ddeb65d6
commit 15c7beabc5
6736 files changed
+627902 -497943

No files matched your search

+65 -51
View File
@@ -11,38 +11,57 @@
namespace IPS\Data\Cache;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use DomainException;
use IPS\Data\Cache;
use IPS\DateTime;
use IPS\Helpers\Form\Number;
use IPS\Helpers\Form\Password;
use IPS\Helpers\Form\Text;
use IPS\Redis as RedisClass;
use IPS\Request;
use RedisException;
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 Cache Class
*/
class _Redis extends \IPS\Data\Cache
class Redis extends Cache
{
/**
* Redis key
*/
protected string|false $_redisKey;
/**
* Constructor
*
* @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\Cache\Exception;
throw new Exception;
}
}
@@ -51,7 +70,7 @@ class _Redis extends \IPS\Data\Cache
*
* @return bool
*/
public static function supported()
public static function supported(): bool
{
return class_exists('Redis');
}
@@ -61,7 +80,7 @@ class _Redis extends \IPS\Data\Cache
*
* @return boolean
*/
public function checkKeys()
public function checkKeys(): bool
{
return false;
}
@@ -72,45 +91,40 @@ class _Redis extends \IPS\Data\Cache
* @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(
'server' => new \IPS\Helpers\Form\Text( 'server_host', isset( $configuration['server'] ) ? $configuration['server'] : '', FALSE, array( 'placeholder' => '127.0.0.1' ), function( $val )
'server' => new Text( 'server_host', $configuration['server'] ?? '', FALSE, array( 'placeholder' => '127.0.0.1' ), function( $val )
{
if ( \IPS\Request::i()->cache_method === 'Redis' and empty( $val ) )
if ( Request::i()->cache_method === 'Redis' and empty( $val ) )
{
throw new \DomainException( 'datastore_redis_servers_err' );
throw new DomainException( 'datastore_redis_servers_err' );
}
}, NULL, NULL, 'redis_host' ),
'port' => new \IPS\Helpers\Form\Number( 'server_port', isset( $configuration['port'] ) ? $configuration['port'] : NULL, FALSE, array( 'placeholder' => '6379' ), function( $val )
'port' => new Number( 'server_port', $configuration['port'] ?? NULL, FALSE, array( 'placeholder' => '6379' ), function( $val )
{
if ( \IPS\Request::i()->cache_method === 'Redis' AND $val AND ( $val < 0 OR $val > 65535 ) )
if ( Request::i()->cache_method === 'Redis' AND $val AND ( $val < 0 OR $val > 65535 ) )
{
throw new \DomainException( 'datastore_redis_servers_err' );
throw new DomainException( 'datastore_redis_servers_err' );
}
}, NULL, NULL, 'redis_port' ),
'password' => new \IPS\Helpers\Form\Password( 'server_password', isset( $configuration['password'] ) ? $configuration['password'] : '', FALSE, array(), NULL, NULL, NULL, 'redis_password' ),
'password' => new Password( 'server_password', $configuration['password'] ?? '', FALSE, array(), NULL, NULL, NULL, 'redis_password' ),
);
}
/**
* Redis key
*/
protected $_redisKey;
/**
* Get random string used in the keys to identify this site compared to other sites
*
* @return string|FALSE Value from the _datastore; FALSE if key doesn't exist
*/
protected function _getRedisKey()
protected function _getRedisKey(): string|FALSE
{
if ( !$this->_redisKey )
{
if ( !( $this->_redisKey = \IPS\Redis::i()->get( 'redisKey' ) ) )
if ( !( $this->_redisKey = RedisClass::i()->get( 'redisKey' ) ) )
{
$this->_redisKey = md5( mt_rand() );
\IPS\Redis::i()->setex( 'redisKey', 604800, $this->_redisKey );
RedisClass::i()->setex( 'redisKey', 604800, $this->_redisKey );
}
}
@@ -120,10 +134,10 @@ class _Redis extends \IPS\Data\Cache
/**
* Abstract Method: Get
*
* @param string $key
* @param string $key
* @return string|FALSE Value from the _datastore; FALSE if key doesn't exist
*/
protected function get( $key )
protected function get( string $key ): string|FALSE
{
if( array_key_exists( $key, $this->cache ) )
{
@@ -132,13 +146,13 @@ class _Redis extends \IPS\Data\Cache
try
{
$this->cache[ $key ] = \IPS\Redis::i()->get( $this->_getRedisKey() . '_' . $key );
$this->cache[ $key ] = RedisClass::i()->get( $this->_getRedisKey() . '_' . $key );
return $this->cache[ $key ];
}
catch( \RedisException $e )
catch( RedisException $e )
{
\IPS\Redis::i()->resetConnection( $e );
RedisClass::i()->resetConnection( $e );
return FALSE;
}
@@ -147,28 +161,28 @@ class _Redis extends \IPS\Data\Cache
/**
* Abstract Method: Set
*
* @param string $key Key
* @param string $value Value
* @param \IPS\DateTime|NULL $expire Expreation time, or NULL for no expiration
* @param string $key Key
* @param string $value Value
* @param DateTime|NULL $expire Expreation time, or NULL for no expiration
* @return bool
*/
protected function set( $key, $value, \IPS\DateTime $expire = NULL )
protected function set( string $key, string $value, DateTime $expire = NULL ): bool
{
try
{
if ( $expire )
{
return (bool) \IPS\Redis::i()->setex( $this->_getRedisKey() . '_' . $key, $expire->getTimestamp() - time(), $value );
return RedisClass::i()->setex( $this->_getRedisKey() . '_' . $key, $expire->getTimestamp() - time(), $value );
}
else
{
/* Set for 24 hours */
return (bool) \IPS\Redis::i()->setex( $this->_getRedisKey() . '_' . $key, 86400, $value );
return RedisClass::i()->setex( $this->_getRedisKey() . '_' . $key, 86400, $value );
}
}
catch( \RedisException $e )
catch( RedisException $e )
{
\IPS\Redis::i()->resetConnection( $e );
RedisClass::i()->resetConnection( $e );
return FALSE;
}
@@ -177,35 +191,35 @@ class _Redis extends \IPS\Data\Cache
/**
* Abstract Method: Exists?
*
* @param string $key Key
* @param string $key Key
* @return bool
*/
protected function exists( $key )
protected function exists( string $key ): bool
{
if( array_key_exists( $key, $this->cache ) )
{
return ( $this->cache[ $key ] === FALSE ) ? FALSE : TRUE;
return !( ( $this->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 */
return ( $this->get( $key ) === FALSE ) ? FALSE : TRUE;
return !( ( $this->get( $key ) === FALSE ) );
}
/**
* Abstract Method: Delete
*
* @param string $key Key
* @param string $key Key
* @return bool
*/
protected function delete( $key )
protected function delete( string $key ): bool
{
try
{
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;
}
}
@@ -215,12 +229,12 @@ class _Redis extends \IPS\Data\Cache
*
* @return void
*/
public function clearAll()
public function clearAll() : void
{
parent::clearAll();
$this->_redisKey = md5( mt_rand() );
\IPS\Redis::i()->setex( 'redisKey', 604800, $this->_redisKey );
RedisClass::i()->setex( 'redisKey', 604800, $this->_redisKey );
}
}