"Fossies" - the Fresh Open Source Software Archive 
Member "serendipity/bundled-libs/voku/simple-cache/src/voku/cache/AdapterMemcached.php" (20 Nov 2022, 3200 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 "AdapterMemcached.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 Memcached;
8 use voku\cache\Exception\InvalidArgumentException;
9
10 /**
11 * AdapterMemcached: Memcached-adapter
12 */
13 class AdapterMemcached implements iAdapter
14 {
15 /**
16 * @var bool
17 */
18 public $installed = false;
19
20 /**
21 * @var Memcached
22 */
23 private $memcached;
24
25 /**
26 * __construct
27 *
28 * @param Memcached|null $memcached
29 */
30 public function __construct($memcached = null)
31 {
32 if ($memcached instanceof Memcached) {
33 $this->setMemcached($memcached);
34 }
35 }
36
37 /**
38 * @param Memcached $memcached
39 */
40 public function setMemcached(Memcached $memcached)
41 {
42 $this->memcached = $memcached;
43 $this->installed = true;
44
45 $this->setSettings();
46 }
47
48 /**
49 * {@inheritdoc}
50 */
51 public function exists(string $key): bool
52 {
53 return $this->get($key) !== false;
54 }
55
56 /**
57 * {@inheritdoc}
58 */
59 public function get(string $key)
60 {
61 return $this->memcached->get($key);
62 }
63
64 /**
65 * {@inheritdoc}
66 */
67 public function installed(): bool
68 {
69 return $this->installed;
70 }
71
72 /**
73 * {@inheritdoc}
74 */
75 public function remove(string $key): bool
76 {
77 return $this->memcached->delete($key);
78 }
79
80 /**
81 * {@inheritdoc}
82 */
83 public function removeAll(): bool
84 {
85 return $this->memcached->flush();
86 }
87
88 /**
89 * {@inheritdoc}
90 */
91 public function set(string $key, $value): bool
92 {
93 // Make sure we are under the proper limit
94 if (\strlen($this->memcached->getOption(Memcached::OPT_PREFIX_KEY) . $key) > 250) {
95 throw new InvalidArgumentException('The passed cache key is over 250 bytes:' . \print_r($key, true));
96 }
97
98 return $this->memcached->set($key, $value);
99 }
100
101 /**
102 * {@inheritdoc}
103 */
104 public function setExpired(string $key, $value, int $ttl = 0): bool
105 {
106 if ($ttl > 2592000) {
107 $ttl = 2592000;
108 }
109
110 return $this->memcached->set($key, $value, $ttl);
111 }
112
113 /**
114 * Set the MemCached settings.
115 *
116 * @noinspection PhpUndefinedClassConstantInspection -> MSGPACK is not added into phpstorm stubs
117 */
118 private function setSettings()
119 {
120 // Use faster compression if available
121 if (Memcached::HAVE_IGBINARY) {
122 $this->memcached->setOption(Memcached::OPT_SERIALIZER, Memcached::SERIALIZER_IGBINARY);
123 } elseif (\defined('Memcached::HAVE_MSGPACK') && Memcached::HAVE_MSGPACK) {
124 $this->memcached->setOption(Memcached::OPT_SERIALIZER, Memcached::SERIALIZER_MSGPACK);
125 }
126 $this->memcached->setOption(Memcached::OPT_DISTRIBUTION, Memcached::DISTRIBUTION_CONSISTENT);
127 $this->memcached->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
128 $this->memcached->setOption(Memcached::OPT_NO_BLOCK, true);
129 $this->memcached->setOption(Memcached::OPT_TCP_NODELAY, true);
130 $this->memcached->setOption(Memcached::OPT_COMPRESSION, false);
131 $this->memcached->setOption(Memcached::OPT_CONNECT_TIMEOUT, 2);
132 }
133 }