Blue74 | New and Evolving Projects

Jul/10

30

cerealDb – PHP Key Store without a Database

I created a PHP class that allows key storage (insert/update/delete/retrieve) that does not use a database, or a tool such as memcache.

If you can use memcache, do so, this was something I put together to use on a system in which I couldn’t use memcache. A simpler file cache may have been more appropriate, bu I prefer that all my key values stay in one file.

It could use a lot of optimizations, however, this is my first working version. It’s GPL’d so, if there is anyone that has a use for it, be my guest!

Usage:

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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
include 'cerealDb.php';
$x->collectionBaseDir('/tmp');
$x->useCollection('testing.cdb');
$x->insert("test","1234567890");
echo $x->read("test")."\n";
$x->update("test","0987654321");
echo $x->read("test")."\n";
$x->delete("test");
echo $x->read("test")."\n";
 
 
/**
* cerealDb, Serialized Array Storage
* @author Mike Curry
* @version 1.0
*
* Copyright (C) 2010 Mike Curry
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or any later
* version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
 
// CerealDb - Serialized array storage
Class cerealDb {
 
// location of storage
private $_collectionBaseDir;
private $_collectionName;
private $_lastWriteDate;
 
public $data = array();
 
public function __construct() {
}
 
public function collectionBaseDir($directory) {
if (!is_dir($directory)) {
echo $directory."\n";
return false;
}
 
if (substr($directory, -1, 1) != DIRECTORY_SEPARATOR) {
$directory .= DIRECTORY_SEPARATOR;
}
 
$this->_collectionBaseDir = $directory;
 
return true;
}
 
public function useCollection($collection) {
 
$this->_collectionName = trim($collection);
 
$file = $this->_collectionBaseDir.$collection;
 
// open for write, to create if missing
if ($fhandle = fopen($file, 'a+')) {
if (flock($fhandle, LOCK_EX)) {
if (filesize($file) > 0) {
$data = fread($fhandle, filesize($file));
if (strlen($data)) {
$this->data = unserialize($data);
} else {
$this->data = array();
}
}
flock($fhandle, LOCK_UN); // release the lock
}
fclose($fhandle);
 
$this->_lastWriteDate = filemtime($file);
}
 
return true;
}
 
public function insert($key, $data) {
 
if (strlen($this->_collectionName) == 0 ||
strlen($this->_collectionBaseDir) == 0) {
return false;
}
 
$result = false;
$file = $this->_collectionBaseDir.$this->_collectionName;
 
if ($fhandle = fopen($file, 'r+')) {
if (flock($fhandle, LOCK_EX)) {
 
// refresh file?
if (filemtime($file) != $this->_lastWriteDate) {
if (filesize($file) > 0) {
$this->data = unserialize(fread($fhandle, filesize($file)));
}
}
 
// only update if it doesn't exist
if (!isset($this->data[$key])) {
$this->data[$key] = $data;
 
// trunicate file, and update
ftruncate($fhandle, 0);
fwrite($fhandle, serialize($this->data));
$result = true;
}
 
flock($fhandle, LOCK_UN); // release the lock
}
fclose($fhandle);
 
// update file time
$this->_lastWriteDate = filemtime($file);
}
return $result;
}
 
public function update($key, $data) {
 
if (strlen($this->_collectionName) == 0 ||
strlen($this->_collectionBaseDir) == 0) {
return false;
}
 
$result = false;
$file = $this->_collectionBaseDir.$this->_collectionName;
 
if ($fhandle = fopen($file, 'r+')) {
if (flock($fhandle, LOCK_EX)) {
 
// refresh file?
if (filemtime($file) != $this->_lastWriteDate) {
if (filesize($file) > 0) {
$this->data = unserialize(fread($fhandle, filesize($file)));
}
}
 
// only update if exists
if (isset($this->data[$key])) {
$this->data[$key] = $data;
 
// trunicate file, and update
ftruncate($fhandle, 0);
fwrite($fhandle, serialize($this->data));
$result = true;
}
 
flock($fhandle, LOCK_UN); // release the lock
}
fclose($fhandle);
 
// update file time
$this->_lastWriteDate = filemtime($file);
}
return $result;
}
 
public function delete($key) {
 
if (strlen($this->_collectionName) == 0 ||
strlen($this->_collectionBaseDir) == 0) {
return false;
}
 
$result = false;
$file = $this->_collectionBaseDir.$this->_collectionName;
 
if ($fhandle = fopen($file, 'r+')) {
if (flock($fhandle, LOCK_EX)) {
 
// refresh file?
if (filemtime($file) != $this->_lastWriteDate) {
if (filesize($file) > 0) {
$this->data = unserialize(fread($fhandle, filesize($file)));
}
}
 
// only delete if exists
if (isset($this->data[$key])) {
unset($this->data[$key]);
 
// trunicate file, and update
ftruncate($fhandle, 0);
fwrite($fhandle, serialize($this->data));
$result = true;
}
 
flock($fhandle, LOCK_UN); // release the lock
}
fclose($fhandle);
 
// update file time
$this->_lastWriteDate = filemtime($file);
}
return $result;
}
 
public function read($key) {
 
if (strlen($this->_collectionName) == 0 ||
strlen($this->_collectionBaseDir) == 0) {
return false;
}
 
$result = false;
$file = $this->_collectionBaseDir.$this->_collectionName;
 
if ($fhandle = fopen($file, 'r+')) {
if (flock($fhandle, LOCK_EX)) {
 
// refresh file?
if (filemtime($file) != $this->_lastWriteDate) {
if (filesize($file) > 0) {
$this->data = unserialize(fread($fhandle, filesize($file)));
}
}
 
// only return data if exists
if (isset($this->data[$key])) {
$result = $this->data[$key];
}
 
flock($fhandle, LOCK_UN); // release the lock
}
fclose($fhandle);
 
// update file time
$this->_lastWriteDate = filemtime($file);
}
return $result;
}
}>

No tags

1 comment

Leave a Reply

<<

>>

Theme Design by devolux.nh2.me