Test Failed
Push — master ( ba07e0...99686f )
by Jean-Christophe
23:13 queued 09:14
created

MemCachedDriver::store()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 2
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 3
crap 2
1
<?php
2
3
namespace Ubiquity\cache\system;
4
5
use Ubiquity\cache\CacheFile;
6
use Ubiquity\exceptions\CacheException;
7
8
/**
9
 * This class is responsible for storing values with MemCached.
10
 * Ubiquity\cache\system$MemCachedDriver
11
 * This class is part of Ubiquity
12
 *
13
 * @author jcheron <[email protected]>
14
 * @version 1.0.3
15
 *
16
 */
17
class MemCachedDriver extends AbstractDataCache {
18
	/**
19
	 *
20
	 * @var \Memcached
21
	 */
22
	private $cacheInstance;
23
	private const CONTENT = 'content';
24
	private const TAG = 'tag';
25
	private const TIME = 'time';
26
27
	/**
28
	 * Initializes the cache-provider
29
	 */
30
	public function __construct($root, $postfix = "", $cacheParams = [ ]) {
31
		parent::__construct ( $root, $postfix );
32
		$defaultParams = [ 'servers' => [ [ 'host' => '0.0.0.0','port' => 11211 ] ],'serializer' => \Memcached::SERIALIZER_PHP,'persistent' => true ];
33
		$cacheParams = \array_merge ( $defaultParams, $cacheParams );
34
		$this->cacheInstance = new \Memcached ( $cacheParams ['persistent'] ? \crc32 ( $root ) : null );
35
		if (isset ( $cacheParams ['serializer'] )) {
36
			$this->cacheInstance->setOption ( \Memcached::OPT_SERIALIZER, $cacheParams ['serializer'] );
37
		}
38
		if ($this->cacheInstance->isPristine ()) {
39
			$this->addServers ( $cacheParams ['servers'] );
40
		}
41
	}
42
43
	public function addServer($host, $port, $weight = null) {
44
		$this->cacheInstance->addServer ( $host, $port, $weight );
45
		$statuses = $this->cacheInstance->getStats ();
46
		if (! isset ( $statuses ["$host:$port"] )) {
47
			throw new CacheException ( "Connection to the server $host:$port failed!" );
48
		}
49
	}
50
51
	public function addServers(array $servers) {
52
		foreach ( $servers as $srv ) {
53
			$this->addServer ( $srv ['host'] ?? '0.0.0.0', $srv ['port'] ?? 11211, $srv ['weight'] ?? null);
54
		}
55
	}
56
57
	public function setSerializer($serializer) {
58
		$this->cacheInstance->setOption ( \Memcached::OPT_SERIALIZER, $serializer );
59
	}
60
61
	/**
62
	 * Check if annotation-data for the key has been stored.
63
	 *
64
	 * @param string $key cache key
65
	 * @return boolean true if data with the given key has been stored; otherwise false
66
	 */
67
	public function exists($key) {
68
		$k = $this->getRealKey ( $key );
69
		$this->cacheInstance->get ( $k );
70
		return \Memcached::RES_NOTFOUND !== $this->cacheInstance->getResultCode ();
71
	}
72
73
	public function store($key, $content, $tag = null) {
74
		$this->cacheInstance->set ( $this->getRealKey ( $key ), [ self::CONTENT => $content,self::TAG => $tag,self::TIME => \time () ] );
75
	}
76
77
	protected function getRealKey($key) {
78
		return \crc32 ( $key );
79
	}
80
81
	/**
82
	 * Fetches data stored for the given key.
83
	 *
84
	 * @param string $key cache key
85
	 * @return mixed the cached data
86
	 */
87
	public function fetch($key) {
88
		$entry = $this->cacheInstance->get ( $this->getRealKey ( $key ) );
89
		return $entry [self::CONTENT] ?? false;
90
	}
91
92
	/**
93
	 * return data stored for the given key.
94
	 *
95
	 * @param string $key cache key
96
	 * @return mixed the cached data
97
	 */
98
	public function file_get_contents($key) {
99
		return $this->cacheInstance->get ( $this->getRealKey ( $key ) ) [self::CONTENT];
100
	}
101
102
	/**
103
	 * Returns the timestamp of the last cache update for the given key.
104
	 *
105
	 * @param string $key cache key
106
	 * @return int unix timestamp
107
	 */
108
	public function getTimestamp($key) {
109
		$key = $this->getRealKey ( $key );
110
		return $this->cacheInstance->get ( $key ) [self::TIME];
111
	}
112
113
	public function remove($key) {
114
		$key = $this->getRealKey ( $key );
115
		$this->cacheInstance->delete ( $this->getRealKey ( $key ) );
116
	}
117
118
	public function clear() {
119
		$this->cacheInstance->flush ();
120
	}
121
122
	public function getCacheFiles($type) {
123
		$result = [ ];
124
		$keys = $this->cacheInstance->getAllKeys ();
125
126
		foreach ( $keys as $key ) {
127
			$entry = $this->cacheInstance->get ( $key );
128
			if ($entry [self::TAG] === $type) {
129
				$result [] = new CacheFile ( \ucfirst ( $type ), $key, $entry [self::TIME], "", $key );
130
			}
131
		}
132
		if (\sizeof ( $result ) === 0)
133
			$result [] = new CacheFile ( \ucfirst ( $type ), "", "", "" );
134
		return $result;
135
	}
136
137
	public function clearCache($type) {
138
		$keys = $this->cacheInstance->getAllKeys ();
139
		foreach ( $keys as $key ) {
140
			$entry = $this->cacheInstance->get ( $key );
141
			if ($entry [self::TAG] === $type) {
142
				$this->cacheInstance->delete ( $key );
143
			}
144
		}
145
	}
146
147
	public function getCacheInfo() {
148
		return parent::getCacheInfo () . "<br>Driver name : <b>" . \Memcached::class . "</b>";
149
	}
150
151
	public function getEntryKey($key) {
152
		return $this->getRealKey ( $key );
153
	}
154
}