blob: 1c4b685b0c2c409fbdc10355b4aeaa82d8064d17 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
<?php
namespace Studip\Cache;
use Memcached;
use Psr\Cache\CacheItemInterface;
/**
* Cache implementation using memcached.
*
* @author Marcus Lunzenauer <mlunzena@uos.de>
* @copyright (c) Authors
* @license GPL2 or any later version
* @since 5.0
*/
class MemcachedCache extends Cache
{
use KeyTrait;
private Memcached $memcache;
/**
* @return string A translateable display name for this cache class.
*/
public static function getDisplayName(): string
{
return _('Memcached');
}
public function __construct($servers)
{
if (!extension_loaded('memcached')) {
throw new \Exception('Memcache extension missing.');
}
$prefix = \Config::get()->STUDIP_INSTALLATION_ID;
$this->memcache = new Memcached('studip' . ($prefix ? '-' . $prefix : ''));
if (count($this->memcache->getServerList()) === 0) {
foreach ($servers as $server) {
$status = $this->memcache->addServer($server['hostname'], (int) $server['port']);
if (!$status) {
throw new \Exception("Could not add server: {$server['hostname']} @ port {$server['port']}");
}
}
}
}
/**
* Expire item from the cache.
*
* Example:
*
* # expires foo
* $cache->expire('foo');
*
* @param string $arg a single key.
* @returns void
*/
public function expire($arg)
{
$key = $this->getCacheKey($arg);
$this->memcache->delete($key);
}
/**
* Expire all items from the cache.
*/
public function flush()
{
$this->memcache->flush();
}
/**
* Return statistics.
*
* @StudipCache::getStats()
*
* @return array|array[]
*/
public function getStats(): array
{
return $this->memcache->getStats();
}
/**
* Return the Vue component name and props that handle configuration.
*
* @see Cache::getConfig()
*
* @return array
*/
public static function getConfig(): array
{
$currentCache = \Config::get()->SYSTEMCACHE;
// Set default config for this cache
$currentConfig = [
'servers' => []
];
// If this cache is set as system cache, use config from global settings.
if ($currentCache['type'] == __CLASS__) {
$currentConfig = $currentCache['config'];
}
return [
'component' => 'MemcachedCacheConfig',
'props' => $currentConfig
];
}
/**
* @inheritDoc
*/
public function getItem(string $key): CacheItemInterface
{
$item = new Item($key);
$value = $this->memcache->get($this->getCacheKey($key));
if ($this->memcache->getResultCode() !== Memcached::RES_NOTFOUND) {
// Set the value, even if it is the boolean value false:
$item->setHit();
$item->set($value);
}
return $item;
}
/**
* @inheritDoc
*/
public function hasItem(string $key): bool
{
return $this->memcache->checkKey($this->getCacheKey($key));
}
/**
* @inheritDoc
*/
public function save(CacheItemInterface $item): bool
{
$expiration = $this->getExpiration($item);
if ($expiration < 1) {
// The item would expire immediately.
return false;
}
$real_key = $this->getCacheKey($item->getKey());
return $this->memcache->set($real_key, $item->get(), $expiration);
}
}
|