1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kemist\Cache\Storage; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* MemcacheStorage |
7
|
|
|
* |
8
|
|
|
* @package Kemist\Cache |
9
|
|
|
* |
10
|
|
|
* @version 1.1.3 |
11
|
|
|
*/ |
12
|
|
|
class MemcacheStorage extends AbstractStorage implements StorageInterface { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Server ip |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $server; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Port number |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
protected $port; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Info method |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $infoMethod = 'getStats'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Constructor |
34
|
|
|
* |
35
|
|
|
* @param array $options |
36
|
|
|
*/ |
37
|
|
|
public function __construct($memcache, array $options = array()) { |
38
|
|
|
$this->prefix = (isset($options['prefix']) ? $options['prefix'] : ''); |
39
|
|
|
$this->server = (isset($options['server']) ? $options['server'] : '127.0.0.1'); |
40
|
|
|
$this->port = (isset($options['port']) ? $options['port'] : 11211); |
41
|
|
|
$this->provider = $memcache; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Initialise Cache storage |
46
|
|
|
* |
47
|
|
|
* @return bool |
48
|
|
|
* |
49
|
|
|
* @throws \Kemist\Cache\Exception |
50
|
|
|
*/ |
51
|
|
|
public function init() { |
52
|
|
|
return $this->provider->connect($this->server, $this->port); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Checks if the specified name in cache exists |
57
|
|
|
* |
58
|
|
|
* @param string $name cache name |
59
|
|
|
* |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
public function has($name) { |
63
|
|
|
if ($this->provider->get($this->prefix . $name)) { |
64
|
|
|
return true; |
65
|
|
|
} |
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Saves the variable to the $name cache |
71
|
|
|
* |
72
|
|
|
* @param string $name cache name |
73
|
|
|
* @param mixed $val variable to be stored |
74
|
|
|
* @param bool $compressed |
75
|
|
|
* |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
public function store($name, $val, $compressed = false) { |
79
|
|
|
$realName = $this->prefix . $name; |
80
|
|
|
$success = true; |
81
|
|
|
if ($compressed && $this->provider->replace($realName, $val, 2) == false) { |
82
|
|
|
$success = $this->provider->set($realName, $val, 2); |
83
|
|
|
} elseif ($this->provider->replace($realName, $val) == false) { |
84
|
|
|
$success = $this->provider->set($realName, $val); |
85
|
|
|
} |
86
|
|
|
$success ? $this->storeName($name) : null; |
87
|
|
|
return $success; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Destructor |
92
|
|
|
* |
93
|
|
|
* @return type |
94
|
|
|
*/ |
95
|
|
|
public function __destruct() { |
96
|
|
|
return is_object($this->provider) ? $this->provider->close() : null; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
} |
100
|
|
|
|