"Fossies" - the Fresh Open Source Software Archive

Member "serendipity/bundled-libs/voku/simple-cache/src/voku/cache/AdapterFile.php" (20 Nov 2022, 2201 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 "AdapterFile.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 declare(strict_types=1);
    4 
    5 namespace voku\cache;
    6 
    7 /**
    8  * AdapterFile: File-adapter
    9  */
   10 class AdapterFile extends AdapterFileAbstract
   11 {
   12     /**
   13      * {@inheritdoc}
   14      */
   15     public function get(string $key)
   16     {
   17         $path = $this->getFileName($key);
   18 
   19         if (
   20             \file_exists($path) === false
   21             ||
   22             \filesize($path) === 0
   23         ) {
   24             return null;
   25         }
   26 
   27         // init
   28         $string = '';
   29 
   30         /** @noinspection PhpUsageOfSilenceOperatorInspection */
   31         $fp = @\fopen($path, 'rb');
   32         if ($fp && \flock($fp, \LOCK_SH | \LOCK_NB)) {
   33             while (!\feof($fp)) {
   34                 $line = \fgets($fp);
   35                 $string .= $line;
   36             }
   37             \flock($fp, \LOCK_UN);
   38         }
   39         if ($fp) {
   40             \fclose($fp);
   41         }
   42 
   43         if (!$string) {
   44             return null;
   45         }
   46 
   47         $data = $this->serializer->unserialize($string);
   48 
   49         if (!$data || !$this->validateDataFromCache($data)) {
   50             return null;
   51         }
   52 
   53         if ($this->ttlHasExpired($data['ttl']) === true) {
   54             $this->remove($key);
   55 
   56             return null;
   57         }
   58 
   59         return $data['value'];
   60     }
   61 
   62     /**
   63      * {@inheritdoc}
   64      */
   65     public function setExpired(string $key, $value, int $ttl = 0): bool
   66     {
   67         $item = $this->serializer->serialize(
   68             [
   69                 'value' => $value,
   70                 'ttl'   => $ttl ? $ttl + \time() : 0,
   71             ]
   72         );
   73 
   74         // init
   75         $octetWritten = false;
   76 
   77         $cacheFile = $this->getFileName($key);
   78 
   79         // Open the file for writing only. If the file does not exist, it is created.
   80         // If it exists, it is neither truncated, nor the call to this function fails.
   81         /** @noinspection PhpUsageOfSilenceOperatorInspection */
   82         $fp = @\fopen($cacheFile, 'cb');
   83         if ($fp && \flock($fp, \LOCK_EX | \LOCK_NB)) {
   84             \ftruncate($fp, 0);
   85             $octetWritten = \fwrite($fp, $item);
   86             \fflush($fp);
   87             \flock($fp, \LOCK_UN);
   88         }
   89         if ($fp !== false) {
   90             \fclose($fp);
   91         }
   92 
   93         return $octetWritten !== false;
   94     }
   95 }