APCU cache in the kernel

Post Reply
n1x3r
Jr. Bludit
Posts: 2
Joined: Tue Mar 31, 2026 4:54 pm
Been thanked: 2 times

Hello everyone, I’ve been playing around with Bludit for a week now and I’ve come up with a good kernel-level improvement that you could implement to speed up page loading and reduce the number of database reads: implementing an APCU cache in dbjson.class.php

Users simply need to enable the apcu extension in PHP and modify the `__construct` function in `bl-kernel/abstract/dbjson.class.php`

Here is the modified function:

Code: Select all

	function __construct($file, $firstLine=true)
	{
		$this->file = $file;
		$this->db = array();
		$this->dbBackup = array();
		$this->firstLine = $firstLine;

		if (file_exists($file)) {

			$array = false;

			if (function_exists('apcu_fetch')) {

				$mtime = filemtime($file);
				$key = md5($file . $mtime);

				$array = apcu_fetch($key);
				if ($array === false) {

					$lines = file($file);

					if ($firstLine) {
						unset($lines[0]);
					}

					$implode = implode('', $lines);

					$array = $this->unserialize($implode);

					apcu_store($key, $array, 600);

				}
			}else{

				$lines = file($file);

				if ($firstLine) {
					unset($lines[0]);
				}

				$implode = implode('', $lines);

				$array = $this->unserialize($implode);
			}

			if (empty($array)) {
				$this->db = array();
				$this->dbBackup = array();
			} else {
				$this->db = $array;
				$this->dbBackup = $array;
			}
		}
	}
You can change the line ‘apcu_store($key, $array, 600);’ to whatever number of seconds you prefer.
I hope this is useful for all of you who have a lot of content and need to speed up this CMS even further
I'll also leave it up to the developers, in case they want to implement it in future versions.

thx

EDIT: It is also possible to implement it in pligin.class.php

Code: Select all

	function __construct()
	{
		$this->dbFields = array();
		$this->customHooks = array();

		$reflector = new ReflectionClass(get_class($this));

		// Directory name
		$this->directoryName = basename(dirname($reflector->getFileName()));

		// Class Name
		$this->className = $reflector->getName();

		$this->formButtons = true;

		// Call the method init() from the children
		$this->init();

		// Init empty database with default values
		$this->db = $this->dbFields;

		$this->filenameDb = PATH_PLUGINS_DATABASES . $this->directoryName . DS . 'db.php';

		// --- Metadata ---
		$this->filenameMetadata = PATH_PLUGINS . $this->directoryName() . DS . 'metadata.json';

		if (function_exists('apcu_fetch')) {

			// ===== CACHE METADATA =====
			$mtimeMeta = filemtime($this->filenameMetadata);
			$keyMeta = md5($this->filenameMetadata . $mtimeMeta);

			$metadata = apcu_fetch($keyMeta);

			if ($metadata === false) {
				$metadataString = file_get_contents($this->filenameMetadata);
				$metadata = json_decode($metadataString, true);

				apcu_store($keyMeta, $metadata, 600);
			}

			$this->metadata = $metadata;

		} else {
			$metadataString = file_get_contents($this->filenameMetadata);
			$this->metadata = json_decode($metadataString, true);
		}

		// If the plugin is installed then get the database
		if ($this->installed()) {

			if (function_exists('apcu_fetch')) {

				$mtimeDb = filemtime($this->filenameDb);
				$keyDb = md5($this->filenameDb . $mtimeDb);

				$dbData = apcu_fetch($keyDb);

				if ($dbData === false) {
					$Tmp = new dbJSON($this->filenameDb);
					$dbData = $Tmp->db;

					apcu_store($keyDb, $dbData, 600);
				}

				$this->db = $dbData;

			} else {
				$Tmp = new dbJSON($this->filenameDb);
				$this->db = $Tmp->db;
			}

			$this->prepare();
		}
	}
Post Reply