suchen
Heimphp教程php手册APP 接口开发及读取静态缓存,app读取静态缓存

APP 接口开发及读取静态缓存,app读取静态缓存

<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 .= "\n"; 65 $sXml .= self::xmlEncode($arData); 66 $sXml .= "\n"; 67 exit($sXml); 68 } 69 70 /** 71 * xml encode 72 * @author zhaoyingnan 2015-10-19 11:10 73 * @param array $arData 74 * @return string 75 **/ 76 private function xmlEncode($arData = array()) 77 { 78 if(!$arData) 79 return ''; 80 $sXml = $sAttr= ''; 81 foreach($arData as $mKey => $mVal) 82 { 83 if(is_numeric($mKey)) 84 { 85 $sAttr = " id='{$mKey}'"; 86 $mKey = 'item'; 87 } 88 $sXml .= is_array($mVal) ? self::xmlEncode($mVal) : "$mKey}{$sAttr}>{$mVal}{$mKey}>"; 89 } 90 return $sXml; 91 } 92 } 93 ?>
<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> ?>

 

Stellungnahme
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Python实现无头浏览器采集应用的页面数据缓存与增量更新功能剖析Python实现无头浏览器采集应用的页面数据缓存与增量更新功能剖析Aug 08, 2023 am 08:28 AM

Python实现无头浏览器采集应用的页面数据缓存与增量更新功能剖析导语:随着网络应用的不断普及,许多数据采集任务需要对网页进行抓取和解析。而无头浏览器通过模拟浏览器的行为,可以实现对网页的完全操作,使得页面数据的采集变得简单高效。本文将介绍使用Python实现无头浏览器采集应用的页面数据缓存与增量更新功能的具体实现方法,并附上详细的代码示例。一、基本原理无头

PHP和swoole如何实现高效的数据缓存和存储?PHP和swoole如何实现高效的数据缓存和存储?Jul 23, 2023 pm 04:03 PM

PHP和swoole如何实现高效的数据缓存和存储?概述:在Web应用开发中,数据的缓存和存储是非常重要的一部分。而PHP和swoole提供了一种高效的方法来实现数据的缓存与存储。本文将介绍如何使用PHP和swoole来实现高效的数据缓存和存储,并给出相应的代码示例。一、swoole简介:swoole是一个针对PHP语言开发的,高性能的异步网络通信引擎,它可以

Golang中数据缓存、结果缓存、页面缓存都应当怎样使用?Golang中数据缓存、结果缓存、页面缓存都应当怎样使用?Jun 19, 2023 pm 09:04 PM

Golang是一门高效、简洁、可靠的编程语言,其在Web应用程序方面的使用越来越广泛。为了优化Web应用程序的性能,开发人员通常需要使用缓存技术来减少资源的访问和提升应用程序的响应速度。在Golang中,数据缓存、结果缓存和页面缓存是常用的缓存技术。在本文中,我们将了解这3个缓存技术的用途和用法。1.数据缓存数据缓存是指将经常访问的数据存储到内存中,以提高

如何使用PHP和SQLite进行数据缓存和优化如何使用PHP和SQLite进行数据缓存和优化Jul 30, 2023 am 11:57 AM

如何使用PHP和SQLite进行数据缓存和优化引言:在开发Web应用程序的过程中,数据缓存和优化对于提高性能和减少数据库查询次数非常重要。PHP提供了丰富的数据库操作功能,而SQLite则是一种轻量级的嵌入式数据库,非常适合用于缓存数据。本文将介绍如何使用PHP和SQLite进行数据缓存和优化。一、什么是SQLiteSQLite是一种嵌入式的关系型数

小程序开发中的PHP数据缓存与缓存策略小程序开发中的PHP数据缓存与缓存策略Jul 05, 2023 pm 02:57 PM

小程序开发中的PHP数据缓存与缓存策略随着小程序的快速发展,更多的开发者开始关注如何提高小程序的性能和响应速度。其中一个重要的优化手段就是使用数据缓存来减少对数据库和外部接口的频繁访问。而在PHP中,我们可以利用各种缓存策略来实现数据缓存。本文将介绍PHP中的数据缓存原理,并提供几个常见的缓存策略的示例代码。一、数据缓存原理数据缓存是指将数据存放在内存中,以

UniApp实现数据缓存与持久化存储的最佳方案UniApp实现数据缓存与持久化存储的最佳方案Jul 05, 2023 pm 08:33 PM

UniApp是一种基于Vue.js的跨平台开发框架,可以将一个项目同时编译成多个平台上可运行的应用程序,如iOS、Android等。在开发移动应用时,数据的缓存和持久化存储是非常重要的方面,本文将介绍UniApp中实现数据缓存与持久化存储的最佳方案,并提供相应的代码示例。一、数据缓存在移动应用开发中,为了提高应用的用户体验,减少网络请求的次数和数据加载的时间

如何通过Webman框架实现数据缓存和页面缓存?如何通过Webman框架实现数据缓存和页面缓存?Jul 08, 2023 am 10:58 AM

如何通过Webman框架实现数据缓存和页面缓存?Webman是一款基于Python的Web框架,它具有轻量、灵活、易用的特点,并且支持多种插件和扩展。在Web开发中,实现数据缓存和页面缓存是提高网站性能和用户体验的重要手段之一。在本文中,我们将探讨如何通过Webman框架实现数据缓存和页面缓存,并给出相应的代码示例。一、数据缓存数据缓存是将一些频繁访问的数据

刨析PHP数据缓存的工作原理及应用场景刨析PHP数据缓存的工作原理及应用场景Aug 10, 2023 am 09:41 AM

刨析PHP数据缓存的工作原理及应用场景随着互联网技术的不断发展,用户访问量的大幅增加,对于数据的处理能力和效率要求也越来越高。在PHP开发中,数据缓存技术被广泛应用,可以有效地提高网站性能和用户体验。本文通过分析PHP数据缓存的工作原理,结合实际应用场景,来深入了解如何使用数据缓存提升网站性能。一、PHP数据缓存的工作原理PHP数据缓存是将一些经常使用的数据

See all articles

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

Heiße Werkzeuge

Dreamweaver Mac

Dreamweaver Mac

Visuelle Webentwicklungstools

SublimeText3 Linux neue Version

SublimeText3 Linux neue Version

SublimeText3 Linux neueste Version

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

SublimeText3 Englische Version

SublimeText3 Englische Version

Empfohlen: Win-Version, unterstützt Code-Eingabeaufforderungen!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Leistungsstarke integrierte PHP-Entwicklungsumgebung