"Fossies" - the Fresh Open Source Software Archive

Member "serendipity/bundled-libs/voku/simple-cache/src/voku/cache/AdapterMemcache.php" (20 Nov 2022, 2782 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 "AdapterMemcache.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 use Memcache;
    8 use voku\cache\Exception\InvalidArgumentException;
    9 
   10 /**
   11  * AdapterMemcache: Memcache-adapter
   12  */
   13 class AdapterMemcache implements iAdapter
   14 {
   15     /**
   16      * @var bool
   17      */
   18     public $installed = false;
   19 
   20     /**
   21      * @var Memcache
   22      */
   23     private $memcache;
   24 
   25     /**
   26      * @var bool
   27      */
   28     private $compressed = false;
   29 
   30     /**
   31      * __construct
   32      *
   33      * @param Memcache|null $memcache
   34      */
   35     public function __construct($memcache = null)
   36     {
   37         if ($memcache instanceof Memcache) {
   38             $this->setMemcache($memcache);
   39         }
   40     }
   41 
   42     /**
   43      * @param Memcache $memcache
   44      */
   45     public function setMemcache(Memcache $memcache)
   46     {
   47         $this->memcache = $memcache;
   48         $this->installed = true;
   49     }
   50 
   51     /**
   52      * {@inheritdoc}
   53      */
   54     public function exists(string $key): bool
   55     {
   56         return $this->get($key) !== false;
   57     }
   58 
   59     /**
   60      * {@inheritdoc}
   61      */
   62     public function get(string $key)
   63     {
   64         return $this->memcache->get($key);
   65     }
   66 
   67     /**
   68      * {@inheritdoc}
   69      */
   70     public function installed(): bool
   71     {
   72         return $this->installed;
   73     }
   74 
   75     /**
   76      * {@inheritdoc}
   77      */
   78     public function remove(string $key): bool
   79     {
   80         return $this->memcache->delete($key);
   81     }
   82 
   83     /**
   84      * {@inheritdoc}
   85      */
   86     public function removeAll(): bool
   87     {
   88         return $this->memcache->flush();
   89     }
   90 
   91     /**
   92      * {@inheritdoc}
   93      */
   94     public function set(string $key, $value): bool
   95     {
   96         // Make sure we are under the proper limit
   97         if (\strlen($key) > 250) {
   98             throw new InvalidArgumentException('The passed cache key is over 250 bytes:' . \print_r($key, true));
   99         }
  100 
  101         return $this->memcache->set($key, $value, $this->getCompressedFlag());
  102     }
  103 
  104     /**
  105      * {@inheritdoc}
  106      */
  107     public function setExpired(string $key, $value, int $ttl = 0): bool
  108     {
  109         if ($ttl > 2592000) {
  110             $ttl = 2592000;
  111         }
  112 
  113         return $this->memcache->set($key, $value, $this->getCompressedFlag(), $ttl);
  114     }
  115 
  116     /**
  117      * Get the compressed-flag from MemCache.
  118      *
  119      * @return int 2 || 0
  120      */
  121     private function getCompressedFlag(): int
  122     {
  123         return $this->isCompressed() ? \MEMCACHE_COMPRESSED : 0;
  124     }
  125 
  126     /**
  127      * Check if compression from MemCache is active.
  128      *
  129      * @return bool
  130      */
  131     public function isCompressed(): bool
  132     {
  133         return $this->compressed;
  134     }
  135 
  136     /**
  137      * Activate the compression from MemCache.
  138      *
  139      * @param mixed $value will be converted to bool
  140      */
  141     public function setCompressed($value)
  142     {
  143         $this->compressed = (bool) $value;
  144     }
  145 }