"Fossies" - the Fresh Open Source Software Archive

Member "serendipity/bundled-libs/voku/simple-cache/src/voku/cache/AdapterFileSimple.php" (20 Nov 2022, 1803 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 "AdapterFileSimple.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  * AdapterFileSimple: File-adapter (simple)
    9  */
   10 class AdapterFileSimple extends AdapterFileAbstract
   11 {
   12     const CACHE_FILE_PREFIX = '__simple_';
   13 
   14     /**
   15      * @return resource
   16      */
   17     protected function getContext()
   18     {
   19         static $CONTEXT_CACHE = null;
   20 
   21         if ($CONTEXT_CACHE === null) {
   22             $CONTEXT_CACHE = \stream_context_create(
   23                 [
   24                     'http' => [
   25                         'timeout' => 2,
   26                     ],
   27                 ]
   28             );
   29         }
   30 
   31         return $CONTEXT_CACHE;
   32     }
   33 
   34     /**
   35      * {@inheritdoc}
   36      */
   37     public function get(string $key)
   38     {
   39         $path = $this->getFileName($key);
   40 
   41         if (
   42             \file_exists($path) === false
   43             ||
   44             \filesize($path) === 0
   45         ) {
   46             return null;
   47         }
   48 
   49         // init
   50         $string = \file_get_contents(
   51             $path,
   52             false,
   53             $this->getContext()
   54         );
   55 
   56         if (!$string) {
   57             return null;
   58         }
   59 
   60         $data = $this->serializer->unserialize($string);
   61 
   62         if (!$data || !$this->validateDataFromCache($data)) {
   63             return null;
   64         }
   65 
   66         if ($this->ttlHasExpired($data['ttl']) === true) {
   67             $this->remove($key);
   68 
   69             return null;
   70         }
   71 
   72         return $data['value'];
   73     }
   74 
   75     /**
   76      * {@inheritdoc}
   77      */
   78     public function setExpired(string $key, $value, int $ttl = 0): bool
   79     {
   80         return $this->writeFile(
   81             $this->getFileName($key),
   82             $this->serializer->serialize(
   83                 [
   84                     'value' => $value,
   85                     'ttl'   => $ttl ? $ttl + \time() : 0,
   86                 ]
   87             )
   88         );
   89     }
   90 }