Version 5.0.0 beta 9

This commit is contained in:
Neo committed 2025-12-19 17:47:00 -08:00
1 parent 214f649f1d
commit 56979f8ee8
202 files changed
+4403 -1123

No files matched your search

+57 -14
View File
@@ -553,13 +553,22 @@ trait Statistics
/**
* Clear any cached stats
*
* @param bool $force Force immediate cache delete
* @return void
* @throws Exception
*/
public function clearCachedStatistics(): void
public function clearCachedStatistics( bool $force=FALSE )
{
$idField = static::$databaseColumnId;
Db::i()->delete( 'core_item_statistics_cache', array( 'cache_class=? and cache_item_id=?', get_class( $this ), $this->$idField ) );
if( $force )
{
Db::i()->delete( 'core_item_statistics_cache', [ 'cache_class=? and cache_item_id=?', \get_class( $this ), $this->$idField ] );
}
else
{
/* Set cache to time out in 10 minutes. */
Db::i()->update( 'core_item_statistics_cache', [ 'cache_added' => ( time() - 172200 ) ], [ 'cache_class=? and cache_item_id=?', \get_class( $this ), $this->$idField ] );
}
}
/**
@@ -728,6 +737,11 @@ trait Statistics
*/
public static array|null $cachedActivity = NULL;
/**
* @var bool|null Process has rebuild lock
*/
protected ?bool $hasRebuildLock = NULL;
/**
* Get the cached data
*
@@ -738,28 +752,57 @@ trait Statistics
protected function _getCached( string $key ): mixed
{
$idField = static::$databaseColumnId;
if( $this->hasRebuildLock === FALSE )
{
return [];
}
$class = get_class( $this );
$arrayKey = $class.'.'.$this->$idField;
if ( !isset( static::$cachedActivity[$arrayKey] ) )
$arrayKey = $class . '.' . $this->$idField;
if ( !isset( static::$cachedActivity[ $arrayKey ] ) )
{
try
{
$cache = Db::i()->select( '*', 'core_item_statistics_cache', array( 'cache_class=? and cache_item_id=?', $class, $this->$idField ) )->first();
if ( $cache['cache_added'] > time() - 86400 )
$cache = Db::i()->select( '*', 'core_item_statistics_cache', [ 'cache_class=? and cache_item_id=?', $class, $this->$idField ] )->first();
if ( $cache['cache_added'] > ( time() - 172800 ) )
{
static::$cachedActivity[ $arrayKey ] = json_decode( $cache['cache_contents'], TRUE );
}
else
{
Db::i()->delete( 'core_item_statistics_cache', array( 'cache_class=? and cache_item_id=?', get_class( $this ), $this->$idField ) );
$this->hasRebuildLock = TRUE;
$affectedRows = Db::i()->delete( 'core_item_statistics_cache', [ 'cache_class=? and cache_item_id=?', \get_class( $this ), $this->$idField ] );
if( $affectedRows == 0 )
{
/* We did not delete the row, allow another process to rebuild the cache */
$this->hasRebuildLock = FALSE;
return [];
}
}
}
catch( \UnderflowException $e )
{
/* Try to get the lock for new cache generation */
try
{
Db::i()->insert( 'core_item_statistics_cache', [
'cache_class' => \get_class( $this ),
'cache_item_id' => $this->$idField,
'cache_contents' => '[]',
'cache_added' => time()
] );
$this->hasRebuildLock = TRUE;
}
/* We didn't get the lock, return empty array and allow other process to build cache */
catch( \IPS\Db\Exception $e )
{
$this->hasRebuildLock = FALSE;
return [];
}
}
catch( UnderflowException $e ) { }
}
if ( isset(static::$cachedActivity[ $arrayKey ] ) and isset( static::$cachedActivity[ $arrayKey ][ $key ] ) )
if ( isset( static::$cachedActivity[ $arrayKey ] ) and isset( static::$cachedActivity[ $arrayKey ][ $key ] ) )
{
return static::$cachedActivity[ $arrayKey ][ $key ];
}
@@ -767,10 +810,10 @@ trait Statistics
{
if( !isset( static::$cachedActivity[ $arrayKey ] ) )
{
static::$cachedActivity[ $arrayKey ] = array();
static::$cachedActivity[ $arrayKey ] = [];
}
static::$cachedActivity[ $arrayKey ][ $key ] = array();
static::$cachedActivity[ $arrayKey ][ $key ] = [];
throw new OutOfRangeException;
}
}
@@ -819,8 +862,8 @@ trait Statistics
{
Db::i()->insert( 'core_item_statistics_cache', array(
'cache_class' => get_class( $this ),
'cache_item_id' => str_replace( get_class($this) . '.', '', $key),
'cache_contents' => json_encode( static::$cachedActivity[$key] ),
'cache_item_id' => str_replace( \get_class( $this ) . '.', '', $key ),
'cache_contents' => json_encode( static::$cachedActivity[ $key ] ) ?? '[]',
'cache_added' => time()
), TRUE );
}