=== QuantumCache ===
Contributors: adelong01
Donate link: https://paypal.me/quantumcache
Tags: caching, performance, redis, database, multisite
Requires at least: 6.0
Tested up to: 7.0
Requires PHP: 8.0
Stable tag: 1.0.0
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Speeds up WordPress by caching database queries and fragments for anonymous visitors using Redis or MySQL, without storing cache data in wp_options.

== Description ==

QuantumCache accelerates WordPress by caching database query results and content fragments used to build pages. Instead of caching entire HTML pages, it reduces the database work required to render pages while preserving WordPress’s native invalidation behavior.

It is *not* an output buffer or page cache, and it does not replace existing page caching solutions.


### Features
* Caches the results of the main front-end `WP_Query` for anonymous requests.
* Tag-based invalidation: purge by post ID, post type, or taxonomy.
* Fragment caching for `the_content` (classic) and core blocks such as `core/post-content`, `core/latest-posts`, `core/archives`, and `core/navigation`.
* Multiple storage modes:
  * Auto – use Redis if available, otherwise fall back to MySQL.
  * Redis – fastest mode; uses the PHP Redis extension.
  * MySQL – portable fallback using compact tables (`qc_entries`, `qc_tagmap`).
* Compatible with the Redis Object Cache drop-in.
* Avoids wp_options and postmeta storage – all cache data is stored in Redis or
  QuantumCache-owned database tables.
* Multisite-aware: per-site prefixes and per-site tables.
* WP-CLI commands: `wp quantumcache flush` and `wp quantumcache status`.
* Metrics data layer: persistent hit, miss, store, and purge event counters available via `Metrics::totals()` and `Metrics::deltas()`.

### Performance summary

In benchmarks on a plugin-heavy WordPress site (WooCommerce + Yoast SEO),
QuantumCache reduced per-request database queries by up to ~99% on cached pages
and achieved cache hit rates exceeding 97% with Redis.

In the benchmark environment:

* Redis mode reduced warm TTFB by roughly **23–27%** and total response time by **18–21%** versus an uncached baseline.
* MySQL fallback mode delivered **modest improvements (~3–10%)** when Redis was unavailable.

Actual results depend on theme complexity, plugin load, database topology, and traffic patterns.

### When to use QuantumCache

QuantumCache is useful when:

* Your site already uses a page cache (such as a CDN, reverse proxy, or caching plugin) but WordPress still performs significant database work.
* You want faster response times without relying solely on full-page caching.
* Your hosting environment supports Redis, or you want a portable MySQL fallback.

QuantumCache complements page caching rather than replacing it.

### What QuantumCache does NOT do

QuantumCache is not a page cache or full HTML cache.

It does not replace solutions such as:
* CDN caching
* reverse proxy caches
* traditional WordPress page caching plugins

Instead, QuantumCache reduces the database work required to build pages, making those systems more effective.

### Debug headers (optional)
Enable *Settings → QuantumCache → Send X-QC debug headers* to view cache status in HTTP responses:
* `X-QC-Main: HIT | MISS | STORE`
* `X-QC-Store: AUTO | redis | mysql`
* `X-QC-Hydrated:` number of posts loaded from cache

Append `?qc_nocache=1` to any URL to bypass caching for that request.

Debug headers are sent only when explicitly enabled in settings and contain no
personal or user-specific data.

### WP-CLI commands

QuantumCache provides the following WP-CLI commands:

wp quantumcache status
Shows the active backend, storage mode, and lifetime cache counters.

wp quantumcache flush
Clears all cache entries for the current site.

Use --yes to skip the confirmation prompt.

### QuantumCache Pro

QuantumCache Pro extends the core engine with advanced caching capabilities for WooCommerce and logged-in users.

See: https://quantumcache.io

== Installation ==

1. Upload the `quantumcache` folder to `/wp-content/plugins/` and activate it.
2. (Optional) You may install the Redis Object Cache plugin for additional Redis features. QuantumCache works independently.
3. Go to *Settings → QuantumCache*:
   * Choose a store mode: Auto, Redis, or MySQL.
   * Configure Redis host/port if not already set by constants.
   * Optionally enable debug headers.
4. Load a public page twice. On the second load, the response should include `X-QC-Main: HIT`.

== Frequently Asked Questions ==

= Does it cache logged-in users? =
No. QuantumCache focuses on anonymous (front-end) traffic to ensure safe invalidation. Logged-in caching is not included.

= Do I need Redis? =
Redis is recommended for best performance, but not required. QuantumCache automatically falls back to its internal MySQL store.

= Does it cache admin pages, feeds, or searches? =
No. Those are intentionally excluded.

= Is it Multisite-compatible? =
Yes. Each site uses its own prefix and tables. Purge actions apply per site.

= How do I purge the cache? =
From *Settings → QuantumCache*:
* Purge Everything – clears all QuantumCache entries for this site.
* Purge by Tag – e.g. `post:123` or `taxonomy:category`.
* Purge by URL – resolves to post/taxonomy tags automatically.

From the command line via WP-CLI: `wp quantumcache flush`

= Can I manage QuantumCache from the command line? =
Yes. Two WP-CLI commands are available:
* `wp quantumcache flush` – clear all entries for the current site (prompts for confirmation; use `--yes` to skip).
* `wp quantumcache status` – display backend, storage stats, and lifetime hit/miss/store/purge counters.

= Which fragments are cached? =
* Classic themes – `the_content` on singulars.
* Block themes – `core/post-content` and common navigation/listing blocks.

= Does it cache dynamic content or nonces? =
No. Fragments containing nonces, tokens, or user-specific data are skipped automatically.

= Does this replace my page cache or CDN? =
No. QuantumCache operates below page caches and CDNs by reducing database and
rendering overhead. It is designed to complement, not replace, page caching
solutions.

= What happens on uninstall? =
Unless the `quantumcache_preserve_data_on_uninstall` filter returns true, QuantumCache removes its own tables and options. No wp_options or postmeta entries are left behind.

== Screenshots ==

1. Settings page showing store mode and Redis options.
2. Cache statistics and purge controls.
3. Network panel showing `X-QC-*` debug headers.

== Changelog ==

= 1.0.0 =
* Verified: All QuantumCache functionality was tested with WordPress 7.0

= 0.1.7 =
* Fixed: RedisStore::purgeAll() was referencing the wrong internal property ($this->redis instead of $this->r), causing all bulk purge calls from the admin "Purge Everything" button to silently fail and return 0 keys removed.
* Fixed: MySQLStore::purgeAll() was not implemented; the admin "Purge Everything" button showed "backend does not support bulk purge" on MySQL/Auto installs. Both qc_entries and qc_tagmap are now cleared correctly.
* Fixed: Stats::mysql_stats() queried a non-existent column (created_at) instead of the correct column (updated); this caused the Entries count in the admin Overview table to always display 0 for MySQL-backed installs.
* Major improvements to fragment caching behavior, new WP-CLI tooling, and internal metrics support.
* Perf: Added FragmentCache::peek() for pre-render cache check without lock or producer execution.
* Fixed: Fragment cache key role dimension now uses a sorted role list before implode, preventing cache misses for users with multiple roles due to non-deterministic array key order in $user->roles (FragmentCache.php).
* Feature: Added WP-CLI support — `wp quantumcache flush` clears all cache entries for the current site (with confirmation prompt, or --yes to skip); `wp quantumcache status` reports backend, storage stats, and lifetime event counters.
* Feature: Added Metrics data layer tracking split query and fragment hit, miss, store, and purge event counts with atomic wp_options persistence; exposes Metrics::deltas() for current-request data and Metrics::totals() for cumulative counters.
* Dev: Added quantumcache_cache_hit, quantumcache_cache_miss, and quantumcache_cache_store action hooks at each cache outcome point in QueryCache and FragmentCache, available for Pro and third-party extensions.
* Fixed: Changed singular query alias cache keys so they respect the logged-in role cache bucket, preventing anonymous singular alias entries from satisfying logged-in requests and preventing proper role-scoped primary key creation.

= 0.1.6 =

* Fix: Corrected QUANTUMCACHE_VERSION constant value to match plugin header (was 0.1.4).
* Compat: Fire quantumcache_loaded action after plugin init so Pro and third-party extensions can reliably detect when Free has finished bootstrapping.	

= 0.1.5 =

* Security: Hardened direct database access by validating and escaping internal
  table identifiers to satisfy PluginCheck and reviewer requirements.
* Code: Resolved all PluginCheck warnings; no functional behavior changes.
* Cleanup: Removed development-only tooling files from the release package.
* Stability: Minor internal cleanup following load testing and runtime validation.
  extensions can reliably detect when Free has finished bootstrapping.

= 0.1.4 =

* Fix: Fixed incomplete Stats.php code concerning MySQL reporting
* Fix: Prefixed all global functions and variables (qc_* → quantumcache_*) to satisfy Plugin Check naming requirements.
* Fix: Updated uninstall routine to use prefixed globals and updated option names.
* Fix: Added defensive table-name validation and explicit casting in Maintenance and Stats to address Plugin Check warnings.
* Fix: Corrected several internal calls referencing the old function names (qc_should_debug_headers, etc.), preventing runtime errors.

= 0.1.3 =

* Fix: Updated MySQLStore write operations to use $wpdb->insert() with format arrays, resolving the PHPCS / reviewer-required change.
* Fix: Corrected StoreFactory to pass the required $wpdb instance when constructing MySQLStore, restoring MySQL fallback functionality.

= 0.1.2 =
* Admin: refined settings layout, help text, and capability/nonce checks.
* Code: PHPCS/WPCS compliance (sanitization, escaping, Yoda, no short ternaries).
* Store: Redis “Purge Everything” via SCAN/DEL with site prefix.
* Store: MySQL tag mapping writes via `wpdb->insert()` with formats.
* Read path: safe request URI normalization for keys and display.

= 0.1.1 =
* Fix: hydration logic in `on_posts_pre_query()` (now primes and hydrates safely).
* Perf: archive hit path uses `update_post_caches()` without taxonomy overhead.
* Safety: skip caching very small archives (fewer than 12 IDs).
* Headers: ensure `X-QC-Store-Actual` reflects real backend.

= 0.1.0 =
* Initial public release.
* Query-level caching with tag-based invalidation.
* Fragment caching for `the_content` and key blocks.
* Redis (preferred) and MySQL stores.
* Multisite support.
* Admin UI: store selection, Redis config, debug headers, purge controls, live stats.
* PHPCS clean (WordPress coding standards).

== Upgrade Notice ==

= 0.1.2 =
Admin refinements, PHPCS compliance, Redis purge, and safer URI handling.

== Privacy ==

QuantumCache does not collect, send, or log personal data.  
It stores cache entries in Redis or its own MySQL tables.  
Other plugins on your site may handle cookies or data independently.
No remote requests are made, and no data is transmitted to external servers.
