1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package CleverStyle CMS |
4
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
5
|
|
|
* @copyright Copyright (c) 2011-2016, Nazar Mokrynskyi |
6
|
|
|
* @license MIT License, see license.txt |
7
|
|
|
*/ |
8
|
|
|
namespace cs\Cache; |
9
|
|
|
use |
10
|
|
|
cs\Core; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Provides cache functionality based on Memcached. |
14
|
|
|
* Support optionally base configuration option Core::instance()->memcached_host and Core::instance()->memcached_port |
15
|
|
|
*/ |
16
|
|
|
class Memcached extends _Abstract_with_namespace { |
17
|
|
|
/** |
18
|
|
|
* @var \Memcached |
19
|
|
|
*/ |
20
|
|
|
protected $memcached; |
21
|
|
|
function __construct () { |
22
|
|
|
if (!extension_loaded('memcached')) { |
23
|
|
|
return; |
24
|
|
|
} |
25
|
|
|
$this->memcached = new \Memcached(DOMAIN); |
26
|
|
|
$Core = Core::instance(); |
27
|
|
|
$this->memcached->addServer($Core->memcached_host ?: '127.0.0.1', $Core->memcached_port ?: 11211); |
28
|
|
|
} |
29
|
|
|
/** |
30
|
|
|
* @inheritdoc |
31
|
|
|
*/ |
32
|
|
|
protected function available_internal () { |
33
|
|
|
return (bool)$this->memcached; |
34
|
|
|
} |
35
|
|
|
/** |
36
|
|
|
* @inheritdoc |
37
|
|
|
*/ |
38
|
|
|
protected function get_internal ($item) { |
39
|
|
|
return $this->memcached->get($item); |
40
|
|
|
} |
41
|
|
|
/** |
42
|
|
|
* @inheritdoc |
43
|
|
|
*/ |
44
|
|
|
protected function set_internal ($item, $data) { |
45
|
|
|
return $this->memcached->set($item, $data); |
46
|
|
|
} |
47
|
|
|
/** |
48
|
|
|
* @inheritdoc |
49
|
|
|
*/ |
50
|
|
|
protected function del_internal ($item) { |
51
|
|
|
return $this->memcached->delete($item); |
52
|
|
|
} |
53
|
|
|
/** |
54
|
|
|
* @inheritdoc |
55
|
|
|
*/ |
56
|
|
|
protected function increment_internal ($item) { |
57
|
|
|
return $this->memcached->increment($item); |
58
|
|
|
} |
59
|
|
|
/** |
60
|
|
|
* @inheritdoc |
61
|
|
|
*/ |
62
|
|
|
protected function clean_internal () { |
63
|
|
|
return $this->memcached->flush(); |
64
|
|
|
} |
65
|
|
|
/** |
66
|
|
|
* Close connections to memcached servers |
67
|
|
|
*/ |
68
|
|
|
function __destruct () { |
69
|
|
|
if ($this->memcached) { |
70
|
|
|
$this->memcached->quit(); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|