StarCache is a comprehensive WordPress MU-Plugin (Must-Use Plugin) that provides intelligent, multi-layered caching for PHP/WordPress applications. It auto-detects the best available cache backend, hooks into WordPress at the right moments, and gives developers a clean API to improve site performance without manual plumbing.
| Feature | Details |
|---|---|
| Backend auto-detection | Probes Redis → Memcached → Memcache → WordPress built-in object cache in priority order at startup. |
| OPcache awareness | Detects whether PHP OPcache is enabled and exposes the status via API and the admin bar. |
| Full-page cache | Output-buffer caching for entire HTML responses; serves cached pages before WordPress queries the database. |
| Fragment / partial cache | getFragment / saveFragment helpers to cache chunks of template output (sidebar, navigation, widgets, etc.). |
| Varnish integration | Sends Cache-Control, Vary, and X-Cache-Tags headers; issues HTTP PURGE requests to Varnish on post save / status change. |
| Query cache utility (deprecated) | StarQueryCache provides cachedWpdbQuery() for SQL look-aside caching. Hook-based WP_Query caching via posts_pre_query / the_posts is not registered and will not be in v3.0. |
| Transient helpers | Per-site and network-wide (multisite) transient wrappers with consistent key generation. |
| Asset minification | Minifies enqueued CSS and JS files in PHP (no external tools), writes to a filesystem cache, and swaps the enqueued src URL. |
| Multisite-aware | Every cache key embeds the current blog ID; network transients span all sites. |
| WP-CLI command | wp starcache flush bumps version counters to invalidate cache entries. It does not delete underlying storage. |
| Admin bar badge | Shows the active backend (e.g. StarCache: REDIS + OPcache) to admins. |
- Copy all PHP files from this repository into
wp-content/mu-plugins/. - The plugin loads automatically – no activation required.
- If you are using Composer, add the package:
composer require maximilliangroupinc/starcacheThen in your MU-Plugin loader file:
require_once WP_CONTENT_DIR . '/mu-plugins/starcache.php';// Redis
define('WP_REDIS_HOST', '127.0.0.1');
define('WP_REDIS_PORT', 6379);
define('WP_REDIS_PASSWORD', 'secret'); // optional
define('WP_REDIS_DATABASE', 0); // optional
// Memcached (preferred over Memcache when both extensions are loaded)
define('MEMCACHED_SERVERS', [
['host' => '127.0.0.1', 'port' => 11211],
]);
// Varnish (enables Cache-Control headers and PURGE requests)
define('VARNISH_HOST', '127.0.0.1');
define('VARNISH_PORT', 6081);
// Asset minification cache directory (must be web-accessible)
define('STARCACHE_ASSET_DIR', WP_CONTENT_DIR . '/cache/starcache/assets');
define('STARCACHE_ASSET_URL', WP_CONTENT_URL . '/cache/starcache/assets');
// Set to false to disable asset minification
define('STARCACHE_MINIFY', true);// Get the singleton StarCache instance
$cache = star_cache();
// Store a value (TTL defaults to 1 hour)
$cache->star_setCachedData(['user' => 'Jane'], 'user_profile', '42');
// Retrieve
$data = $cache->star_getCachedData('user_profile', '42');
// Delete
$cache->star_deleteCachedData('user_profile', '42');
// Cache-aside (get-or-set pattern)
$posts = $cache->star_remember('homepage_posts', function () {
return get_posts(['numberposts' => 10]);
}, 300);star_cache_set(['key' => 'value'], 'my_feature'); // store
star_cache_get('my_feature'); // retrieve
star_cache_delete('my_feature'); // delete
star_cache_remember('nav_menu', fn() => wp_nav_menu([
'echo' => false,
]), 3600);if (!\StarCache\StarPageCache::getFragment('sidebar')) {
// Render sidebar – output is captured
get_sidebar();
\StarCache\StarPageCache::saveFragment('sidebar');
}global $wpdb;
$sql = $wpdb->prepare("SELECT * FROM {$wpdb->posts} WHERE post_status = %s", 'publish');
$results = \StarCache\StarQueryCache::cachedWpdbQuery($sql, ARRAY_A, 300);// Store data shared across all sites in the network
\StarCache\StarTransientCache::star_setNetworkCachedData($config, 'global_settings');
// Retrieve
$config = \StarCache\StarTransientCache::star_getNetworkCachedData('global_settings');// Purge a specific URL from Varnish + object cache
\StarCache\StarPageCache::purgeUrl('https://example.com/my-page/');wp starcache flushStarCache probes backends in the following order and uses the first one that responds successfully:
- Redis – requires the
redisPHP extension; readsWP_REDIS_*constants. - Memcached – requires the
memcachedPHP extension; readsMEMCACHED_SERVERS. - Memcache – requires the
memcachePHP extension; readsMEMCACHE_SERVER_HOST/PORT. - WordPress object cache – always available; backed by APCu, file, or database depending on the active object-cache drop-in.
The detected backend is exposed via StarCacheAdapter::getBackend() and shown in
the WordPress admin bar for administrators.
| Filter / Action | Purpose |
|---|---|
starcache_bypass_page_cache |
Return true to skip page caching for the current request. |
starcache_page_ttl |
Override the full-page cache TTL (default 600 seconds). |
starcache_query_ttl |
Override the WP_Query cache TTL (default 300 seconds). |
starcache_cache_query |
Return false to prevent a specific WP_Query from being cached. |
starcache_varnish_enabled |
Return true to force-enable Varnish headers without defining VARNISH_HOST. |
starcache_minify_enabled |
Return false to disable asset minification at runtime. |
starcache_after_purge |
Fired after a page is purged; receives $postId and $post. |
starcache_after_query_invalidate |
Fired after query caches are invalidated; receives $postId. |
composer install
./vendor/bin/phpunitStarCache is released under the Apache 2.0 License.
Contributions are welcome! Please see CONTRIBUTING.md for details.
If you encounter any issues or have questions, please open an issue on the GitHub repository.