Search by

se7enxweb / sevenx_valkey_cache

7x

Redis/Valkey backed cache handler for Exponential / eZ Publish Legacy. Replaces disk file storage for template blocks and content view caches.

Package info

github.com/se7enxweb/sevenx_valkey_cache

Homepage

Type:exponential-extension

pkg:composer/se7enxweb/sevenx_valkey_cache

Statistics

Installs: 21

Dependents: 0

Suggesters: 2

Stars: 1

Open Issues: 0

v1.0.2.11 2026-12-03 17:56 UTC

This package is auto-updated.

Last update: 2026-12-03 17:56:11 UTC


README

A Redis/Valkey backed cache handler for Exponential 6.0.15 or later.

It replaces disk file storage for:

  • {valkey-block ...} template blocks
  • content content/view full view cache
  • compiled eZINI cache files

Cache entries are stored in Redis/Valkey with TTL based expiry, generation locks and reverse node / subtree indexes so that publishing content selectively purges the affected cache entries.

License

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

See the LICENSE file or http://www.gnu.org/licenses/ for the full license text.

Author

Developed and maintained by 7x — https://se7enx.com

Requirements

  • Exponential 6.0.15 or later
  • PHP redis or igbinary extension (php-redis is recommended)
  • A running Redis or Valkey server reachable from the web server

Kernel integration

Exponential 6.0.15 is expected to ship the kernel hooks this extension needs:

  • kernel/content/view.php calls sevenxValkeyCacheBlock for the content view cache.
  • lib/ezutils/classes/ezini.php calls sevenxValkeyINICache from loadCache(), saveCache() and resetCache() for compiled INI arrays.

If the kernel does not yet contain these calls, the same hooks can be applied as a manual patch. The current rollout (which kernels are already patched) is tracked in /root/bin/doc/SEVENX_VALKEY_CACHE_ROLLOUT.md.

Installation

Manual install

  1. Copy or checkout the extension into extension/sevenx_valkey_cache.

  2. Make the extension active in settings/override/site.ini.append.php:

[ExtensionSettings]
ActiveExtensions[]=sevenx_valkey_cache
  1. Regenerate autoloads:
php bin/php/ezpgenerateautoloads.php
  1. (Optional) Clear caches:
php bin/php/ezcache.php --clear-all --allow-root-user

Composer install

composer require se7enxweb/sevenx_valkey_cache

Then activate the extension and regenerate autoloads as described above.

Configuration

Configure the Redis/Valkey connection in settings/override/valkeycache.ini.append.php:

[ValkeyCacheSettings]
Host=127.0.0.1
Port=6379
Database=0
KeyPrefix=sevenx_valkey_cache:

# Unique identifier for this installation. Set to "auto" to derive a hash from
# the database name and site name, or use a fixed string when sharing a cache
# between servers.
InstallationHash=auto

DefaultTTL=3600
LockTTL=30

# Persistent connections may reduce connect time but should be used with care
# when several databases share a php-fpm pool.
Persistent=disabled

# Request-scoped in-memory cache. Keeps a per-request copy of retrieved entries
# to avoid repeated Redis round-trips for the same key on the same page.
LocalCache=enabled

# Cache compiled eZINI arrays in Redis/Valkey instead of local PHP files.
# Requires Exponential 6.0.15 or later.
IniCache=enabled

Content view cache

In Exponential 6.0.15 kernel/content/view.php uses sevenxValkeyCacheBlock for the content view cache backend. This stores full view results in Redis/Valkey instead of disk and dramatically reduces render time for cached nodes.

Compiled INI cache

Exponential 6.0.15 adds eZINI hooks that let the extension store compiled INI arrays in Redis/Valkey through sevenxValkeyINICache. This removes the per-request var/cache/ini/*.php disk touches and makes INI caches cluster-safe.

To enable it, set IniCache=enabled in valkeycache.ini (shown in the configuration example above).

Since 1.0.2.3 a request makes one round trip for its settings, whatever the number of INI files it reads:

  • Every compiled array is its own key, named by a generation number of the installation. A request reads the generation once (one GET), then each array from APCu (IniLocalCache=enabled), and from Valkey only when APCu does not have it yet. Clearing the INI caches, or deleting one, raises the generation, so every node and every PHP process drops its copies at once.
  • Every array expires after IniTTL seconds plus a random part of up to IniTTLJitter, so arrays written together never all expire in the same second.
  • IniSharedAcrossHosts=enabled keeps one copy for all web hosts of the installation, instead of one per host name. A compiled INI file is the same for every host of a siteaccess.
  • IniNodeFingerprint=enabled (the default since 1.0.2.8) also keys each compiled array by the content of that INI's settings files on the node that reads it. Nodes that share one Valkey but set an INI differently (a per-node override, such as a Katalyst transport) each keep their own copy of that INI and still share every other one. Before, whichever node compiled an INI last decided its values for all nodes. The fingerprints are worked out once per INI generation (a walk of settings/ and extension/*/settings/, about 70 ms) and kept in APCu.

Since 1.0.2.10 the INI arrays in Valkey are a kernel cache item of their own, "Valkey INI cache" (valkey_ini, tag ini, [Cache_valkey_ini] in the extension's site.ini.append.php). ezcache.php --clear-tag=ini, --clear-id=valkey_ini, --clear-all and Setup > Cache raise the generation on every 6.0.15 kernel, also the stable one that clears only the INI caches on disk. --clear-id=ini alone reaches only the kernel's own item on such a kernel (the command takes the first item of an id), so use --clear-id=ini,valkey_ini or --clear-tag=ini there. A kernel that has eZCache::clearValkeyINICache() clears Valkey itself with its ini and global_ini items, and the extension's item then does nothing, so a clear never raises the generation more often than before. Web workers that keep settings in memory (Velocity) still need a restart to read changed settings, as with any INI change.

Since 1.0.2.10 the generation is stored as a plain string, without the connection's serializer, and the arrays written through igbinary have keys of their own. A PHP with igbinary and one without it (a command line PHP and the web PHP of one installation) now read the same generation: before, a clear from a PHP with igbinary left a value the others could not read, so they kept no compiled INI array at all, and their own clears never reached the first. Values written by earlier versions are still read.

Since 1.0.2.5 the generation is a random token, not a counter: a flush that deleted the counter made it start again at 0 and meet the arrays of before the last clear. The hashes written by versions before 1.0.2.3 are never read; the next clear removes them.

Reads from a local replica (1.0.2.4)

With ReadHost and ReadPort set, every read (cache-blocks, the view cache, the compiled INI arrays) goes to that server, a replica of Host on the same machine, and falls back to Host when it does not answer. Writes, locks and clears always go to Host. A replica may lag a few milliseconds behind; a read that misses an entry written a moment ago renders it again, as any miss does.

A cache-block with node or subtree expiry prefetches the other entries of that node with one MGET only while the node has at most PrefetchMaxKeys entries (32). The index of a node lists every variant of its blocks (per user, address and siteaccess); an MGET of thousands of keys blocked Valkey to serve one.

Usage

{valkey-block} template function

Use {valkey-block} anywhere you would use the built-in {cache-block} but want the rendered output stored in Redis/Valkey.

{valkey-block expiry=3600 subtree_expiry=$node.node_id keys=array( 'my-block', $node.node_id )}
    ... expensive template logic ...
{/valkey-block}

Parameters:

  • expiry — TTL in seconds (default 7200).
  • subtree_expiry — Node id to use as a subtree dependency. When any node under this subtree changes, the block is purged.
  • node_expiry — Node id to use as a node dependency. When the node changes, the block is purged.
  • keys — Additional cache key components (array or scalar).
  • lock — Use generation locks to avoid cache stampedes (default true).
  • ignore_content_expiry — If true, ignore the global template-block-cache and global-template-block-cache expiry timestamps.

Manual cache management

The sevenxValkeyCacheBlock singleton can be used directly in custom code:

$cache = sevenxValkeyCacheBlock::instance();
$cache->put( 'my-key', $content, 3600 );
$content = $cache->get( 'my-key' );
$cache->purge( 'my-key' );
$cache->flush();

Static cache handler

sevenxValkeyCacheHandler is registered as ContentSettings.StaticCacheHandler. It is invoked when content changes and purges the Redis/Valkey view cache and block cache entries that are indexed by the affected node ids and their ancestors.

Cleared with the kernel's caches (1.0.2.6)

From 1.0.2.6 the kernel's own cache clearing reaches the caches this extension keeps in Valkey. The extension registers sevenxValkeyCacheClearListener in its settings/site.ini.append.php:

[Cache]
ClearListeners[]=sevenxValkeyCacheClearListener

The kernel's eZCache calls it after every cache item it clears or purges, so the same thing happens from bin/php/ezcache.php (--clear-id, --clear-tag, --clear-all, --purge), from Setup > Cache in the administration interface and from the cache steps of exp:velocity deploy (the compiled INI arrays are cleared by the kernel itself with its ini and global_ini caches, since eZINI reads them by this extension's class name):

Kernel cache cleared Cleared in Valkey
content, template-block the content view caches and {valkey-block} entries, with their node and subtree indexes; the INI arrays stay

eZContentCacheManager calls it for the nodes of every content change (publish, move, hide ...) whatever StaticCacheHandler the site uses: the view caches and node-expiry entries of those nodes go through the node index, and the subtree-expiry entries of the nodes and their path up to the root through the subtree index. A site with sevenxValkeyCacheHandler as its static cache handler is cleared by that handler and not a second time.

Only the keys of this installation (its KeyPrefix and InstallationHash) are touched. Without the PHP redis extension, or with Valkey not answering, nothing is cleared in Valkey and the kernel's clear goes on with a warning. The listener needs a kernel with expCacheClearListener (Exponential 6.0.15 after 2026-11-27); an older kernel does not read the setting, and the extension works with it as before (its static cache handler, cache-blocks and INI cache are unchanged).

Also in 1.0.2.6: the INI cache class loads without the PHP redis extension. Before, eZINI's first use of it stopped with "Class Redis not found" where the extension was missing; it now reports itself disabled and the INI files are read as usual.

In 1.0.2.7 a clear covers every siteaccess of the installation. With InstallationHash=auto each siteaccess with a site name of its own has a key prefix of its own, and a clear (ezcache.php --clear-id=content, Setup > Cache) or a publish used to reach only the siteaccess it ran in. The clear, the flush and the purge by node and subtree now cover the prefixes of every siteaccess of AvailableSiteAccessList[] and RelatedSiteAccessList[] that uses the same Valkey server and database. A siteaccess without an installation hash is never cleared from another one. The prefixes themselves are unchanged.

Since then, only siteaccesses that read the same content database (site.ini [DatabaseSettings] implementation, server, port and database) are included: a node id of another database is another node. A publish in the administration therefore purges the changed nodes from the views of every front siteaccess of the site at once, and nothing else. The template-block expiry and the kernel's HTTP cache purges are global already (one expiry file, one shared purge state), so they had no such gap.

In 1.0.2.9 the kernel view patch (doc/kernel_content_view.patch) keeps signed-in visitors out of the shared view cache. The cache key leaves out the user's roles (ViewCacheTweaks ignore_userroles), so a page rendered for an editor, with its edit and remove buttons, could be served to anonymous visitors; signed-in visitors now neither read nor write it and render live. A view whose template sets cache_ttl=0 (the view result's no_cache flag) is never stored, and the generation lock it took is released so waiting requests render it themselves. A positive cache_ttl is used as the lifetime instead of the fixed 3600 seconds. Kernels already patched need the new patch applied; The Exponential 6.0.15 kernel line carries the same logic in kernel/private/classes/views/content/view.php since 6fc5e24e2f.

Implementation notes

The extension deliberately follows the architecture of the mugo_memcache proof-of-concept: a lightweight cache block class, a static cache handler for selective purging, a template function for block caching, a kernel/content/view.php content view cache integration, and a sevenxValkeyINICache backend for compiled INI caches. The storage backend has been changed from Memcache to Redis/Valkey, with added support for TTL based expiry, atomic generation locks, and node/subtree reverse indexes without requiring a relational database.

Current rollout

As of 2026-07-20, sevenx_valkey_cache is installed and active on the following Exponential/eZ Publish docroots:

  • alpha.se7enx.com — /var/www/vhosts/alpha.se7enx.com/doc/alpha.se7enx.com
  • edit.alpha.se7enx.com — /var/www/vhosts/alpha.se7enx.com/doc/edit.alpha.se7enx.com

All other *.se7enx.com, *.ezpublish.se7enx.com, *.exponential.earth and related eZ installations do not yet have the extension or the required kernel hooks. The full rollout status (including kernel patch state per docroot) is tracked in /root/bin/doc/SEVENX_VALKEY_CACHE_ROLLOUT.md.

Official status: Officially Supported for Exponential 6.