{
    "slug": "opcache",
    "term": "OPcache",
    "category": "php",
    "difficulty": "intermediate",
    "short": "A PHP extension that caches precompiled bytecode in shared memory, eliminating repeated parsing and compilation overhead.",
    "long": "OPcache (bundled since PHP 5.5) stores compiled PHP bytecode in shared memory so subsequent requests skip the parse-and-compile phase. It can reduce CPU usage by 50–90% for busy applications. Key configuration settings include opcache.memory_consumption (typically 128–256 MB), opcache.max_accelerated_files (set above your file count), opcache.validate_timestamps (disable in production for maximum speed; invalidate cache on deploy), and opcache.jit_buffer_size (PHP 8.0+ JIT). Misconfigured OPcache can expose cached files to other processes — ensure opcache.restrict_api is set appropriately.",
    "aliases": [
        "PHP OPcache",
        "opcode cache",
        "Zend OPcache"
    ],
    "tags": [
        "php",
        "performance",
        "caching",
        "configuration"
    ],
    "misconception": "OPcache is only useful on high-traffic production servers. OPcache eliminates repeated parsing and compilation of PHP files on every request — it provides significant speedup even on low-traffic sites and should be enabled in any production environment.",
    "why_it_matters": "Without OPcache, PHP parses and compiles every file on every request — on a busy server this wastes hundreds of milliseconds per request and burns CPU. OPcache cuts PHP execution time by 50–80% with zero code changes.",
    "common_mistakes": [
        "Leaving OPcache disabled in production because it was absent from the default php.ini.",
        "Setting opcache.validate_timestamps=1 in production — defeats caching by checking file mtimes on every request.",
        "Forgetting to restart PHP-FPM after deploying — stale bytecode serves old code until the cache expires.",
        "Setting opcache.memory_consumption too low — cache eviction under memory pressure causes random misses."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "opcode_caching",
        "php_ini",
        "php_fpm"
    ],
    "prerequisites": [
        "php_compilation_pipeline",
        "aot_vs_jit",
        "php_ini"
    ],
    "refs": [
        "https://www.php.net/manual/en/book.opcache.php",
        "https://www.php.net/manual/en/opcache.configuration.php"
    ],
    "bad_code": "; php.ini — OPcache not configured, effectively disabled\n; opcache.enable=0",
    "good_code": "; php.ini production settings\nopcache.enable=1\nopcache.memory_consumption=256\nopcache.max_accelerated_files=20000\nopcache.validate_timestamps=0  ; clear cache on deploy instead",
    "quick_fix": "Enable OPcache with opcache.enable=1, opcache.memory_consumption=256, opcache.max_accelerated_files=20000, opcache.validate_timestamps=0 in production",
    "severity": "high",
    "effort": "low",
    "created": "2026-03-13",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/opcache",
        "html_url": "https://codeclaritylab.com/glossary/opcache",
        "json_url": "https://codeclaritylab.com/glossary/opcache.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[OPcache](https://codeclaritylab.com/glossary/opcache) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/opcache"
            }
        }
    }
}