forked from CoderKungfu/php-queue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemcache.php
More file actions
96 lines (86 loc) · 2.73 KB
/
Copy pathMemcache.php
File metadata and controls
96 lines (86 loc) · 2.73 KB
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
<?php
namespace PHPQueue\Backend;
use PHPQueue\Exception\BackendException;
class Memcache extends Base
{
public $servers;
public $is_persistent = false;
public $use_compression = false;
public $expiry = 0;
public function __construct($options=array())
{
parent::__construct();
if (!empty($options['servers']) && is_array($options['servers'])) {
$this->servers = $options['servers'];
}
if (!empty($options['persistent'])) {
$this->is_persistent = $options['persistent'];
}
if (!empty($options['compress']) && is_bool($options['compress'])) {
$this->use_compression = $options['compress'];
}
if (!empty($options['expiry']) && is_numeric($options['expiry'])) {
$this->expiry = $options['expiry'];
}
}
public function connect()
{
if (empty($this->servers)) {
throw new BackendException("No servers specified");
}
$this->connection = new \Memcache;
foreach ($this->servers as $server) {
if (is_string($server)) {
$this->connection->addserver($server, 11211, $this->is_persistent);
} elseif (is_array($server)) {
call_user_func_array(array($this->connection, 'addserver'), $server);
} else {
throw new BackendException("Unknown Memcache server arguments.");
}
}
}
/**
* @param string $key
* @param mixed $data
* @param int $expiry
* @return boolean
* @throws \PHPQueue\Exception
*/
public function add($key=null, $data=null, $expiry=null)
{
if (empty($key) && !is_string($key)) {
throw new BackendException("Key is invalid.");
}
if (empty($data)) {
throw new BackendException("No data.");
}
$this->beforeAdd();
if (empty($expiry)) {
$expiry = $this->expiry;
}
$status = $this->getConnection()->replace($key, $data, $this->use_compression, $expiry);
if ($status == false) {
$status = $this->getConnection()->set($key, $data, $this->use_compression, $expiry);
}
if (!$status) {
throw new BackendException("Unable to save data.");
}
return $status;
}
/**
* @param string $key
* @return mixed
*/
public function get($key=null)
{
$this->beforeGet($key);
return $this->getConnection()->get($key);
}
public function clear($key=null)
{
$this->beforeClear($key);
$this->getConnection()->delete($key);
$this->last_job_id = $key;
return true;
}
}