"Fossies" - the Fresh Open Source Software Archive

Member "serendipity/bundled-libs/voku/simple-cache/src/voku/cache/AdapterOpCache.php" (20 Nov 2022, 3528 Bytes) of package /linux/www/serendipity-2.4.0.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AdapterOpCache.php" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.3.5_vs_2.4.0.

    1 <?php
    2 
    3 declare(strict_types=1);
    4 
    5 namespace voku\cache;
    6 
    7 /**
    8  * AdapterOpCache: PHP-OPcache
    9  *
   10  * OPcache improves PHP performance by storing precompiled script bytecode
   11  * in shared memory, thereby removing the need for PHP to load and
   12  * parse scripts on each request.
   13  */
   14 class AdapterOpCache extends AdapterFileSimple
   15 {
   16     /**
   17      * @var bool
   18      */
   19     private static $hasCompileFileFunction;
   20 
   21     /**
   22      * {@inheritdoc}
   23      */
   24     public function __construct($cacheDir = null)
   25     {
   26         parent::__construct($cacheDir);
   27 
   28         $this->serializer = new SerializerNo();
   29 
   30         if (self::$hasCompileFileFunction === null) {
   31             /** @noinspection PhpComposerExtensionStubsInspection */
   32             /** @noinspection PhpUsageOfSilenceOperatorInspection */
   33             self::$hasCompileFileFunction = (
   34                 \function_exists('opcache_compile_file')
   35                 &&
   36                 !empty(@\opcache_get_status())
   37             );
   38         }
   39     }
   40 
   41     /**
   42      * {@inheritdoc}
   43      */
   44     public function get(string $key)
   45     {
   46         $path = $this->getFileName($key);
   47 
   48         if (
   49             \file_exists($path) === false
   50             ||
   51             \filesize($path) === 0
   52         ) {
   53             return null;
   54         }
   55 
   56         /** @noinspection PhpIncludeInspection */
   57         $data = include $path;
   58 
   59         if (!$data || !$this->validateDataFromCache($data)) {
   60             return null;
   61         }
   62 
   63         if ($this->ttlHasExpired($data['ttl']) === true) {
   64             $this->remove($key);
   65 
   66             return null;
   67         }
   68 
   69         return $data['value'];
   70     }
   71 
   72     /**
   73      * {@inheritdoc}
   74      */
   75     protected function getFileName(string $key): string
   76     {
   77         return $this->cacheDir . \DIRECTORY_SEPARATOR . self::CACHE_FILE_PREFIX . $key . '.php';
   78     }
   79 
   80     /**
   81      * {@inheritdoc}
   82      *
   83      * @noinspection PhpUndefinedClassInspection
   84      * @noinspection PhpUndefinedNamespaceInspection
   85      * @noinspection BadExceptionsProcessingInspection
   86      */
   87     public function setExpired(string $key, $value, int $ttl = 0): bool
   88     {
   89         $item = [
   90             'value' => $value,
   91             'ttl'   => $ttl ? $ttl + \time() : 0,
   92         ];
   93         if (\class_exists('\Symfony\Component\VarExporter\VarExporter')) {
   94             try {
   95                 $content = \Symfony\Component\VarExporter\VarExporter::export($item);
   96             } catch (\Symfony\Component\VarExporter\Exception\ExceptionInterface $e) {
   97                 $content = \var_export($item, true);
   98             }
   99         } else {
  100             $content = \var_export($item, true);
  101         }
  102 
  103         $content = '<?php return ' . $content . ';';
  104 
  105         $cacheFile = $this->getFileName($key);
  106 
  107         $result = $this->writeFile(
  108             $cacheFile,
  109             $content
  110         );
  111 
  112         if (
  113             $result === true
  114             &&
  115             self::$hasCompileFileFunction === true
  116         ) {
  117             // opcache will only compile and cache files older than the script execution start.
  118             // set a date before the script execution date, then opcache will compile and cache the generated file.
  119             /** @noinspection SummerTimeUnsafeTimeManipulationInspection */
  120             \touch($cacheFile, \time() - 86400);
  121 
  122             /** @noinspection PhpComposerExtensionStubsInspection */
  123             \opcache_invalidate($cacheFile);
  124             /** @noinspection PhpComposerExtensionStubsInspection */
  125             \opcache_compile_file($cacheFile);
  126         }
  127 
  128         return $result;
  129     }
  130 }