Merge branch '5' into 6

This commit is contained in:
github-actions 2024-03-30 14:23:47 +00:00
commit 4cbe07a368
3 changed files with 39 additions and 14 deletions

View File

@ -46,3 +46,8 @@ SilverStripe\Core\Injector\Injector:
factory: SilverStripe\Core\Cache\CacheFactory factory: SilverStripe\Core\Cache\CacheFactory
constructor: constructor:
namespace: 'VersionProvider' namespace: 'VersionProvider'
Psr\SimpleCache\CacheInterface.ClassInfo:
factory: SilverStripe\Core\Cache\CacheFactory
constructor:
namespace: "ClassInfo"
disable-container: true

View File

@ -10,6 +10,9 @@ use SilverStripe\Core\Manifest\ClassLoader;
use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DB; use SilverStripe\ORM\DB;
use SilverStripe\View\ViewableData; use SilverStripe\View\ViewableData;
use Psr\SimpleCache\CacheInterface;
use SilverStripe\Core\Flushable;
use SilverStripe\Core\Injector\Injector;
/** /**
* Provides introspection information about the class tree. * Provides introspection information about the class tree.
@ -18,16 +21,8 @@ use SilverStripe\View\ViewableData;
* class introspection heavily and without the caching it creates an unfortunate * class introspection heavily and without the caching it creates an unfortunate
* performance hit. * performance hit.
*/ */
class ClassInfo class ClassInfo implements Flushable
{ {
/**
* Cache for {@link hasTable()}
*
* @internal
* @var array
*/
private static $_cache_all_tables = [];
/** /**
* @internal * @internal
* @var array Cache for {@link ancestry()}. * @var array Cache for {@link ancestry()}.
@ -82,24 +77,41 @@ class ClassInfo
} }
/** /**
* Cached call to see if the table exists in the DB.
* For live queries, use DBSchemaManager::hasTable.
* @param string $tableName * @param string $tableName
* @return bool * @return bool
*/ */
public static function hasTable($tableName) public static function hasTable($tableName)
{ {
// Cache the list of all table names to reduce on DB traffic $cache = self::getCache();
if (empty(self::$_cache_all_tables) && DB::is_active()) { $configData = serialize(DB::getConfig());
self::$_cache_all_tables = DB::get_schema()->tableList(); $cacheKey = 'tableList_' . md5($configData);
$tableList = $cache->get($cacheKey) ?? [];
if (empty($tableList) && DB::is_active()) {
$tableList = DB::get_schema()->tableList();
// Cache the list of all table names to reduce on DB traffic
$cache->set($cacheKey, $tableList);
} }
return !empty(self::$_cache_all_tables[strtolower($tableName)]); return !empty($tableList[strtolower($tableName)]);
}
private static function getCache(): CacheInterface
{
return Injector::inst()->get(CacheInterface::class . '.ClassInfo');
} }
public static function reset_db_cache() public static function reset_db_cache()
{ {
self::$_cache_all_tables = null; self::getCache()->clear();
self::$_cache_ancestry = []; self::$_cache_ancestry = [];
} }
public static function flush()
{
self::reset_db_cache();
}
/** /**
* Returns the manifest of all classes which are present in the database. * Returns the manifest of all classes which are present in the database.
* *

View File

@ -516,6 +516,14 @@ abstract class DBField extends ViewableData implements DBIndexable
return $this->XML(); return $this->XML();
} }
/**
* @return string
*/
public function Nice()
{
return $this->XML();
}
/** /**
* Returns the value to be set in the database to blank this field. * Returns the value to be set in the database to blank this field.
* Usually it's a choice between null, 0, and '' * Usually it's a choice between null, 0, and ''