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> '
<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> ?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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? 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 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 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? 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

How to use ECharts and php interfaces to implement data caching and updating of statistical charts. In web applications, statistical charts are often used to display data analysis results. ECharts is a popular open source JavaScript charting library that can help us create various types of interactive statistical charts. However, fetching data directly from the database and rendering charts may cause performance issues when the amount of data is very large or the data is updated frequently. In order to solve this problem, we can use the php interface to implement statistical charts

Golang is an efficient, concise, and reliable programming language that is increasingly used in web applications. In order to optimize the performance of web applications, developers usually need to use caching technology to reduce resource access and improve application response speed. In Golang, data caching, result caching and page caching are commonly used caching technologies. In this article, we will understand the purpose and usage of these 3 caching technologies. 1. Data caching Data caching refers to storing frequently accessed data in memory to improve

How to use PHP and SQLite for data caching and optimization Introduction: In the process of developing web applications, data caching and optimization are very important to improve performance and reduce the number of database queries. PHP provides rich database operation functions, while SQLite is a lightweight embedded database that is very suitable for caching data. This article will introduce how to use PHP and SQLite for data caching and optimization. 1. What is SQLite? SQLite is an embedded relational number.
