# betterdocs/4.2.0/includes/Dependencies/DI/Definition/Source/SourceCache.php

BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ &amp; Chatbot, version 4.2.0. 78 lines.

- Page: https://pluginprobe.com/plugins/betterdocs/4.2.0/code/includes/Dependencies/DI/Definition/Source/SourceCache.php
- Raw: https://pluginprobe.com/plugins/betterdocs/4.2.0/raw/includes/Dependencies/DI/Definition/Source/SourceCache.php
- Modified: 2023-08-14T08:41:14+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/betterdocs/4.2.0/code/includes/Dependencies/DI/Definition/Source/SourceCache.php#L10-L20`.

```php
<?php

namespace WPDeveloper\BetterDocs\Dependencies\DI\Definition\Source;

use WPDeveloper\BetterDocs\Dependencies\DI\Definition\AutowireDefinition;
use WPDeveloper\BetterDocs\Dependencies\DI\Definition\Definition;
use WPDeveloper\BetterDocs\Dependencies\DI\Definition\ObjectDefinition;

/**
 * Decorator that caches another definition source.
 *
 * @author Matthieu Napoli <matthieu@mnapoli.fr>
 */
class SourceCache implements DefinitionSource, MutableDefinitionSource
{
    /**
     * @var string
     */
    const CACHE_KEY = 'php-di.definitions.';

    /**
     * @var DefinitionSource
     */
    private $cachedSource;

    public function __construct(DefinitionSource $cachedSource)
    {
        $this->cachedSource = $cachedSource;
    }

    public function getDefinition(string $name)
    {
        $definition = apcu_fetch(self::CACHE_KEY . $name);

        if ($definition === false) {
            $definition = $this->cachedSource->getDefinition($name);

            // Update the cache
            if ($this->shouldBeCached($definition)) {
                apcu_store(self::CACHE_KEY . $name, $definition);
            }
        }

        return $definition;
    }

    /**
     * Used only for the compilation so we can skip the cache safely.
     */
    public function getDefinitions() : array
    {
        return $this->cachedSource->getDefinitions();
    }

    public static function isSupported() : bool
    {
        return function_exists('apcu_fetch')
            && ini_get('apc.enabled')
            && ! ('cli' === PHP_SAPI && ! ini_get('apc.enable_cli'));
    }

    public function addDefinition(Definition $definition)
    {
        throw new \LogicException('You cannot set a definition at runtime on a container that has caching enabled. Doing so would risk caching the definition for the next execution, where it might be different. You can either put your definitions in a file, remove the cache or ->set() a raw value directly (PHP object, string, int, ...) instead of a PHP-WPDeveloper\BetterDocs\Dependencies\DI definition.');
    }

    private function shouldBeCached(Definition $definition = null) : bool
    {
        return
            // Cache missing definitions
            ($definition === null)
            // Object definitions are used with `make()`
            || ($definition instanceof ObjectDefinition)
            // Autowired definitions cannot be all compiled and are used with `make()`
            || ($definition instanceof AutowireDefinition);
    }
}

```
