日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內(nèi)最全I(xiàn)T社區(qū)平臺 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁 > php開源 > php教程 > 客戶端調(diào)用服務(wù)端接口減少請求數(shù)據(jù)容量的優(yōu)化例子

客戶端調(diào)用服務(wù)端接口減少請求數(shù)據(jù)容量的優(yōu)化例子

來源:程序員人生   發(fā)布時(shí)間:2016-06-24 08:35:38 閱讀次數(shù):2434次

因項(xiàng)目需要,需要記錄用戶未來3天的預(yù)約時(shí)間,每一個(gè)預(yù)約時(shí)段為1小時(shí)。

例如:00:00:00~00:59:5901:00:00~01:59:59 等為1個(gè)預(yù)約時(shí)段

前端代碼以下:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <meta http-equiv="content-type" content="text/html;charset=utf⑻"> <title> 提交預(yù)約日期 </title> <style type="text/css"> .title{color:#FF0000;} .topic{font-size:18px; font-weight:bold;} </style> <script src="http://code.jquery.com/jquery⑴.11.3.min.js"></script> <script type="text/javascript"> function fsubmit(){ var timetable = []; $("#myform input[type=checkbox]").each(function(){ if(this.checked){ timetable.push($(this).val()); } }); if(timetable.length==0){ alert('請選擇預(yù)約時(shí)間'); return false; } $.post("http://localhost/server.php",{timetable:timetable}).done(function(data){ if(data['success']==true){ alert('提交成功'); }else{ alert('提交失敗'); } }); } </script> </head> <body> <p class="topic">請選擇預(yù)約時(shí)間:</p> <form name="form1" id="myform" method="post" onsubmit="return false"> <p class="title">2015-05-28:</p> <p> <input type="checkbox" name="timetable" value="2015-05⑵8 00:00:00"> 00:00 <input type="checkbox" name="timetable" value="2015-05⑵8 01:00:00"> 01:00 <input type="checkbox" name="timetable" value="2015-05⑵8 02:00:00"> 02:00 <input type="checkbox" name="timetable" value="2015-05⑵8 03:00:00"> 03:00 <input type="checkbox" name="timetable" value="2015-05⑵8 04:00:00"> 04:00 <input type="checkbox" name="timetable" value="2015-05⑵8 05:00:00"> 05:00 <input type="checkbox" name="timetable" value="2015-05⑵8 06:00:00"> 06:00 <input type="checkbox" name="timetable" value="2015-05⑵8 07:00:00"> 07:00 <input type="checkbox" name="timetable" value="2015-05⑵8 08:00:00"> 08:00 <input type="checkbox" name="timetable" value="2015-05⑵8 09:00:00"> 09:00 <input type="checkbox" name="timetable" value="2015-05⑵8 10:00:00"> 10:00 <input type="checkbox" name="timetable" value="2015-05⑵8 11:00:00"> 11:00 </p> <p> <input type="checkbox" name="timetable" value="2015-05⑵8 12:00:00"> 12:00 <input type="checkbox" name="timetable" value="2015-05⑵8 13:00:00"> 13:00 <input type="checkbox" name="timetable" value="2015-05⑵8 14:00:00"> 14:00 <input type="checkbox" name="timetable" value="2015-05⑵8 15:00:00"> 15:00 <input type="checkbox" name="timetable" value="2015-05⑵8 16:00:00"> 16:00 <input type="checkbox" name="timetable" value="2015-05⑵8 17:00:00"> 17:00 <input type="checkbox" name="timetable" value="2015-05⑵8 18:00:00"> 18:00 <input type="checkbox" name="timetable" value="2015-05⑵8 19:00:00"> 19:00 <input type="checkbox" name="timetable" value="2015-05⑵8 20:00:00"> 20:00 <input type="checkbox" name="timetable" value="2015-05⑵8 21:00:00"> 21:00 <input type="checkbox" name="timetable" value="2015-05⑵8 22:00:00"> 22:00 <input type="checkbox" name="timetable" value="2015-05⑵8 23:00:00"> 23:00 </p> <p class="title">2015-05-29:</p> <p> <input type="checkbox" name="timetable" value="2015-05⑵9 00:00:00"> 00:00 <input type="checkbox" name="timetable" value="2015-05⑵9 01:00:00"> 01:00 <input type="checkbox" name="timetable" value="2015-05⑵9 02:00:00"> 02:00 <input type="checkbox" name="timetable" value="2015-05⑵9 03:00:00"> 03:00 <input type="checkbox" name="timetable" value="2015-05⑵9 04:00:00"> 04:00 <input type="checkbox" name="timetable" value="2015-05⑵9 05:00:00"> 05:00 <input type="checkbox" name="timetable" value="2015-05⑵9 06:00:00"> 06:00 <input type="checkbox" name="timetable" value="2015-05⑵9 07:00:00"> 07:00 <input type="checkbox" name="timetable" value="2015-05⑵9 08:00:00"> 08:00 <input type="checkbox" name="timetable" value="2015-05⑵9 09:00:00"> 09:00 <input type="checkbox" name="timetable" value="2015-05⑵9 10:00:00"> 10:00 <input type="checkbox" name="timetable" value="2015-05⑵9 11:00:00"> 11:00 </p> <p> <input type="checkbox" name="timetable" value="2015-05⑵9 12:00:00"> 12:00 <input type="checkbox" name="timetable" value="2015-05⑵9 13:00:00"> 13:00 <input type="checkbox" name="timetable" value="2015-05⑵9 14:00:00"> 14:00 <input type="checkbox" name="timetable" value="2015-05⑵9 15:00:00"> 15:00 <input type="checkbox" name="timetable" value="2015-05⑵9 16:00:00"> 16:00 <input type="checkbox" name="timetable" value="2015-05⑵9 17:00:00"> 17:00 <input type="checkbox" name="timetable" value="2015-05⑵9 18:00:00"> 18:00 <input type="checkbox" name="timetable" value="2015-05⑵9 19:00:00"> 19:00 <input type="checkbox" name="timetable" value="2015-05⑵9 20:00:00"> 20:00 <input type="checkbox" name="timetable" value="2015-05⑵9 21:00:00"> 21:00 <input type="checkbox" name="timetable" value="2015-05⑵9 22:00:00"> 22:00 <input type="checkbox" name="timetable" value="2015-05⑵9 23:00:00"> 23:00 </p> <p class="title">2015-05-30:</p> <p> <input type="checkbox" name="timetable" value="2015-05⑶0 00:00:00"> 00:00 <input type="checkbox" name="timetable" value="2015-05⑶0 01:00:00"> 01:00 <input type="checkbox" name="timetable" value="2015-05⑶0 02:00:00"> 02:00 <input type="checkbox" name="timetable" value="2015-05⑶0 03:00:00"> 03:00 <input type="checkbox" name="timetable" value="2015-05⑶0 04:00:00"> 04:00 <input type="checkbox" name="timetable" value="2015-05⑶0 05:00:00"> 05:00 <input type="checkbox" name="timetable" value="2015-05⑶0 06:00:00"> 06:00 <input type="checkbox" name="timetable" value="2015-05⑶0 07:00:00"> 07:00 <input type="checkbox" name="timetable" value="2015-05⑶0 08:00:00"> 08:00 <input type="checkbox" name="timetable" value="2015-05⑶0 09:00:00"> 09:00 <input type="checkbox" name="timetable" value="2015-05⑶0 10:00:00"> 10:00 <input type="checkbox" name="timetable" value="2015-05⑶0 11:00:00"> 11:00 </p> <p> <input type="checkbox" name="timetable" value="2015-05⑶0 12:00:00"> 12:00 <input type="checkbox" name="timetable" value="2015-05⑶0 13:00:00"> 13:00 <input type="checkbox" name="timetable" value="2015-05⑶0 14:00:00"> 14:00 <input type="checkbox" name="timetable" value="2015-05⑶0 15:00:00"> 15:00 <input type="checkbox" name="timetable" value="2015-05⑶0 16:00:00"> 16:00 <input type="checkbox" name="timetable" value="2015-05⑶0 17:00:00"> 17:00 <input type="checkbox" name="timetable" value="2015-05⑶0 18:00:00"> 18:00 <input type="checkbox" name="timetable" value="2015-05⑶0 19:00:00"> 19:00 <input type="checkbox" name="timetable" value="2015-05⑶0 20:00:00"> 20:00 <input type="checkbox" name="timetable" value="2015-05⑶0 21:00:00"> 21:00 <input type="checkbox" name="timetable" value="2015-05⑶0 22:00:00"> 22:00 <input type="checkbox" name="timetable" value="2015-05⑶0 23:00:00"> 23:00 </p> <p><input type="submit" name="b1" onclick="fsubmit()" value="提交"></p> </form> </body> </html>

后端代碼以下:

<?php $data = $_POST['timetable']; file_put_contents('timetable.log', json_encode($data)); header('content-type:applicaiton/json;charset=utf8'); echo json_encode(array('success'=>true)); ?>



例如要提交某個(gè)用戶的3天預(yù)約數(shù)據(jù),所有時(shí)段都預(yù)約(全選)
后端收到的要求數(shù)據(jù)以下:

["2015-05⑵8 00:00:00","2015-05⑵8 01:00:00","2015-05⑵8 02:00:00","2015-05⑵8 03:00:00","2015-05⑵8 04:00:00","2015-05⑵8 05:00:00","2015-05⑵8 06:00:00","2015-05⑵8 07:00:00","2015-05⑵8 08:00:00","2015-05⑵8 09:00:00","2015-05⑵8 10:00:00","2015-05⑵8 11:00:00","2015-05⑵8 12:00:00","2015-05⑵8 13:00:00","2015-05⑵8 14:00:00","2015-05⑵8 15:00:00","2015-05⑵8 16:00:00","2015-05⑵8 17:00:00","2015-05⑵8 18:00:00","2015-05⑵8 19:00:00","2015-05⑵8 20:00:00","2015-05⑵8 21:00:00","2015-05⑵8 22:00:00","2015-05⑵8 23:00:00","2015-05⑵9 00:00:00","2015-05⑵9 01:00:00","2015-05⑵9 02:00:00","2015-05⑵9 03:00:00","2015-05⑵9 04:00:00","2015-05⑵9 05:00:00","2015-05⑵9 06:00:00","2015-05⑵9 07:00:00","2015-05⑵9 08:00:00","2015-05⑵9 09:00:00","2015-05⑵9 10:00:00","2015-05⑵9 11:00:00","2015-05⑵9 12:00:00","2015-05⑵9 13:00:00","2015-05⑵9 14:00:00","2015-05⑵9 15:00:00","2015-05⑵9 16:00:00","2015-05⑵9 17:00:00","2015-05⑵9 18:00:00","2015-05⑵9 19:00:00","2015-05⑵9 20:00:00","2015-05⑵9 21:00:00","2015-05⑵9 22:00:00","2015-05⑵9 23:00:00","2015-05⑶0 00:00:00","2015-05⑶0 01:00:00","2015-05⑶0 02:00:00","2015-05⑶0 03:00:00","2015-05⑶0 04:00:00","2015-05⑶0 05:00:00","2015-05⑶0 06:00:00","2015-05⑶0 07:00:00","2015-05⑶0 08:00:00","2015-05⑶0 09:00:00","2015-05⑶0 10:00:00","2015-05⑶0 11:00:00","2015-05⑶0 12:00:00","2015-05⑶0 13:00:00","2015-05⑶0 14:00:00","2015-05⑶0 15:00:00","2015-05⑶0 16:00:00","2015-05⑶0 17:00:00","2015-05⑶0 18:00:00","2015-05⑶0 19:00:00","2015-05⑶0 20:00:00","2015-05⑶0 21:00:00","2015-05⑶0 22:00:00","2015-05⑶0 23:00:00"]

這里寫圖片描述

要求數(shù)據(jù)content-length:2879

使用這類方式要求數(shù)據(jù)容量比較大,影響響應(yīng)時(shí)間。



改進(jìn)方法1:把DateTime格式時(shí)間轉(zhuǎn)為時(shí)間戳再提交

前端代碼修改以下:修改javascript fsubmit方法

<script type="text/javascript"> function fsubmit(){ var timetable = []; $("#myform input[type=checkbox]").each(function(){ if(this.checked){ // 把時(shí)間轉(zhuǎn)為時(shí)間戳 var time = $(this).val(); var timestamp = Date.parse(new Date(time)); timestamp = timestamp / 1000; timetable.push(timestamp); } }); if(timetable.length==0){ alert('請選擇預(yù)約時(shí)間'); return false; } $.post("http://localhost/server.php",{timetable:timetable}).done(function(data){ if(data['success']==true){ alert('提交成功'); }else{ alert('提交失敗'); } }); } </script>

后端收到的要求數(shù)據(jù)以下:

["1432742400","1432746000","1432749600","1432753200","1432756800","1432760400","1432764000","1432767600","1432771200","1432774800","1432778400","1432782000","1432785600","1432789200","1432792800","1432796400","1432800000","1432803600","1432807200","1432810800","1432814400","1432818000","1432821600","1432825200","1432828800","1432832400","1432836000","1432839600","1432843200","1432846800","1432850400","1432854000","1432857600","1432861200","1432864800","1432868400","1432872000","1432879200","1432882800","1432886400","1432890000","1432893600","1432897200","1432900800","1432904400","1432908000","1432911600","1432915200","1432918800","1432922400","1432926000","1432929600","1432933200","1432936800","1432940400","1432944000","1432947600","1432951200","1432954800","1432958400","1432962000","1432965600","1432969200","1432972800","1432976400","1432980000","1432983600","1432987200","1432990800","1432994400","1432998000"]

這里寫圖片描述

要求數(shù)據(jù)content-length:1916

后端收到后可把時(shí)間戳轉(zhuǎn)為datetime格式使用



改進(jìn)方法2:使用1個(gè)32位的整型變量,把每小時(shí)作為32位整型的其中1位來存儲

時(shí)段與32位整型的對應(yīng)關(guān)系是
從右至左開始是00:00,01:00,02:00,03:00 以此類推,如果有選擇的時(shí)段則為1,否則為0

例如:需要預(yù)約2016-05⑵8的 10:00,12:00,14:00,16:00,18:00

0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 疏忽 疏忽 疏忽 疏忽 疏忽 疏忽 疏忽 疏忽 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00

則用32位2進(jìn)制表示為 1010101010000000000 即10進(jìn)制349184 = 2的10次方 + 2的12次方 + 2的14次方 + 2的16次方 + 2的18次方


前端代碼修改以下:修改javascript fsubmit方法

<script type="text/javascript"> function fsubmit(){ var timetable = []; var tmp = {}; $("#myform input[type=checkbox]").each(function(){ if(this.checked){ // 拆分日期與時(shí)間 var datetime = $(this).val(); var datetime = datetime.split(' '); var day = datetime[0]; var time = parseInt(datetime[1].substring(0,2)); // 創(chuàng)建日期與時(shí)間數(shù)組 if(typeof(tmp[day])=='undefined'){ tmp[day] = []; } tmp[day].push(time); } }); // 合拼數(shù)據(jù),轉(zhuǎn)換格式 for(day in tmp){ // 根據(jù)數(shù)據(jù)集合,合拼,創(chuàng)建10進(jìn)制數(shù)據(jù) var time = tmp[day]; var timeint = 0; for(var i=0; i<time.length; i++){ timeint += Math.pow(2,time[i]); } timetable.push(day+' '+timeint); } if(timetable.length==0){ alert('請選擇預(yù)約時(shí)間'); return false; } $.post("http://localhost/server.php",{timetable:timetable}).done(function(data){ if(data['success']==true){ alert('提交成功'); }else{ alert('提交失敗'); } }); } </script>

后端收到的要求數(shù)據(jù)以下:

["2015-05⑵8 16777215","2015-05⑵9 16777215","2015-05⑶0 16777215"]

這里寫圖片描述

要求數(shù)據(jù)content-length:107

使用這類方法,1天不管選了多少個(gè)預(yù)約時(shí)段,都只會有1條記錄,大量減少提交的數(shù)據(jù)大小。


后端把10進(jìn)制轉(zhuǎn)換為時(shí)間的方法:
1.把10進(jìn)制轉(zhuǎn)為2進(jìn)制,并反轉(zhuǎn)
2.循環(huán)判斷每位,記錄值為1的
3.記錄保存


代碼以下:

<?php $datetime = '2015-05⑵8 1398016'; list($day, $time) = explode(' ', $datetime); $bintime = decbin($time); $bintime = strrev($bintime); $result = array(); for($i=0,$len=strlen($bintime); $i<$len; $i++){ if(substr($bintime, $i, 1)==1){ $result[] = $day.' '.str_pad($i, 2, '0', STR_PAD_LEFT).':00:00'; } } print_r($result); ?>

輸出:

Array ( [0] => 2015-05-28 08:00:00 [1] => 2015-05-28 10:00:00 [2] => 2015-05-28 12:00:00 [3] => 2015-05-28 14:00:00 [4] => 2015-05-28 16:00:00 [5] => 2015-05-28 18:00:00 [6] => 2015-05-28 20:00:00 )



總結(jié):本文提供1種可以把大量數(shù)據(jù)轉(zhuǎn)換為小容量數(shù)據(jù)傳輸?shù)姆椒?,但需要加入更多的運(yùn)算。因此實(shí)際開發(fā)中需根據(jù)實(shí)際需求來判斷是不是合適用時(shí)間換空間,或空間換時(shí)間的算法。

生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 91久久国产| 国产精品久久久一区麻豆最新章节 | av一区二区三区在线播放 | 中文一区 | 高清在线观看av | 亚洲精品一区二区在线观看 | 欧美精品一区在线发布 | 久色成人 | 国产精品一区二区在线 | 五月激情网址 | 日本久久一区二区 | 成人精品国产免费网站 | 国产h在线观看 | 欧美一区| 亚洲一二三四区电影 | 在线中文字幕亚洲 | 优优亚洲精品久久久久久久 | 国产精品久久久久久久久久新婚 | 国产另类在线视频 | 在线视频亚洲一区 | 成人99| 欧美日韩精品 | 国产亚洲视频在线 | 亚洲精品国产福利 | 久久久不卡 | 国产精品大片 | 国产激情在线视频 | 91人人看| 国产精品永久在线 | 午夜视频在线免费观看 | 在线a视频网站 | 日韩欧美电影在线观看 | 国产成人精品午夜 | 成人在线观看av | 午夜久久精品 | 亚洲精品自拍偷拍 | 在线中文视频 | 国产91在线 | 亚洲 | 国产精品99久久 | 中文字幕第一页在线播放 | 国产色网|