"Fossies" - the Fresh Open Source Software Archive

Member "serendipity/bundled-libs/voku/simple-cache/src/voku/cache/AdapterArray.php" (20 Nov 2022, 2338 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 "AdapterArray.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  * AdapterArray: simple array-cache adapter
    9  */
   10 class AdapterArray implements iAdapter
   11 {
   12     /**
   13      * @var array
   14      */
   15     private static $values = [];
   16 
   17     /**
   18      * @var array<string, array<int>>
   19      */
   20     private static $expired = [];
   21 
   22     /**
   23      * {@inheritdoc}
   24      */
   25     public function exists(string $key): bool
   26     {
   27         $this->removeExpired($key);
   28 
   29         return \array_key_exists($key, self::$values);
   30     }
   31 
   32     /**
   33      * {@inheritdoc}
   34      */
   35     public function get(string $key)
   36     {
   37         return $this->exists($key) ? self::$values[$key] : null;
   38     }
   39 
   40     /**
   41      * {@inheritdoc}
   42      */
   43     public function installed(): bool
   44     {
   45         return true;
   46     }
   47 
   48     /**
   49      * {@inheritdoc}
   50      */
   51     public function remove(string $key): bool
   52     {
   53         $this->removeExpired($key);
   54 
   55         if (\array_key_exists($key, self::$values) === true) {
   56             unset(self::$values[$key]);
   57 
   58             return true;
   59         }
   60 
   61         return false;
   62     }
   63 
   64     /**
   65      * {@inheritdoc}
   66      */
   67     public function removeAll(): bool
   68     {
   69         self::$values = [];
   70         self::$expired = [];
   71 
   72         return true;
   73     }
   74 
   75     /**
   76      * {@inheritdoc}
   77      */
   78     public function set(string $key, $value): bool
   79     {
   80         self::$values[$key] = $value;
   81 
   82         return true;
   83     }
   84 
   85     /**
   86      * {@inheritdoc}
   87      */
   88     public function setExpired(string $key, $value, int $ttl = 0): bool
   89     {
   90         self::$values[$key] = $value;
   91 
   92         if ($ttl !== 0) {
   93             self::$expired[$key] = [\time(), $ttl];
   94         }
   95 
   96         return true;
   97     }
   98 
   99     /**
  100      * Remove expired cache.
  101      *
  102      * @param string $key
  103      *
  104      * @return bool
  105      */
  106     private function removeExpired($key): bool
  107     {
  108         if (
  109             \array_key_exists($key, self::$expired) === false
  110             ||
  111             \array_key_exists($key, self::$values) === false
  112         ) {
  113             return false;
  114         }
  115 
  116         list($time, $ttl) = self::$expired[$key];
  117         \assert(\is_int($time));
  118         \assert(\is_int($ttl));
  119 
  120         if (\time() > ($time + $ttl)) {
  121             unset(self::$values[$key]);
  122         }
  123 
  124         if (!isset(self::$values[$key])) {
  125             unset(self::$expired[$key]);
  126         }
  127 
  128         return true;
  129     }
  130 }