colly_wyx
2018-04-09 66c82a8cc3c870d3300bfb648824de7cab764a8e
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
<?php
/**
 * 键-值对的 MQ
 *
 * - 队列存放于Key-Value的缓存中
 *
 * @author dogstar <chanzonghuang@gmail.com> 20160430
 */
 
class Task_MQ_KeyValue implements Task_MQ {
 
    /**
     * @var PhalApi_Cache_Memcached/PhalApi_Cache_Memcache/PhalApi_Cache_File $kvCache 缓存实例
     */
    protected $kvCache;
 
    public function __construct(PhalApi_Cache $kvCache) {
        $this->kvCache = $kvCache;
    }
 
    public function add($service, $params = array()) {
        $list = $this->kvCache->get($service);
        if (empty($list)) {
            $list = array();
        }
 
        $list[] = $params;
 
        $this->kvCache->set($service, $list, $this->getExpireTime());
 
        $list = $this->kvCache->get($service);
 
        return true;
    }
 
    public function pop($service, $num = 1) {
        $rs = array();
        if ($num <= 0) {
            return $rs;
        }
 
        $list = $this->kvCache->get($service);
        if (empty($list)) {
            $list = array();
        }
 
        $rs = array_splice($list, 0, $num);
 
        $this->kvCache->set($service, $list, $this->getExpireTime());
 
        return $rs;
    }
 
    /**
     * 最大缓存时间,一年
     */
    protected function getExpireTime() {
        return 31536000;
    }
}