Home > php教程 > PHP源码 > memcache通用类 包括hash分配

memcache通用类 包括hash分配

PHP中文网
Release: 2016-05-23 08:39:44
Original
1074 people have browsed it

1.  class.php

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

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

<?php

namespace YBL;

class memcache

{

    protected $config = array();

    public $memcache_obj = &#39;&#39;;

    protected $host_list = &#39;&#39;;

    protected $connectFailures = 0;

    public function __construct($config = array())

    //默认配置

    {

        $this->config = array(

            &#39;host&#39; => &#39;127.0.0.1&#39;,

            &#39;port&#39; => 11211,

            &#39;persistent&#39; => false,

            &#39;weight&#39; => 1,

            //超时时间

            &#39;timeout&#39; => 1,

            //重试间隔时间

            &#39;retry_interval&#39; => 15,

            &#39;status&#39; => false,

            //=============================

            //是否打开压缩

            &#39;threshold&#39; => 1,

            //压缩率

            &#39;min_savings&#39; => 0.2,

            //最大重试次数

            &#39;max_connect_retries&#39; => 5

        );

        //不带一致性hash分配方案

        if (!isset($config[&#39;memcache_deploy&#39;]) || !is_callable($config[&#39;memcache_deploy&#39;])) {

            $config[&#39;memcache_deploy&#39;] = array(

                __CLASS__,

                &#39;default_memcache_deploy&#39;

            );

        }

        //带一致性hash分配方案

        if (!isset($config[&#39;memcache_hdeploy&#39;]) || !is_callable($config[&#39;memcache_hdeploy&#39;])) {

            $config[&#39;memcache_hdeploy&#39;] = array(

                __CLASS__,

                &#39;default_memcache_hdeploy&#39;

            );

        }

        //错误处理

        if (!isset($config[&#39;failure_callback&#39;]) || !is_callable($config[&#39;failure_callback&#39;])) {

            $config[&#39;failure_callback&#39;] = array(

                __CLASS__,

                &#39;default_error_hander&#39;

            );

        }

        $this->config       = array_merge($this->config, $config);

        $this->memcache_obj = new \Memcache;

    }

    public function host($host_list = &#39;&#39;)

    {

        $this->host_list = $host_list;

        return $this;

    }

    //默认错误处理

    static public function default_error_hander($host = array())

    //var_dump($host);

    {

          

    }

    static public function default_memcache_hdeploy($host, $hash = &#39;&#39;)

    {

        static $node_object;

        if (!($node_object instanceof yhash)) {

            $config      = array(

                //虚拟节点20个

                &#39;replicas&#39; => 20

            );

            $node_object = new yhash($config);

            //添加节点

            $node_object->addTargets(array_keys($host));

        }

        return $node_object->lookup($hash);

    }

      

      

    static public function default_memcache_deploy($host)

    {

        $keys = array_keys($host);

        $key  = array_rand($keys, 1);

        return $keys[$key];

    }

      

    public function alink()

    {

          

        $default_host     = array(

            &#39;host&#39; => $this->config[&#39;host&#39;],

            &#39;port&#39; => $this->config[&#39;port&#39;],

            &#39;persistent&#39; => $this->config[&#39;persistent&#39;],

            &#39;weight&#39; => $this->config[&#39;weight&#39;],

            &#39;timeout&#39; => $this->config[&#39;timeout&#39;],

            &#39;retry_interval&#39; => $this->config[&#39;retry_interval&#39;],

            &#39;status&#39; => $this->config[&#39;status&#39;],

            &#39;failure_callback&#39; => $this->config[&#39;failure_callback&#39;],

            &#39;threshold&#39; => $this->config[&#39;threshold&#39;],

            &#39;min_savings&#39; => $this->config[&#39;min_savings&#39;]

        );

        $failure_callback = &#39;&#39;;

        if (!empty($this->host_list)) {

            foreach ($this->host_list as $_host) {

                $_host = array_merge($default_host, $_host);

                $failure_callback = function($host = &#39;&#39;, $port = 11211) use ($_host)

                {

                    return call_user_func_array($_host[&#39;failure_callback&#39;], array(

                        $_host

                    ));

                };

                $this->memcache_obj->addServer($_host[&#39;host&#39;], $_host[&#39;port&#39;], $_host[&#39;persistent&#39;], $_host[&#39;weight&#39;], $_host[&#39;timeout&#39;], $_host[&#39;retry_interval&#39;], $_host[&#39;status&#39;], $failure_callback);

                if ($_host[&#39;threshold&#39;]) {

                    $this->memcache_obj->setCompressThreshold($_host[&#39;threshold&#39;], $_host[&#39;min_savings&#39;]);

                }

            }

        } else {

            $failure_callback = function($host = &#39;&#39;, $port = 11211) use ($default_host)

            {

                return call_user_func_array($default_host[&#39;failure_callback&#39;], array(

                    $default_host

                ));

            };

            $this->memcache_obj->addServer($default_host[&#39;host&#39;], $default_host[&#39;port&#39;], $default_host[&#39;persistent&#39;], $default_host[&#39;weight&#39;], $default_host[&#39;timeout&#39;], $default_host[&#39;retry_interval&#39;], $default_host[&#39;status&#39;], $failure_callback);

            if ($default_host[&#39;threshold&#39;]) {

                $this->memcache_obj->setCompressThreshold($default_host[&#39;threshold&#39;], $default_host[&#39;min_savings&#39;]);

            }

              

        }

    }

      

    public function link()

    {

        $default_host = &#39;&#39;;

        $host_no      = -1;

        $return       = false;

        $_host        = array();

        if (!empty($this->host_list)) {

            $host_no = call_user_func_array($this->config[&#39;memcache_deploy&#39;], array(

                $this->host_list

            ));

            $_host   = $this->host_list[$host_no];

        }

          

        $default_host = array(

            &#39;host&#39; => $this->config[&#39;host&#39;],

            &#39;port&#39; => $this->config[&#39;port&#39;],

            &#39;persistent&#39; => $this->config[&#39;persistent&#39;],

            //&#39;weight&#39;=>$this->config[&#39;weight&#39;],

            &#39;timeout&#39; => $this->config[&#39;timeout&#39;],

            &#39;retry_interval&#39; => $this->config[&#39;retry_interval&#39;],

            //&#39;status&#39;=>$this->config[&#39;status&#39;],

            &#39;failure_callback&#39; => $this->config[&#39;failure_callback&#39;],

            &#39;threshold&#39; => $this->config[&#39;threshold&#39;],

            &#39;min_savings&#39; => $this->config[&#39;min_savings&#39;]

        );

        $_host        = array_merge($default_host, $_host);

          

        if ($_host[&#39;persistent&#39;]) {

            $return = @$this->memcache_obj->pconnect($_host[&#39;host&#39;], $_host[&#39;port&#39;], $_host[&#39;timeout&#39;]);

        } else {

            $return = @$this->memcache_obj->connect($_host[&#39;host&#39;], $_host[&#39;port&#39;], $_host[&#39;timeout&#39;]);

        }

        if (!$return) {

            $this->connectFailures++;

            if ($this->connectFailures <= $this->config[&#39;max_connect_retries&#39;]) {

                if (count($this->host_list) > 1) {

                    call_user_func_array($_host[&#39;failure_callback&#39;], array(

                        $_host

                    ));

                    usleep($_host[&#39;interval_time&#39;]);

                    unset($this->host_list[$host_no]);

                    $this->link();

                } else {

                    $this->connectFailures = 0;

                    call_user_func_array($_host[&#39;failure_callback&#39;], array(

                        $_host

                    ));

                }

            } else {

                $this->connectFailures = 0;

                call_user_func_array($_host[&#39;failure_callback&#39;], array(

                    $_host

                ));

            }

        } else {

            $this->connectFailures = 0;

            if ($_host[&#39;threshold&#39;]) {

                $this->memcache_obj->setCompressThreshold($_host[&#39;threshold&#39;], $_host[&#39;min_savings&#39;]);

            }

              

        }

    }

      

      

      

    public function hlink($hash = &#39;&#39;)

    {

          

        $default_host = &#39;&#39;;

        $host_no      = -1;

        $return       = false;

        $_host        = array();

        if (!empty($this->host_list)) {

            $host_no = call_user_func_array($this->config[&#39;memcache_hdeploy&#39;], array(

                $this->host_list,

                $hash

            ));

            $_host   = $this->host_list[$host_no];

        }

          

        $default_host = array(

            &#39;host&#39; => $this->config[&#39;host&#39;],

            &#39;port&#39; => $this->config[&#39;port&#39;],

            &#39;persistent&#39; => $this->config[&#39;persistent&#39;],

            //&#39;weight&#39;=>$this->config[&#39;weight&#39;],

            &#39;timeout&#39; => $this->config[&#39;timeout&#39;],

            &#39;retry_interval&#39; => $this->config[&#39;retry_interval&#39;],

            //&#39;status&#39;=>$this->config[&#39;status&#39;],

            &#39;failure_callback&#39; => $this->config[&#39;failure_callback&#39;],

            &#39;threshold&#39; => $this->config[&#39;threshold&#39;],

            &#39;min_savings&#39; => $this->config[&#39;min_savings&#39;],

            &#39;hash&#39; => $hash

        );

        $_host        = array_merge($default_host, $_host);

        if ($_host[&#39;persistent&#39;]) {

            $return = $this->memcache_obj->pconnect($_host[&#39;host&#39;], $_host[&#39;port&#39;], $_host[&#39;timeout&#39;]);

        } else {

            $return = $this->memcache_obj->connect($_host[&#39;host&#39;], $_host[&#39;port&#39;], $_host[&#39;timeout&#39;]);

        }

        if (!$return) {

              

            $this->connectFailures = 0;

            call_user_func_array($_host[&#39;failure_callback&#39;], array(

                $_host

            ));

              

        } else {

              

            if ($_host[&#39;threshold&#39;]) {

                $this->memcache_obj->setCompressThreshold($_host[&#39;threshold&#39;], $_host[&#39;min_savings&#39;]);

            }

              

        }

    }

    /*

    type类型 值reset, malloc, maps, cachedump, slabs, items, sizes

    slabid slab的id

    limit 返回的最大条数

    */

    public function _server($type = &#39;&#39;, $slabid = 1, $limit = 1000000)

    {

        //$this->memcache_obj->close();

        if ($type === &#39;&#39;)

            return $this->memcache_obj->getStats();

        if ($type != &#39;cachedump&#39;)

            return $this->memcache_obj->getStats($type);

        return $this->memcache_obj->getStats($type, $slabid, $limit);

    }

    public function _allserver($type = &#39;&#39;, $slabid = 1, $limit = 1000000)

    {

        if ($type === &#39;&#39;)

            return $this->memcache_obj->getExtendedStats();

        if ($type != &#39;cachedump&#39;)

            return $this->memcache_obj->getExtendedStats($type);

        return $this->memcache_obj->getExtendedStats($type, $slabid, $limit);

    }

    public function close()

    {

        return $this->memcache_obj->close();

    }

    public function slabid()

    {

        $ids = $id = array();

        $ids = $this->memcache_obj->getStats(&#39;slabs&#39;);

        foreach ($ids as $k => $v) {

            if (is_int($k)) {

                $id[] = $k;

            }

        }

        return $id;

    }

    //!!!!!!!!!!!============================================

    public function getallkey($limit = 1000000)

    {

        $ids = $this->slabid();

        $arr = array();

        foreach ($ids as $id) {

            $arr = array_merge(array_keys($this->memcache_obj->getStats(&#39;cachedump&#39;, $id, $limit)), $arr);

        }

        return $arr;

    }

    public function set($key, $var, $expire = 0, $flag = MEMCACHE_COMPRESSED)

    {

        return $this->memcache_obj->set($key, $var, $flag, $expire);

    }

      

    public function add($key, $var, $expire = 0, $flag = MEMCACHE_COMPRESSED)

    {

        return $this->memcache_obj->add($key, $var, $flag, $expire);

    }

      

    public function get($key, &$flag = 0)

    {

        $gflag  = 0;

        $return = $this->memcache_obj->get($key, $gflag);

        $flag   = $gflag;

        return $return;

    }

      

    public function delete($key, $timeout = 0)

    {

        if ($key === &#39;all&#39;) {

            return $this->memcache_obj->flush();

        }

        return $this->memcache_obj->delete($key, $timeout);

    }

    public function replace($key, $var, $expire = 0, $flag = MEMCACHE_COMPRESSED)

    {

        return $this->memcache_obj->replace($key, $var, $flag, $expire);

    }

      

    public function increment($key, $var)

    {

        return $this->memcache_obj->increment($key, (int) $var);

    }

      

    public function decrement($key, $var)

    {

        return $this->memcache_obj->decrement($key, (int) $var);

    }

      

    public function append($key, $var, $expire = 0, $flag = MEMCACHE_COMPRESSED)

    {

        $return = $this->memcache_obj->get($key);

        if (empty($return))

            return false;

        return $this->memcache_obj->replace($key, $return . $var, $flag, $expire);

    }

    public function prepend($key, $var, $expire = 0, $flag = MEMCACHE_COMPRESSED)

    {

        $return = $this->memcache_obj->get($key);

        if (empty($return))

            return false;

        return $this->memcache_obj->replace($key, $var . $return, $flag, $expire);

    }

    public function cas($key, $key2)

    {

        $return1 = $this->memcache_obj->get($key);

        if (empty($return1))

            return false;

        $return2 = $this->memcache_obj->get($key2);

        if (empty($return2))

            return false;

        return ($this->memcache_obj->replace($key, $return2)) && ($this->memcache_obj->replace($key2, $return));

    }

 }

Copy after login

2. memcache.php

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

<?php

namespace YBL;

require_once(&#39;../lib/memcache/class.php&#39;);

//====================================================================================

//默认配置

$dconfig  = array(

    //主机

    &#39;host&#39; => &#39;127.0.0.1&#39;,

    //端口

    &#39;port&#39; => 11211,

    //是否长连接

    &#39;persistent&#39; => false,

    //比重

    &#39;weight&#39; => 1,

    //超时时间

    &#39;timeout&#39; => 1,

    //重试间隔时间

    &#39;retry_interval&#39; => 15,

    //状态

    &#39;status&#39; => false,

    //=============================

    //是否打开压缩

    &#39;threshold&#39; => 1,

    //压缩率

    &#39;min_savings&#39; => 0.2,

    //最大重试次数

    &#39;max_connect_retries&#39; => 5,

    //不带一致性hash分配方案

    //回调函数

    //参数 主机数组

    &#39;memcache_deploy&#39; => &#39;default_memcache_deploy&#39;,

    //带一致性hash分配方案

    //回调函数

    //参数 主机数组 hash值

    &#39;memcache_hdeploy&#39; => &#39;default_memcache_hdeploy&#39;,

    //错误处理

    //回调函数

    //参数 主机数组

    &#39;failure_callback&#39; => &#39;default_error_hander&#39;

);

//====================================================================================

//连接

/*memcahe 三种连接方式

1.连接池addserver

2.普通connect/pconnect

3.带hash的connect/pconnect

*/

//1.连接池

$memcache = new memcache();

$host     = array(

    array(

        &#39;host&#39; => &#39;localhost&#39;,

        &#39;port&#39; => &#39;11211&#39;

    ),

    array(

        &#39;host&#39; => &#39;127.0.0.1&#39;,

        &#39;port&#39; => &#39;11211&#39;

    ),

    array(

        &#39;host&#39; => &#39;localhost&#39;,

        &#39;port&#39; => &#39;11212&#39;

    ),

    array(

        &#39;host&#39; => &#39;127.0.0.1&#39;,

        &#39;port&#39; => &#39;11212&#39;

    )

);

$memcache->host($host);

$memcache->alink();

//print_r($memcache->_allserver());

//-------------------------------------------------------------

//2.普通connect/pconnect

$memcache2 = new memcache();

$host      = array(

    array(

        &#39;host&#39; => &#39;localhost&#39;,

        &#39;port&#39; => &#39;11211&#39;

    ),

    array(

        &#39;host&#39; => &#39;127.0.0.1&#39;,

        &#39;port&#39; => &#39;11211&#39;

    ),

    array(

        &#39;host&#39; => &#39;localhost&#39;,

        &#39;port&#39; => &#39;11212&#39;

    ),

    array(

        &#39;host&#39; => &#39;127.0.0.1&#39;,

        &#39;port&#39; => &#39;11212&#39;

    )

);

$memcache2->host($host);

$memcache2->link();

//print_r($memcache2->_allserver());

//--------------------------------------------------------------

//3.带hash的connect/pconnect

//导入算法类

include(&#39;../lib/hash/class.php&#39;);

$memcache3 = new memcache();

$host      = array(

    array(

        &#39;host&#39; => &#39;localhost&#39;,

        &#39;port&#39; => &#39;11211&#39;

    ),

    array(

        &#39;host&#39; => &#39;127.0.0.1&#39;,

        &#39;port&#39; => &#39;11211&#39;

    ),

    array(

        &#39;host&#39; => &#39;localhost&#39;,

        &#39;port&#39; => &#39;11211&#39;

    ),

    array(

        &#39;host&#39; => &#39;127.0.0.1&#39;,

        &#39;port&#39; => &#39;11211&#39;

    )

);

$memcache3->host($host);

//$memcache3->hlink(&#39;233ybl&#39;);

//print_r($memcache3->_allserver());

//==================================================================================

//内置函数

//set($key, $var, $expire = 0, $flag = MEMCACHE_COMPRESSED)

//add($key, $var, $expire = 0, $flag = MEMCACHE_COMPRESSED)

//get($key, &$flag = 0)

//delete($key, $timeout = 0)

//replace($key, $var, $expire = 0, $flag = MEMCACHE_COMPRESSED)

//increment($key, $var)

//decrement($key, $var)

//append($key, $var, $expire = 0, $flag = MEMCACHE_COMPRESSED)

//prepend($key, $var, $expire = 0, $flag = MEMCACHE_COMPRESSED)

// cas($key, $key2)

$memcache3->hlink(&#39;32ybl&#39;);

$memcache3->set(&#39;32ybl&#39;, 55);

//$memcache3->increment(&#39;32ybl&#39;,333);

echo $memcache3->get(&#39;32ybl&#39;);

//======================================================================

//系统函数

//获取当前连接服务器信息

//_server($type = &#39;&#39;, $slabid = 1, $limit = 1000000)

//获取所有连接服务器信息

//_allserver($type = &#39;&#39;, $slabid = 1, $limit = 1000000)

//获取所有key名

//getallkey($limit=1000000)

var_dump($memcache->getallkey());

Copy after login

                           

       

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Issues
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template