Table of Contents
APP 接口开发及读取静态缓存,app读取静态缓存
Home Backend Development PHP Tutorial APP 接口开发及读取静态缓存,app读取静态缓存_PHP教程

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

Jul 12, 2016 am 09:07 AM
Data cache

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> '
Copy after login
'; 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> ?>
Copy after login
<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> ?>
Copy after login

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1061852.htmlTechArticleAPP 接口开发及读取静态缓存,app读取静态缓存 1 ? php 2 /* * 3 * Description: App 接口 4 * Create date:2015-10-19 13:36 5 * Author: zhaoyingnan 6 * */ 7 class Res...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Optimization strategies for data caching and memory tables in PHP and MySQL indexes and their impact on query performance Optimization strategies for data caching and memory tables in PHP and MySQL indexes and their impact on query performance Oct 15, 2023 pm 12:01 PM

Optimization strategies for data caching and in-memory tables of PHP and MySQL indexes and their impact on query performance Introduction: PHP and MySQL are a very common combination when developing and optimizing database-driven applications. In the interaction between PHP and MySQL, index data caching and memory table optimization strategies play a crucial role in improving query performance. This article will introduce the optimization strategies for data caching and memory tables of PHP and MySQL indexes, and explain their impact on query performance in detail with specific code examples.

How to choose a data caching solution suitable for PHP projects? How to choose a data caching solution suitable for PHP projects? Aug 10, 2023 pm 09:21 PM

How to choose a data caching solution suitable for PHP projects? With the rapid development of the Internet and the advent of the big data era, how to efficiently handle data access and caching has become an important issue for PHP projects. As a common performance optimization method, data caching can effectively improve the response speed and user experience of the website. However, when choosing a data caching solution suitable for PHP projects, we need to consider a series of factors, including cache type, data access mode, caching strategy, etc. This article will discuss how to choose from these aspects

Data caching and local storage experience sharing in Vue project development Data caching and local storage experience sharing in Vue project development Nov 03, 2023 am 09:15 AM

Data caching and local storage experience sharing in Vue project development In the development process of Vue project, data caching and local storage are two very important concepts. Data caching can improve application performance, while local storage can achieve persistent storage of data. In this article, I will share some experiences and practices in using data caching and local storage in Vue projects. 1. Data caching Data caching is to store data in memory so that it can be quickly retrieved and used later. In Vue projects, there are two commonly used data caching methods:

Analysis of page data caching and incremental update functions of Python implementation for headless browser collection applications Analysis of page data caching and incremental update functions of Python implementation for headless browser collection applications Aug 08, 2023 am 08:28 AM

Analysis of page data caching and incremental update functions for headless browser collection applications implemented in Python Introduction: With the continuous popularity of network applications, many data collection tasks require crawling and parsing web pages. The headless browser can fully operate the web page by simulating the behavior of the browser, making the collection of page data simple and efficient. This article will introduce the specific implementation method of using Python to implement the page data caching and incremental update functions of a headless browser collection application, and attach detailed code examples. 1. Basic principles: headless

How do PHP and swoole achieve efficient data caching and storage? How do PHP and swoole achieve efficient data caching and storage? Jul 23, 2023 pm 04:03 PM

How do PHP and swoole achieve efficient data caching and storage? Overview: In web application development, data caching and storage are a very important part. PHP and swoole provide an efficient method to cache and store data. This article will introduce how to use PHP and swoole to achieve efficient data caching and storage, and give corresponding code examples. 1. Introduction to swoole: swoole is a high-performance asynchronous network communication engine developed for PHP language. It can

Application of queue technology in delayed message processing and data caching in PHP and MySQL Application of queue technology in delayed message processing and data caching in PHP and MySQL Oct 15, 2023 am 08:03 AM

Application of queue technology in delayed message processing and data caching in PHP and MySQL Introduction: With the rapid development of the Internet, the demand for real-time data processing is getting higher and higher. However, traditional database operation methods often cause performance bottlenecks when processing large amounts of real-time data. In order to solve this problem, queue technology came into being, which can help us implement asynchronous processing of data and improve system performance and response speed. This article will introduce the application of queue technology in delayed message processing and data caching in PHP and MySQL, and through specific code

Data caching and caching strategies for real-time chat functionality using PHP Data caching and caching strategies for real-time chat functionality using PHP Aug 25, 2023 pm 09:36 PM

Data caching and caching strategies for real-time chat function using PHP Introduction: In modern social media and Internet applications, real-time chat function has become an important part of user interaction. In order to provide an efficient real-time chat experience, data caching and caching strategies have become the focus of developers. This article will introduce data caching and caching strategies for implementing real-time chat functionality using PHP, and provide relevant code examples. 1. The role of data caching Data caching is to reduce the burden on the database and improve the response speed of the system. in live chat

UniApp is the best solution for data caching and persistent storage UniApp is the best solution for data caching and persistent storage Jul 05, 2023 pm 08:33 PM

UniApp is a cross-platform development framework based on Vue.js, which can compile a project into applications that can run on multiple platforms at the same time, such as iOS, Android, etc. When developing mobile applications, data caching and persistent storage are very important aspects. This article will introduce the best solution for implementing data caching and persistent storage in UniApp, and provide corresponding code examples. 1. Data caching in mobile application development, in order to improve the user experience of the application and reduce the number of network requests and data loading time

See all articles