我在PHP中构建了一个简单的脚本,它有两个功能:
我在没有数据库的情况下构建了这个,因为要求非常简单,并且由于客户端/主机设置MySQL数据库的策略将是乏味的。
现在出现了另一项要求。我需要添加一个函数来返回X个最新上传文件的列表,如果可能的话还要添加分页。
没有MySQL,最简单的方法是什么?或者是否足以保证建立数据库?
答案 0 :(得分:0)
如果它仍然可以使用文件那么简单。您可以将时间戳保存在纯文本文件中,并从中检索最后n行。如果您需要更复杂的东西,可以尝试sqlite或Kyoto cabinet。在您需要表达某些数据集之间的关系之前,您可能没有关系数据库。
答案 1 :(得分:0)
这是我的解决方案。我只是将新上传的ID写入纯文本文件。因为每个ID正好是37个字节(36 + \ n),所以文件可以随机访问。我还使用flock
使其成为线程安全的。
function gen_uuid() {
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
// 16 bits for "time_mid"
mt_rand( 0, 0xffff ),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand( 0, 0x0fff ) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand( 0, 0x3fff ) | 0x8000,
// 48 bits for "node"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}
// "constants"
$savedDir = dirname(__FILE__) . '/saved';
$orderedFile = dirname(__FILE__) . '/ordered.txt';
// prepare response
$resp = null;
// switch on action
$action = $_REQUEST['action'];
if ($action == 'saveVideo') {
// read params
$video = $_REQUEST['video'];
$face = $_REQUEST['face'];
// add current timestamp
$date = time();
// generate UUID
$id = gen_uuid();
// package data
$pack = array(
'id' => $id,
'video' => $video,
'face' => $face,
'date' => $date
);
// save package using ID as filename
file_put_contents($savedDir . '/' . $id, json_encode($pack));
// write new ID to file with ordered IDs
$f = fopen($orderedFile, 'a');
if(flock($f, LOCK_EX)) {
fseek($f, 0, SEEK_END);
fwrite($f, $id . "\n");
flock($f, LOCK_UN);
}
fclose($f);
$resp = array('id' => $id);
}
else if ($action == 'getLatestVideos') {
// $debug = '';
// parse parameters
$amount = intval($_REQUEST['amount']);
$start = intval($_REQUEST['start']);
// $debug .= "amount $amount start $start";
// find $amount latest IDs (skip $start)
$ids = array();
$f = fopen($orderedFile, 'r');
if(flock($f, LOCK_SH)) {
// get file size
$fst = fstat($f);
$fsize = $fst['size'];
// determine IDs available
$available = floor($fsize / 37);
// $debug .= " available $available";
if ($start < $available) {
// determine amount to seek back
$totamount = min($amount + $start, $available);
// $debug .= " totamount $totamount";
// seek back
fseek($f, -37 * $totamount, SEEK_END);
// now read forward until we have $amount (if could not seek back entirely, leave off the amount that would be sought back)
for ($i = 0; $i < min($amount, $available-$start); $i++) {
// read ID
$id = '';
while(strlen($id) < 36) {
$id .= fread($f, 36 - strlen($id));
}
// skip \n
fseek($f, 1, SEEK_CUR);
// add ID to list
$ids[] = $id;
}
}
flock($f, LOCK_UN);
}
fclose($f);
// reverse IDs so list becomes from new to old
$ids = array_reverse($ids);
// get datas from IDs
$resp = array();
foreach ($ids as $id) {
$resp[] = json_decode(file_get_contents($savedDir . '/' . $id));
}
// $resp[] = $debug;
}
else if ($action == 'getVideo') {
// read and clean ID
$id = strtolower($_REQUEST['id']);
$id = preg_replace('/[^a-f0-9-]/', '', $id);
// get data from ID
$resp = json_decode(file_get_contents($savedDir . '/' . $id));
}
// unknown operation
else {
$resp = 'Unknown action ' . $action;
}
// write response
echo json_encode($resp);