-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMemtex.php
More file actions
175 lines (144 loc) · 5.82 KB
/
Copy pathMemtex.php
File metadata and controls
175 lines (144 loc) · 5.82 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
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
<?php
class Memtex
{
// memcache class
private $mcache;
private $verbose = false;
/**
* host: Memcache host
* port: Memcache port
*/
public function connect($host, $port)
{
$this->verbose_output("connect(): Connecting to $host:$port");
$this->mcache = memcache_connect($host, $port);
if ($this->mcache === false)
{
$this->verbose_output("connect(): returning false due to memcache = false");
return false;
}
$this->verbose_output("connect(): success");
return true;
}
/**
* Request a lock for the specified lock name. Will not block, will instantly return true or false
* name: Unique name for the lock (see uniqid() if you need a way to generate one)
* expire: Expiry time in seconds. If unlock() is not explicitly called, the lock is released after this amount of time.
Set this value carefully, it will block any other process from getting this lock until this expires in case the lock is
not gracefully released. Recommended value is a few seconds, or however long is definitely longer than you could reasonably
need the lock. Maximum value is 2592000 (30 days), 0 means never expire. For longer durations, can be set to a unix time-stamp
value, as per memcache expiry specification (http://php.net/manual/en/memcache.set.php)
* Returns true if the lock was obtained, or false if not
*/
public function lock($name, $expire)
{
$this->verbose_output("lock() called: name - $name, expire - $expire");
// creates a lock value with a guid prefixed by the current machine's hostname
$lockvalue = uniqid(php_uname('n'), true);
$this->verbose_output("lock(): lockvalue: $lockvalue");
if ($this->mcache === false)
{
$this->verbose_output("lock(): returning false due to memcache = false");
return false;
}
if ($this->mcache->add($name, $lockvalue, false, $expire) === false)
{
$this->verbose_output("lock(): returning false due to memcache add() fail, implies item already exists");
return false;
}
// we retrieve the value and check it against the desired lock value because there are claims that add() above
// can return true from concurrent threads
if ($this->mcache->get($name) !== $lockvalue)
{
$this->verbose_output("lock(): returning false due to memcache get() check fail");
return false;
}
$this->verbose_output("lock(): returning true");
return true;
}
/**
* Retrieve meta-data for the lock (meta-data being whatever extra data you'd like to store)
* name: Name of the lock
* Returns false on failure, or the meta-data
*/
public function get_lock_metadata($name)
{
$this->verbose_output("get_lock_metadata() called: name - $name");
if($this->mcache === false)
{
$this->verbose_output("get_lock_metadata(): returning false due to memcache = false");
return false;
}
$item = $this->mcache->get($name."_metadata");
if ($item === false)
{
$this->verbose_output("get_lock_metadata(): returning false due to memcache get() fail");
return false;
}
$this->verbose_output("get_lock_metadata(): success returning $item");
return $item;
}
/**
* Set some meta-data for the lock (meta-data being whatever extra data you'd like to store)
* name: Name of the lock
* data: Meta-data for the lock
* expire: Expiry time for the meta-data. Maximum value is 2592000 (30 days), 0 means never expire.
Per memcache specification: http://php.net/manual/en/memcache.set.php.
* Returns true on success, false on failure
*/
public function set_lock_metadata($name, $data, $expire)
{
$this->verbose_output("set_lock_metadata() called: name - $name, data - $data, expire - $expire");
if($this->mcache === false)
{
$this->verbose_output("set_lock_metadata(): returning false due to memcache = false");
return false;
}
if ($this->mcache->replace($name."_metadata", $data, false, $expire) === false)
{
if ($this->mcache->set($name."_metadata", $data, false, $expire) === false)
{
$this->verbose_output("set_lock_metadata(): returning false due to memcache set() fail");
return false;
}
}
$this->verbose_output("set_lock_metadata(): success");
return true;
}
/**
* name: Name for the lock to unlock
* Returns true on success, false on failure
*/
public function unlock($name)
{
$this->verbose_output("unlock() called: name - $name");
if($this->mcache === false)
{
$this->verbose_output("unlock(): returning false due to memcache = false");
return false;
}
if ($this->mcache->delete($name) === false)
{
$this->verbose_output("unlock(): returning false due to memcache delete() fail");
return false;
}
$this->verbose_output("unlock(): success");
return true;
}
/**
* v: True for verbose, false for not
*/
public function verbose($v)
{
$this->verbose = $v;
}
/**
* If verbose is enabled, output the message
*/
public function verbose_output($m)
{
if ($this->verbose)
echo "(Memtex): $m\n";
}
}
?>