ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
Memcached.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\Cache\Adaptor;
22 
24 
28 class Memcached extends BaseAdaptor implements Adaptor
29 {
30  private const PERSISTENT_ID = 'ilias_cache';
31  private ?\Memcached $server = null;
32 
33  public function __construct(Config $config)
34  {
35  parent::__construct($config);
36  if (!class_exists(\Memcached::class)) {
37  return;
38  }
39  if ($config->getNodes() === []) {
40  return;
41  }
42  $this->initServer($config);
43  }
44 
45 
46  public function isAvailable(): bool
47  {
48  return class_exists(\Memcached::class) && $this->server !== null && $this->server->getVersion() !== false;
49  }
50 
51  public function has(string $container, string $key): bool
52  {
53  return $this->server->get($this->buildKey($container, $key)) !== false;
54  }
55 
56  public function get(string $container, string $key): ?string
57  {
58  return $this->server->get($this->buildKey($container, $key)) ?: null;
59  }
60 
61  public function set(string $container, string $key, string $value, int $ttl): void
62  {
63  $this->server->set($this->buildKey($container, $key), $value, $ttl);
64  }
65 
66  public function delete(string $container, string $key): void
67  {
68  $this->server->delete($this->buildKey($container, $key), 0);
69  }
70 
71  public function flushContainer(string $container): void
72  {
73  $prefix = $this->buildContainerPrefix($container);
74  foreach ($this->server->getAllKeys() as $key) {
75  if (str_starts_with($key, $prefix)) {
76  $this->server->set($key, false, 0);
77  }
78  }
79  foreach ($this->server->getAllKeys() as $key) {
80  if (str_starts_with($key, $prefix)) {
81  $this->server->get(
82  $key
83  ); // invalidates the key, see https://www.php.net/manual/en/memcached.expiration.php
84  }
85  }
86  $this->server->flushBuffers();
87  // $this->lock(1);// see https://github.com/memcached/memcached/issues/307
88  }
89 
90  public function flush(): void
91  {
92  $this->server->flush();
93  }
94 
95  protected function initServer(Config $config): void
96  {
97  $this->server = new \Memcached(self::PERSISTENT_ID);
98  $nodes = $config->getNodes();
99  foreach ($nodes as $node) {
100  $this->server->addServer(
101  $node->getHost(),
102  $node->getPort(),
103  $node->getWeight() ?? 100 / count($nodes)
104  );
105  }
106  }
107 }
buildKey(string $container, string $key)
Definition: BaseAdaptor.php:42
buildContainerPrefix(string $container)
Definition: BaseAdaptor.php:47
server()
description: > This example shows how a Progress Bar can be rendered and updated by the server...
Definition: server.php:27
__construct(Config $config)
Definition: Memcached.php:33
$container
Definition: wac.php:13
has(string $container, string $key)
Definition: Memcached.php:51
flushContainer(string $container)
Definition: Memcached.php:71
__construct(Container $dic, ilPlugin $plugin)
initServer(Config $config)
Definition: Memcached.php:95