ILIAS  release_8 Revision v8.24
class.ilMemcache.php
Go to the documentation of this file.
1<?php
2
25{
26 protected static ?\Memcached $memcache_object = null;
27
31 public function __construct(string $service_id, string $component)
32 {
33 if (!(self::$memcache_object instanceof Memcached) && $this->getInstallable()) {
37 $memcached = new Memcached();
38
39 if (ilMemcacheServer::count() > 0) {
40 $memcached->resetServerList();
41 $servers = [];
43 ->get();
44 foreach ($list as $ilMemcacheServer) {
45 $servers[] = array(
46 $ilMemcacheServer->getHost(),
47 $ilMemcacheServer->getPort(),
48 $ilMemcacheServer->getWeight(),
49 );
50 }
51 $memcached->addServers($servers);
52 }
53
54 self::$memcache_object = $memcached;
55 }
57 }
58
59 protected function getMemcacheObject(): ?\Memcached
60 {
62 }
63
64 public function exists(string $key): bool
65 {
66 return $this->getMemcacheObject()->get($this->returnKey($key)) !== null;
67 }
68
69 public function set(string $key, $serialized_value, int $ttl = null): bool
70 {
71 return $this->getMemcacheObject()
72 ->set($this->returnKey($key), $serialized_value, (int) $ttl);
73 }
74
78 public function get(string $key)
79 {
80 return $this->getMemcacheObject()->get($this->returnKey($key));
81 }
82
83 public function delete(string $key): bool
84 {
85 return $this->getMemcacheObject()->delete($this->returnKey($key));
86 }
87
88 public function flush(bool $complete = false): bool
89 {
90 return $this->getMemcacheObject()->flush();
91 }
92
93 protected function getActive(): bool
94 {
95 if ($this->getInstallable()) {
96 $stats = $this->getMemcacheObject()->getStats();
97
98 if (!is_array($stats)) {
99 return false;
100 }
101
102 foreach ($stats as $server) {
103 if ((int) $server['pid'] > 1) {
104 return true;
105 }
106 }
107
108 return false;
109 }
110
111 return false;
112 }
113
114 protected function getInstallable(): bool
115 {
116 return class_exists('Memcached');
117 }
118
119 public function getInstallationFailureReason(): string
120 {
121 $stats = $this->getMemcacheObject()->getStats();
122 $server_available = false;
123 foreach ($stats as $server) {
124 if ($server['pid'] > 0) {
125 $server_available = true;
126 }
127 }
128 if (!$server_available) {
129 return 'No Memcached-Server available';
130 }
131 return parent::getInstallationFailureReason();
132 }
133
137 public function serialize($value): string
138 {
139 return serialize($value);
140 }
141
146 public function unserialize($serialized_value)
147 {
148 return unserialize($serialized_value);
149 }
150
151 public function getInfo(): array
152 {
153 $return = [];
154 if ($this->isInstallable()) {
155 $return['__cache_info'] = $this->getMemcacheObject()->getStats();
156 foreach ($this->getMemcacheObject()->getAllKeys() as $key) {
157 $return[$key] = $this->getMemcacheObject()->get($key);
158 }
159 }
160 return $return;
161 }
162
163 public function isValid(string $key): bool
164 {
165 return true;
166 }
167}
static where($where, $operator=null)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(string $service_id, string $component)
ilGlobalCacheService constructor.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getInstallationFailureReason()
isValid(string $key)
Checks whether the cache key is valid or not.
flush(bool $complete=false)
static Memcached $memcache_object
unserialize($serialized_value)
exists(string $key)
$server
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
string $key
Consumer key/client ID value.
Definition: System.php:193