search

<span> 1</span> <span>php
</span><span> 2</span>     <span>/*</span><span>*
</span><span> 3</span> <span>     * @desc 文件缓存
</span><span> 4</span>      <span>*/</span>
<span> 5</span>     <span>class</span><span> Cache{
</span><span> 6</span>         <span>const</span> C_FILE = '/Runtime/'<span>;
</span><span> 7</span>         <span>private</span> <span>$dir</span> = ''<span>;
</span><span> 8</span>         <span>const</span> EXT = '.tpl'<span>;
</span><span> 9</span>         <span>private</span> <span>$filename</span> = ''<span>;
</span><span>10</span>         <span>public</span> <span>function</span> __construct(<span>$dir</span> = ''<span>){
</span><span>11</span>             <span>$this</span>-><span>dir</span> = <span>$dir</span><span>;
</span><span>12</span>             
<span>13</span> <span>        }
</span><span>14</span>         <span>/*</span><span>*
</span><span>15</span> <span>         *    @desc 设置文件缓存
</span><span>16</span> <span>         *    @param string $key 文件名 
</span><span>17</span> <span>         *    @param unkonw $data 缓存数据
</span><span>18</span> <span>         *  @param int    $expire 过期时间
</span><span>19</span>          <span>*/</span>
<span>20</span>         <span>public</span> <span>function</span> set(<span>$key</span>,<span>$data</span>,<span>$expire</span> = 0<span>){
</span><span>21</span>             <span>$this</span>->filename =     <span>dirname</span>(<span>__FILE__</span>).self::C_FILE.<span>$this</span>-><span>dir</span>.<span>$key</span>.self::<span>EXT;
</span><span>22</span>             <span>if</span>(<span>file_exists</span>(<span>$this</span>-><span>filename)){
</span><span>23</span>                 <span>$res</span> = <span>$this</span>->get(<span>$key</span><span>);
</span><span>24</span>                 <span>if</span>(<span>md5</span>(<span>$res</span>) == <span>md5</span>(json_encode(<span>$data</span><span>) ) ){
</span><span>25</span>                     <span>return</span> <span>true</span><span>;
</span><span>26</span> <span>                }
</span><span>27</span> <span>            }
</span><span>28</span>             <span>if</span>(!<span>is_dir</span>(<span>dirname</span>(<span>$this</span>-><span>filename))){
</span><span>29</span>                 <span>mkdir</span>(<span>dirname</span>(<span>$this</span>->filename),0777<span>);
</span><span>30</span> <span>            }
</span><span>31</span>             
<span>32</span>             <span>$source</span> = <span>fopen</span>(<span>$this</span>->filename,'w+'<span>);
</span><span>33</span>             <span>fwrite</span>(<span>$source</span>,json_encode(<span>$data</span><span>));
</span><span>34</span>             <span>fclose</span>(<span>$source</span><span>);
</span><span>35</span> <span>        }
</span><span>36</span>         
<span>37</span>         <span>/*</span><span>*
</span><span>38</span> <span>         *    @desc 获取文件
</span><span>39</span> <span>         *  @param string $key 文件名
</span><span>40</span>          <span>*/</span>
<span>41</span>         <span>public</span> <span>function</span> get(<span>$key</span><span>){
</span><span>42</span>             <span>//</span><span>$filename = dirname(__FILE__).self::C_FILE.$this->dir.$key.self::EXT;</span>
<span>43</span>             <span>if</span>(!<span>file_exists</span>(<span>$this</span>-><span>filename)){
</span><span>44</span>                 <span>return</span> '缓存文件已经不存在'<span>;
</span><span>45</span>             }<span>else</span><span>{
</span><span>46</span>                 <span>$res</span> = <span>file_get_contents</span>(<span>$this</span>-><span>filename);
</span><span>47</span> <span>            }
</span><span>48</span>             <span>return</span> <span>$res</span><span>;
</span><span>49</span> <span>        }
</span><span>50</span>         <span>/*</span><span>*
</span><span>51</span> <span>         *    @desc 删除文件
</span><span>52</span> <span>         *  @param string $key 文件名
</span><span>53</span>          <span>*/</span>
<span>54</span>         <span>public</span> <span>function</span> del(<span>$key</span><span>){
</span><span>55</span>             <span>unlink</span>(<span>$this</span>-><span>filename);
</span><span>56</span> <span>        }
</span><span>57</span>         
<span>58</span> <span>    }
</span><span>59</span>     
<span>60</span>     <span>$data</span> = <span>array</span>('name'=>'song','age'=>20,'sex'=>'man','favority'=><span>array</span>('apple','banana'<span>));
</span><span>61</span>     <span>$cache</span> = <span>new</span><span> Cache();
</span><span>62</span>     <span>$cache</span>->set('cache',<span>$data</span><span>);
</span><span>63</span>     <span>//</span><span>$cache->get('cache');
</span><span>64</span> <span>    //$cache->del('cache');</span>

The above has introduced simple file classes, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement
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
华为GT3 Pro和GT4的差异是什么?华为GT3 Pro和GT4的差异是什么?Dec 29, 2023 pm 02:27 PM

许多用户在选择智能手表的时候都会选择的华为的品牌,其中华为GT3pro和GT4都是非常热门的选择,不少用户都很好奇华为GT3pro和GT4有什么区别,下面就就给大家介绍一下二者。华为GT3pro和GT4有什么区别一、外观GT4:46mm和41mm,材质是玻璃表镜+不锈钢机身+高分纤维后壳。GT3pro:46.6mm和42.9mm,材质是蓝宝石玻璃表镜+钛金属机身/陶瓷机身+陶瓷后壳二、健康GT4:采用最新的华为Truseen5.5+算法,结果会更加的精准。GT3pro:多了ECG心电图和血管及安

php blob怎么转filephp blob怎么转fileMar 16, 2023 am 10:47 AM

php blob转file的方法:1、创建一个php示例文件;2、通过“function blobToFile(blob) {return new File([blob], 'screenshot.png', { type: 'image/jpeg' })}”方法实现Blob转File即可。

入职后,我才明白什么叫Cache入职后,我才明白什么叫CacheJul 31, 2023 pm 04:03 PM

事情其实是这样的,当时领导交给我一个perf硬件性能监视的任务,在使用perf的过程中,输入命令perf list,我看到了以下信息:我的任务就要让这些cache事件能够正常计数,但关键是,我根本不知道这些misses、loads是什么意思。

修复:截图工具在 Windows 11 中不起作用修复:截图工具在 Windows 11 中不起作用Aug 24, 2023 am 09:48 AM

为什么截图工具在Windows11上不起作用了解问题的根本原因有助于找到正确的解决方案。以下是截图工具可能无法正常工作的主要原因:对焦助手已打开:这可以防止截图工具打开。应用程序损坏:如果截图工具在启动时崩溃,则可能已损坏。过时的图形驱动程序:不兼容的驱动程序可能会干扰截图工具。来自其他应用程序的干扰:其他正在运行的应用程序可能与截图工具冲突。证书已过期:升级过程中的错误可能会导致此issu简单的解决方案这些适合大多数用户,不需要任何特殊的技术知识。1.更新窗口和Microsoft应用商店应用程

如何修复无法连接到iPhone上的App Store错误如何修复无法连接到iPhone上的App Store错误Jul 29, 2023 am 08:22 AM

第1部分:初始故障排除步骤检查苹果的系统状态:在深入研究复杂的解决方案之前,让我们从基础知识开始。问题可能不在于您的设备;苹果的服务器可能会关闭。访问Apple的系统状态页面,查看AppStore是否正常工作。如果有问题,您所能做的就是等待Apple修复它。检查您的互联网连接:确保您拥有稳定的互联网连接,因为“无法连接到AppStore”问题有时可归因于连接不良。尝试在Wi-Fi和移动数据之间切换或重置网络设置(“常规”>“重置”>“重置网络设置”>设置)。更新您的iOS版本:

使用cache可以提高计算机运行速度这是因为什么使用cache可以提高计算机运行速度这是因为什么Dec 09, 2020 am 11:28 AM

使用cache可以提高计算机运行速度这是因为Cache缩短了CPU的等待时间。Cache是位于CPU和主存储器DRAM之间,规模较小,但速度很高的存储器。Cache的功能是提高CPU数据输入输出的速率;Cache容量小但速度快,内存速度较低但容量大,通过优化调度算法,系统的性能会大大改善。

cache是什么存储器?cache是什么存储器?Nov 25, 2022 am 11:48 AM

cache叫做高速缓冲存储器,是介于中央处理器和主存储器之间的高速小容量存储器,一般由高速SRAM构成;这种局部存储器是面向CPU的,引入它是为减小或消除CPU与内存之间的速度差异对系统性能带来的影响。Cache容量小但速度快,内存速度较低但容量大,通过优化调度算法,系统的性能会大大改善。

nginx反向代理缓存教程。nginx反向代理缓存教程。Feb 18, 2024 pm 04:48 PM

以下是nginx反向代理缓存的教程:安装nginx:sudoaptupdatesudoaptinstallnginx配置反向代理:打开nginx配置文件:sudonano/etc/nginx/nginx.conf在http块中添加以下配置来启用缓存:http{...proxy_cache_path/var/cache/nginxlevels=1:2keys_zone=my_cache:10mmax_size=10ginactive=60muse_temp_path=off;proxy_cache

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.