Rumah > Artikel > pembangunan bahagian belakang > APP 接口开发及读取静态缓存,app读取静态缓存_PHP教程
<span> 1</span> <?<span>php </span><span> 2</span> <span>/*</span><span>* </span><span> 3</span> <span> * Description: App 接口 </span><span> 4</span> <span> * Create date:2015-10-19 13:36 </span><span> 5</span> <span> * Author: zhaoyingnan </span><span> 6</span> <span> *</span><span>*/</span> <span> 7</span> <span>class</span><span> Response </span><span> 8</span> <span>{ </span><span> 9</span> <span>/*</span><span>* </span><span>10</span> <span> * 综合方式 </span><span>11</span> <span> * @author zhaoyingnan 2015-10-19 11:24 </span><span>12</span> <span> * @param int $iCode 状态码 </span><span>13</span> <span> * @param string $sMsg 提示信息 </span><span>14</span> <span> * @param mix $mixData data </span><span>15</span> <span> * @param string $sType 接口返回类型 </span><span>16</span> <span> * @return string </span><span>17</span> <span> *</span><span>*/</span> <span>18</span> <span>static</span> <span>function</span> show(<span>$iCode</span>, <span>$sMsg</span> = '', <span>$mixData</span> = '', <span>$sType</span> = 'json'<span>) </span><span>19</span> <span> { </span><span>20</span> <span>if</span>(!<span>is_numeric</span>(<span>$iCode</span><span>)) </span><span>21</span> <span>return</span> ''<span>; </span><span>22</span> <span>$arData</span> = <span>array</span><span>( </span><span>23</span> 'code' => <span>$iCode</span>, <span>24</span> 'message' => <span>$sMsg</span>, <span>25</span> 'data' => <span>$mixData</span> <span>26</span> <span> ); </span><span>27</span> <span>switch</span>(<span>$sType</span><span>) </span><span>28</span> <span> { </span><span>29</span> <span>case</span> 'array': <span>30</span> <span>echo</span> '<pre class="brush:php;toolbar:false">'<span>; </span><span>31</span> <span>print_r</span>(<span>$arData</span><span>); </span><span>32</span> <span>echo</span> ''; 33 break; 34 case 'xml': 35 self::xml($arData); 36 break; 37 default: 38 self::json($arData); 39 } 40 } 41 42 /** 43 * json 44 * @author zhaoyingnan 2015-10-19 10:21 45 * @param array $arData 46 * @return string 47 **/ 48 private function json($arData= array()) 49 { 50 exit(json_encode($arData)); 51 } 52 53 /** 54 * xml 55 * @author zhaoyingnan 2015-10-19 10:21 56 * @param array $arData 57 * @return string 58 **/ 59 private function xml($arData = array()) 60 { 61 header('Content-Type:text/xml'); 62 $sXml = ''; 63 $sXml .= "\n"; 64 $sXml .= "
<span> 1</span> <?<span>php </span><span> 2</span> <span>/*</span><span>* </span><span> 3</span> <span> * Description: 静态缓存 </span><span> 4</span> <span> * Create date:2015-10-19 13:36 </span><span> 5</span> <span> * Author: zhaoyingnan </span><span> 6</span> <span> *</span><span>*/</span> <span> 7</span> <span>class</span> <span>file</span> <span> 8</span> <span>{ </span><span> 9</span> <span>private</span> <span>$sExt</span> = '.txt'<span>; </span><span>10</span> <span>11</span> <span>/*</span><span>* </span><span>12</span> <span> * 生成/删除/获取 缓存 </span><span>13</span> <span> * @author zhaoyingnan 2015-10-19 11:33 </span><span>14</span> <span> * @param string $sKey 文件名 </span><span>15</span> <span> * @param mix $mixValue 被缓存的数据(为''时表示获取缓存,为NUll时为删除缓存文件,否则为生成缓存) </span><span>16</span> <span> * @param string $sPath 文件保存的路径 </span><span>17</span> <span> * @param int $iCacheTime 缓存时间(秒),0为永不过期 </span><span>18</span> <span> * @return boolean </span><span>19</span> <span> *</span><span>*/</span> <span>20</span> <span>public</span> <span>function</span> cacheData(<span>$sKey</span>, <span>$mixValue</span> = '', <span>$sPath</span> = '/alidata/www/lianxi/file/', <span>$iCacheTime</span> = 0<span>) </span><span>21</span> <span> { </span><span>22</span> <span>$sPath</span> = <span>rtrim</span>(<span>$sPath</span>, '/').'/'<span>; </span><span>23</span> <span>$sFileName</span> = <span>$sPath</span>.<span>$sKey</span>.<span>$this</span>-><span>sExt; </span><span>24</span> <span>//</span><span>生成缓存文件</span> <span>25</span> <span>if</span>(<span>$mixValue</span><span>) </span><span>26</span> <span> { </span><span>27</span> <span>if</span>(!<span>is_dir</span>(<span>$sPath</span><span>)) </span><span>28</span> <span>mkdir</span>(<span>$sPath</span>, 0777<span>); </span><span>29</span> <span>$iCacheTime</span> = <span>sprintf</span>('%011d', <span>$iCacheTime</span><span>); </span><span>30</span> <span>return</span> <span>file_put_contents</span>(<span>$sFileName</span>, <span>$iCacheTime</span>.json_encode(<span>$mixValue</span><span>)); </span><span>31</span> <span> } </span><span>32</span> <span>33</span> <span>if</span>(<span>is_file</span>(<span>$sFileName</span>) && !<span>$mixValue</span><span>) </span><span>34</span> <span> { </span><span>35</span> <span>if</span>(<span>is_null</span>(<span>$mixValue</span><span>)) </span><span>36</span> <span> { </span><span>37</span> <span>//</span><span>删除缓存</span> <span>38</span> <span>return</span> <span>unlink</span>(<span>$sFileName</span><span>); </span><span>39</span> <span> } </span><span>40</span> <span>41</span> <span>//</span><span>获取缓存</span> <span>42</span> <span>$sContent</span> = <span>file_get_contents</span>(<span>$sFileName</span><span>); </span><span>43</span> <span>$iTime</span> = <span>intval</span>(<span>substr</span>(<span>$sContent</span>, 0, 11<span>)); </span><span>44</span> <span>$sContent</span> = <span>substr</span>(<span>$sContent</span>, 11<span>); </span><span>45</span> <span>if</span>(<span>$iTime</span> != 0 && <span>$iTime</span> + <span>filemtime</span>(<span>$sFileName</span>) < <span>time</span><span>()) </span><span>46</span> <span> { </span><span>47</span> <span>//</span><span>过期了,删除</span> <span>48</span> <span>unlink</span>(<span>$sFileName</span><span>); </span><span>49</span> <span>return</span> <span>FALSE</span><span>; </span><span>50</span> <span> } </span><span>51</span> <span>return</span> <span>$sContent</span><span>; </span><span>52</span> <span> } </span><span>53</span> <span>else</span> <span>54</span> <span> { </span><span>55</span> <span>return</span> <span>FALSE</span><span>; </span><span>56</span> <span> } </span><span>57</span> <span> } </span><span>58</span> <span>} </span><span>59</span> ?>
<span> 1</span> <?<span>php </span><span> 2</span> <span>include</span> 'response.php'<span>; </span><span> 3</span> <span>include</span> 'file.php'<span>; </span><span> 4</span> <span>$_GET</span>['format'] = <span>isset</span>(<span>$_GET</span>['format']) && <span>in_array</span>(<span>$_GET</span>['format'], <span>array</span>('xml', 'json', 'array')) ? <span>$_GET</span>['format'] : 'json'<span>; </span><span> 5</span> <span>$file</span> = <span>new</span> <span>File</span><span>(); </span><span> 6</span> <span>//</span><span>删除缓存 </span><span> 7</span> <span>//exit(var_dump($file->cacheData('index_cache', null)));</span> <span> 8</span> <span> 9</span> <span>if</span>(!<span>$sContent</span> = <span>$file</span>->cacheData('index_cache'<span>)) </span><span>10</span> <span>{ </span><span>11</span> <span>//</span><span>echo "获取缓存失败\n"; </span><span>12</span> <span> //echo "获取数据\n";</span> <span>13</span> <span>$arData</span> = <span>array</span><span>( </span><span>14</span> 'id' => 1, <span>15</span> 'name' => 'TeddyNan', <span>16</span> 'sex' => 23, <span>17</span> <span>array</span><span>( </span><span>18</span> 'nani'=><span>array</span><span>( </span><span>19</span> 'g'=>'gg', <span>20</span> 2, <span>21</span> 4 <span>22</span> <span> ) </span><span>23</span> <span> ) </span><span>24</span> <span> ); </span><span>25</span> <span>//</span><span>echo "生成缓存\n";</span> <span>26</span> <span>27</span> <span>$file</span>->cacheData('index_cache', <span>$arData</span>, '/alidata/www/lianxi/file/', 0<span>); </span><span>28</span> Response::show(0, 'success', <span>$arData</span>, <span>$_GET</span>['format'<span>]); </span><span>29</span> <span>} </span><span>30</span> <span>else</span> <span>31</span> <span>{ </span><span>32</span> Response::show(0, 'success', json_decode(<span>$sContent</span>, <span>TRUE</span>), <span>$_GET</span>['format'<span>]); </span><span>33</span> <span>} </span><span>34</span> ?>