colly_wyx
2018-05-18 50a53f7db63c5729b0ddbc93367117cf35ccf03c
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
<?php
/**
 * PhalApi_Cache_Memecahce MC缓存
 *
 * - 使用序列化对需要存储的值进行转换,以提高速度
 * - 默认不使用zlib对值压缩
 * - 请尽量使用Memcached扩展
 *
 * @package     PhalApi\Cache
 * @license     http://www.phalapi.net/license GPL 协议
 * @link        http://www.phalapi.net/
 * @author      PhpStorm George <plzhuangyuan@163.com> 15/5/6 下午8:53
 */
 
class PhalApi_Cache_Memcache implements PhalApi_Cache {
 
    protected $memcache = null;
 
    protected $prefix;
 
    /**
     * @param string        $config['host']     Memcache域名,多个用英文逗号分割
     * @param int/string    $config['port']     Memcache端口,多个用英文逗号分割
     * @param int/string    $config['weight']   Memcache权重,多个用英文逗号分割
     * @param string        $config['prefix']   Memcache key prefix
     */
    public function __construct($config) {
        $this->memcache = $this->createMemcache();
 
        $hostArr = explode(',', $config['host']);
        $portArr = explode(',', $config['port']);
        $weightArr = isset($config['weight']) ? explode(',', $config['weight']) : array();
 
        foreach ($hostArr as $idx => $host) {
            $this->memcache->addServer(
                trim($host),
                isset($portArr[$idx])   ? intval($portArr[$idx])    : 11211,
                isset($weightArr[$idx]) ? intval($weightArr[$idx])  : 0
            );
        }
 
        $this->prefix = isset($config['prefix']) ? $config['prefix'] : 'phalapi_';
    }
 
    public function set($key, $value, $expire = 600) {
        $this->memcache->set($this->formatKey($key), @serialize($value), 0, $expire);
    }
 
    public function get($key) {
        $value = $this->memcache->get($this->formatKey($key));
        return $value !== FALSE ? @unserialize($value) : NULL;
    }
 
    public function delete($key) {
        return $this->memcache->delete($this->formatKey($key));
    }
 
    /**
     * 获取MC实例,以便提供桩入口
     * @return Memcache
     */
    protected function createMemcache() {
        return new Memcache();
    }
 
    protected function formatKey($key) {
        return $this->prefix . $key;
    }
}