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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
/**
* memcache接続用クラス
* 実際は、設定を保持して、同じコネクションを使うようにするためのもの。
* $Id: MemcacheManager.inc,v 1.1 2015/10/08 11:18:57 wanggb Exp $
* @author iimuro
* @access public
* @package jp.aimslib2.net
*/
class MemcacheManager{
public $host;
public $port;
public $memcache = null;
public $is_connected = false;
/**
* コンストラクタ
*/
public function __construct(){
}
/**
* 初期化
*/
public function init($host, $port = "11211"){
if (!defined("MEMCACHE_VERSION")) {
define("MEMCACHE_VERSION", 1);
}
//$this->memcache = new Memcache();
$this->host = $host;
if(isset($port)){
$this->port = $port;
}else{
$this->port = "11211";
}
//$this->memcache = memcache_connect($this->host, $this->port);
$this->memcache = new Memcache();
$res = @$this->memcache->pconnect($this->host, $this->port);
if(!$this->memcache || !$res) {
ErrorLogger::doOutput("memcache connection error!", 0, "memcache");
return;
}
$this->is_connected = true;
}
/**
* 追加の接続サーバーを指定します。
*/
public function addServer($host, $port) {
if (MEMCACHE_VERSION < 2) {
ErrorLogger::doOutput("unimplimented function: addServer", 0);
return false;
}
return $this->memcache->addServer($host, $port);
}
/**
* 接続をきります。
*/
public function close() {
if ($this->memcache != null) {
return $this->memcache->close();
}
return false;
}
/**
* 接続します。
*/
public function pconnect() {
if (!$this->is_connected) {
ErrorLogger::doOutput("new connection!", 0, "memcache");
$this->memcache = new Memcache();
$this->is_connected = @$this->memcache->pconnect($this->host, $this->port);
//$this->memcache = memcache_connect($this->host, $this->port);
//if($this->memcache->pconnect($this->host, $this->port) === false) {
//$this->is_connected = $this->memcache->connect($this->host, $this->port);
if(!$this->memcache || !$this->is_connected) {
ErrorLogger::doOutput("memcache connection error!", 0, "memcache");
return;
}
$this->is_connected = true;
ErrorLogger::doOutput($this->is_connected, 0, "memcache");
} else {
//ErrorLogger::doOutput("memcache persistant OK!", 0, "memcache");
}
}
/**
* 減算
*/
public function decrement($key, $value = 1) {
$this->pconnect();
if ($this->is_connected) {
return $this->memcache->decrement($key, $value);
} else {
ErrorLogger::doOutput("memcache connection error! increment", 0, "memcache");
return null;
}
}
/**
* キーを削除します。
*/
public function delete($key) {
$this->pconnect();
return $this->memcache->delete($key);
}
/**
* データを削除します。
*/
public function flush() {
$this->pconnect();
return $this->memcache->flush();
}
public function get($keys) {
$this->pconnect();
if ($this->is_connected) {
return $this->memcache->get($keys);
} else {
ErrorLogger::doOutput("memcache connection error! get", 0, "memcache");
return null;
}
}
public function getExtendedStats() {
if (MEMCACHE_VERSION < 2) {
ErrorLogger::doOutput("unimplimented function: getExtendedStats", 0);
return false;
}
return $this->memcache->getExtendedStats();
}
/**
* 状態を取得する
*/
public function getStats() {
return $this->memcache->getStats();
}
/**
* バージョンを取得する
*/
public function getVersion() {
$this->pconnect();
return $this->memcache->getVersion();
}
/**
* 加算
*/
public function increment($key, $value = 1) {
$this->pconnect();
if ($this->is_connected) {
return $this->memcache->increment($key, $value);
} else {
ErrorLogger::doOutput("memcache connection error! increment", 0, "memcache");
return null;
}
}
/**
* 値をsetします
*/
public function set($key, $var, $flag = null, $expire = 0) {
$this->pconnect();
if ($this->is_connected) {
if ($expire > 2592000) {
ErrorLogger::doOutput("memcache expire exceeds limit. setting to max value:2592000", __CLASS__);
$expire = 2592000;
}
$this->memcache->set($key, $var, $flag, $expire);
} else {
ErrorLogger::doOutput("memcache connection error! set", 0, "memcache");
}
}
/**
* 大きな値に対する自動圧縮処理を有効にする
*/
public function setCompressThreshold($threshold, $min_savings ) {
$this->memcache->setCompressThreshold($threshold,$min_savings);
}
}
?>