Memcache   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A set() 0 3 1
A get() 0 3 1
A delete() 0 3 1
1
<?php
2
3
namespace PhpConsole\Storage;
4
5
/**
6
 * Memcache storage for postponed response data.
7
 *
8
 * @package PhpConsole
9
 * @version 3.1
10
 * @link http://consle.com
11
 * @author Sergey Barbushin http://linkedin.com/in/barbushin
12
 * @copyright © Sergey Barbushin, 2011-2013. All rights reserved.
13
 * @license http://www.opensource.org/licenses/BSD-3-Clause "The BSD 3-Clause License"
14
 * @codeCoverageIgnore
15
 */
16
class Memcache extends ExpiringKeyValue {
17
18
	/** @var  \Memcache */
19
	protected $memcache;
20
21
	public function __construct($host = 'localhost', $port = 11211) {
22
		$this->memcache = new \Memcache();
23
		if(!$this->memcache->connect($host, $port)) {
24
			throw new \Exception('Unable to connect to Memcache server');
25
		}
26
	}
27
28
	/**
29
	 * Save data by auto-expire key
30
	 * @param $key
31
	 * @param string $data
32
	 * @param int $expire
33
	 */
34
	protected function set($key, $data, $expire) {
35
		$this->memcache->set($key, $data, null, $expire);
36
	}
37
38
	/**
39
	 * Get data by key if not expired
40
	 * @param $key
41
	 * @return string
42
	 */
43
	protected function get($key) {
44
		return $this->memcache->get($key);
45
	}
46
47
	/**
48
	 * Remove key in store
49
	 * @param $key
50
	 * @return mixed
51
	 */
52
	protected function delete($key) {
53
		$this->memcache->delete($key);
54
	}
55
}
56