-
Notifications
You must be signed in to change notification settings - Fork 0
/
TillStore.php
228 lines (214 loc) · 5.75 KB
/
TillStore.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
<?php
/**
* TillStore.php
*
* PHP Version 5
*
* @category Database
* @package TillStore
* @author Till Klampaeckel <[email protected]>
* @license http://www.opensource.org/licenses/bsd-license.html BSD License
* @version SVN: $Id$
* @link http://github.com/till/TillStore
*/
/**
* TillStore_Exception
*/
require_once 'TillStore/Exception.php';
/**
* Inspired by the nosql berlin meetup, here's a key value store in PHP! :-)
*
* @category Database
* @package TillStore
* @author Till Klampaeckel <[email protected]>
* @license http://www.opensource.org/licenses/bsd-license.html BSD License
* @version Release: @package_version@
* @link http://github.com/till/TillStore
*/
class TillStore
{
protected $device = '/dev/shm';
protected $key = 'TillStore';
/**
* Construct
*
* @param mixed $device The device to write to, standard: /dev/shm
*
* @return $this
*/
public function __construct($device = null)
{
if ($device !== null) {
$this->device = $device;
}
}
/**
* Delete a key.
*
* @param string $var The name of the key.
*
* @return boolean 'true' if the operation is successful, 'false' otherwise.
* @uses self::getFilename()
*/
public function delete($var)
{
$var = trim($var);
if (empty($var)) {
return false;
}
$filename = $this->getFilename($var);
if (!file_exists($filename)) {
return false;
}
if (!is_readable($filename)) {
return false;
}
return @unlink ($filename);
}
/**
* Set a value!
*
* @param string $var The name of the key.
* @param mixed $value Any data type (int, float, string, object, array, ...)
* @param mixed $ttl Optional: The time to live, in seconds.
*
* @return mixed
*/
public function set($var, $value, $ttl = null)
{
if ($ttl !== null) {
if (!is_int($ttl)) {
throw new TillStore_Exception("Time to live has to be an integer.");
}
}
$filename = $this->getFilename($var);
$status = $this->write($filename, $value, $ttl);
return $value;
}
/**
* Garbage collection. Clean up all expired items.
*
* @return void
*/
public function gc()
{
foreach (glob("{$this->device}/{$this->key}-*") as $value) {
$data = file_get_contents($value);
$data = json_decode($data);
if (empty($data->ttl)) {
continue;
}
if ($data->ttl >= mktime()) {
continue;
}
unlink($value);
}
return;
}
/**
* Get an item from TillStore!
*
* @param string $var The name of the key.
* @param mixed $default The default value to return when nothing is found.
*
* @return mixed
* @uses self::read()
* @throws TillStore_Exception In case the key exists, but is not readable.
*/
public function get($var, $default = null)
{
$data = $this->read($var, $default);
if (!is_object($data) || empty($data)) {
return $default;
}
return unserialize($data->value);
}
/**
* Return an item's time to live!
*
* @param string $var The name of the key.
*
* @return mixed
* @uses self::read()
*/
public function getTtl($var)
{
$data = $this->read($var);
return $data->ttl;
}
/**
* Create a filename from a variable.
*
* @param string $var The name of the key.
*
* @return string
*/
protected function getFilename($var)
{
return $this->device . '/' . $this->key . '-' . md5(trim($var));
}
/**
* Read the key file.
*
* @param string $var The name of the key.
* @param mixed $default Optional: the default value.
*
* @return object
* @throws TillStore_Exception On permission error - when we can't read the file.
*
* @see self::get()
* @see self::getTtl()
* @uses self::getFileName()
*/
protected function read($var, $default = null)
{
$filename = $this->getFileName($var);
if (!file_exists($filename)) {
return $default;
}
if (!is_readable($filename)) {
throw new TillStore_Exception("Internal read error.");
}
$data = file_get_contents($filename);
$data = json_decode($data);
return $data;
}
/**
* Write to the key value store!
*
* This is pretty simple, we serialize the object -- just to be sure, create an
* array and push it in, along with the TTL.
*
* @param string $filename The filename to write the key to.
* @param mixed $value The value of the key.
* @param mixed $ttl The time to live.
*
* @return true
* @throws TillStore_Exception When the write fails.
* @see self::set()
*/
protected function write($filename, $value, $ttl)
{
// $fp = fopen($filename, 'w+');
// if ($fp === false) {
// throw new TillStore_Exception("Unable to write data.");
// }
$expire = null;
if ($ttl !== null) {
$expire = mktime() + $ttl;
}
$data = array(
'value' => serialize($value),
'ttl' => $expire,
);
$data = json_encode($data);
$status = file_put_contents($filename, $data);
if ($status === true) {
return $status;
}
throw new TillStore_Exception("Unable to write data.");
// fwrite($fp, $data);
// fclose($fp);
// return true;
}
}