Memcached setup for PHP 8.x (not memcache)
Cache to disk helps to reduce load, but is no substitute for an in memory cache. Back with Dolphin, there were multiple cache options available. These options are still available with Una, however the various cache products don't exist any more. As part of tuning my server, I wanted to get more performance so I built something to use Memcachd (different to Memcache). Below are my basic instructions and documentation to make it work.
UNA Memcached Integration Guide
This guide will walk you through installing, enabling, and validating Memcached as a cache backend for UNA CMS.
1. Install Memcached on the Server
Install the Memcached server daemon and PHP extension.
For CentOS / AlmaLinux / RHEL:
sudo dnf install memcached php-memcached
sudo systemctl enable --now memcached
For Ubuntu/Debian:
sudo apt-get install memcached php-memcached
sudo systemctl enable --now memcached
Verify Memcached is running:
systemctl status memcached
2. Validate Memcached Is Working
Quick PHP CLI Test:
Create a test.php file on your server, and browse to this file. This is a good check before changing Una to confirm your PHP server is correctly configured, and memcached is operating as expected.
<?php
$mc = new Memcached();
$mc->addServer('127.0.0.1', 11211);
$mc->set('una_test', 'ok', 60);
echo "Value: " . $mc->get('una_test') . "\n";
?>
3. Add the UNA Memcached Cache Handler
Create the following file:
inc/classes/BxDolCacheMemcached.php
<?php defined('BX_DOL') or die('hack attempt');
/**
* Memcached cache handler for UNA
*/
class BxDolCacheMemcached extends BxDolCache
{
protected $iTTL = 3600;
protected $oMemcached = null;
function __construct()
{
parent::__construct();
if (class_exists('Memcached')) {
$this->oMemcached = new Memcached();
$sHost = getParam('sys_cache_memcache_host');
$iPort = getParam('sys_cache_memcache_port');
if (false === strpos($sHost, ',')) {
$this->oMemcached->addServer($sHost, $iPort);
} else {
$aHosts = explode(',', $sHost);
foreach ($aHosts as $s)
$this->oMemcached->addServer(trim($s), $iPort);
}
}
}
function getData($sKey, $iTTL = false)
{
if (!$this->oMemcached) return null;
$mixedData = $this->oMemcached->get($sKey);
return $mixedData === false ? null : $mixedData;
}
function setData($sKey, $mixedData, $iTTL = false)
{
if (!$this->oMemcached) return false;
$ttl = $iTTL === false ? $this->iTTL : $iTTL;
return $this->oMemcached->set($sKey, $mixedData, $ttl);
}
function delData($sKey)
{
if (!$this->oMemcached) return false;
$this->oMemcached->delete($sKey);
return true;
}
function isAvailable()
{
return $this->oMemcached !== null;
}
function isInstalled()
{
return extension_loaded('memcached');
}
function removeAllByPrefix($s)
{
if (!$this->oMemcached) return false;
return $this->oMemcached->flush();
}
}
4. Add "Memcached" as a Cache Option in UNA
To allow the admin panel to select Memcached as a backend, update the UNA config:
Run this SQL query in your UNA database:
UPDATE sys_options
SET extra = REPLACE(extra, 'Memcache', 'Memcache,Memcached')
WHERE extra LIKE '%Memcache%'
AND extra NOT LIKE '%Memcached%';
This will add "Memcached" to all picklists that currently offer "Memcache".
5. Enable Memcached in UNA Admin
5.1 Configure system wide Memcached options
- Go to Studio → Settings → Cache
- For each cache type (page, DB, templates, etc.), select `Memcached` from the storage engine dropdown.
- Set the Memcache Host (usually `127.0.0.1`) and **Port** (usually `11211`).
- Save changes.
**Note:** UNA uses the same host/port settings for both Memcache and Memcached PHP extensions.
5.2 Configure module specific settings
Some modules also require configuration to activate Memcached. Timeline is one such example. Check your installed modules.
- Go to Studio → Settings → Timeline → Cache
- Enable item cache, and select Memcached from the storage engine dropdown.
6. (Optional) Monitor Memcached Usage
- Show cache keys:
memcached-tool 127.0.0.1 dump | grep -a ^add
- Show stats:
memcached-tool 127.0.0.1 stats
- Page Count Stats:
memcached-tool 10.0.0.5:11211 display
- Settings:
memcached-tool 127.0.0.1 settings
7. Troubleshooting
- Ensure php-memcached is installed (not just php-memcache).
- Restart PHP-FPM/Apache after installing extensions.
- Confirm Memcached server is running and reachable.
- Watch error logs (php_errors.log or UNA Studio logs) for issues.
8. Other considerations
- Security: If server is public-facing, firewall the Memcached port (`11211`). However, this should not be an issue if your RPM only installs a listener on the loopback address.